ios - 条形图(创建为 CALayer)高度/边界动画

标签 ios swift core-animation calayer

<分区>

我创建了一个包含条形图的图表 (CALayer)。它们的顶部和底部都有文本 (CATextLayer)。现在我想给条形添加一些动画(让它们从底部到顶部增加)。

这是我现在的代码(此方法创建条形并将其添加到主层 (mainLayer)):

 private func drawBar(xPos: CGFloat, yPos: CGFloat, color: UIColor? = .gray) {
    let initialBound = CGRect(x: xPos, y: mainLayer.frame.height - bottomSpace, width: barWidth, height: 0)
    let finalBound = CGRect(x: xPos, y: yPos, width: barWidth, height: mainLayer.frame.height - bottomSpace - yPos)
    let increaseBar = CABasicAnimation(keyPath: "bounds")
    increaseBar.fromValue = initialBound
    increaseBar.toValue = finalBound
    increaseBar.duration = 2.0

    let barLayer = CALayer()

    barLayer.frame = finalBound
    barLayer.cornerRadius = 20
    barLayer.backgroundColor = color?.cgColor
    barLayer.add(increaseBar, forKey: nil)
    mainLayer.addSublayer(barLayer)
}

这里是使用的属性:

  //Width of each bar
let barWidth: CGFloat = 40.0

//Space between each bar
let space: CGFloat = 20.0


//Space at the bottom of the bar to show the title
private let bottomSpace: CGFloat = 40.0

//Space at the top of each bar to show the value
private let topSpace: CGFloat = 40.0

xPosyPos 以这种方式找到(index 只是一个 Int,它以 for in 从零开始循环到条目数,在本例中,它等于 1):

      /// Starting x postion of the bar
     let xPos: CGFloat = space + CGFloat(index) * (barWidth + space)

    /// Starting y postion of the bar
    let yPos: CGFloat = translateHeightValueToYPosition(value: entry.height)

entry.height 只是 0.01.0 之间的任意数字。下面是 translateHeightValueToYPosition(value:) 方法的定义:

 private func translateHeightValueToYPosition(value: Float) -> CGFloat {

    let height: CGFloat = CGFloat(value) * (mainLayer.frame.height - bottomSpace - topSpace)
    return mainLayer.frame.height - bottomSpace - height
}

现在,一切正常,除了条形动画从它们总高度的中间开始。我试图更改 yPos 的值(手动)但没有成功。我还尝试通过最初将其设置为 0 来为栏的 height 设置动画,但同样没有成功。

这是动画的样子:

enter image description here

如何使条形从底部向顶部增加,而不是从它们高度的中间增加?我将不胜感激任何建议。

最佳答案

你的代码很好,唯一的问题是图层的 anchor 。默认的 anchorPoint 设置为 CGPoint(x: 0.5, y: 0.5) 这是中间的。所以你只需要改变它们。

对于顶部:CGPoint.zero

对于底部:CGPoint(x: 1, y: 1)

这里是正确的代码,只需要更正一次:

private func drawBar(xPos: CGFloat, yPos: CGFloat, color: UIColor? = .gray) {
let initialBound = CGRect(x: xPos, y: mainLayer.frame.height - bottomSpace, width: barWidth, height: 0)
let finalBound = CGRect(x: xPos, y: yPos, width: barWidth, height: mainLayer.frame.height - bottomSpace - yPos)
let increaseBar = CABasicAnimation(keyPath: "bounds")
increaseBar.fromValue = initialBound
increaseBar.toValue = finalBound
increaseBar.duration = 2.0

let barLayer = CALayer()
// my code line
barLayer.anchorPoint = CGPoint(x: 1, y: 1)
barLayer.frame = finalBound
barLayer.cornerRadius = 20
barLayer.backgroundColor = color?.cgColor
barLayer.add(increaseBar, forKey: nil)
mainLayer.addSublayer(barLayer)

希望我的回答能解决您的问题。 :)

关于ios - 条形图(创建为 CALayer)高度/边界动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49515340/

相关文章:

objective-c - 混合 CAGradientLayer 用作阴影

ios - 访问另一个数组中的 JSON 数组

ios - 检测在 NSString 中占用多个索引点的字符的方法?

ios - 从 Parse.com 获取 String 并将其设置为 UILabel (iOs Swift)?

ios - 播放 AVAudioPlayerNode 时 AVAudioEngine inputNode 的格式发生变化

ios - 如何同步几个不同图层的CAKeyframeAnimation动画?

ios - 无法使用类型为 'implode' 的参数列表调用 '(String)'

swift - 在 Swift 中传递条件作为参数?

ios - UIButton 未在 swift 中调用操作函数

ios - 无法在 CoreAnimation 完成后更新模型层