【问题标题】:How to use UIViewAnimationCurve options in CAAnimation如何在 CAAnimation 中使用 UIViewAnimationCurve 选项
【发布时间】:2017-10-10 10:10:43
【问题描述】:

我在 CAShapeLayer 中使用 CAAnimation 来简单地对从左到右的线条进行动画处理(它只是一个更复杂程序的缩减版)。我已经放慢了速度,以表明开始的四分之一几乎是瞬间的,然后其余的动画正常。

我已经看到可以使用 UIView.animate 的选项来设置 UIViewAnimationCurve 选项(例如,.EaseIn 或 .Linear)。在 CAShapeLayer 中嵌入 CAAnimation 时是否可以设置这些选项?并且每次要运行动画都需要创建,还是有“运行动画”命令?

以下是我的精简代码:

    // the containing UIView is called mainView

    mainView.layer.sublayers = nil

    // create a line using a UIBezierPath

    let path = UIBezierPath()
    path.move(to: CGPoint(x: 100, y: 100))
    path.addLine(to: CGPoint(x: 300, y: 100))

    // create a CAShapeLayer

    let shapeLayer = CAShapeLayer()
    shapeLayer.frame = CGRect(x: 0, y: 0, width: 1024, height: 1024)
    shapeLayer.strokeColor = UIColor.green.cgColor
    shapeLayer.lineWidth = 100
    shapeLayer.path = path.cgPath

    // create the animation

    mainView.layer.addSublayer(shapeLayer)
    let animation = CABasicAnimation(keyPath: "LetterStroke")

    animation.fromValue = 0
    animation.toValue = 1
    animation.duration = 10

    shapeLayer.add(animation, forKey: "animation")

非常感谢。

【问题讨论】:

    标签: ios swift3 caanimation


    【解决方案1】:

    您应该使用CABasicAnimationtimingFunction 属性。有一个构造函数接受these 预定义的计时函数,如

     animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseIn)
    

    【讨论】:

    • 这是正确答案,非常感谢。我实际上发现问题出在其他地方,如下所示,但事实上,您确实按照所述回答了我的问题。
    【解决方案2】:

    问题在于,线条动画似乎开始了四分之一。我忽略了在我的问题中放入原来是有问题的代码行。

    shapeLayer.lineCap = kCALineCapRound

    这与 100 的 lineWidth 相结合,使线条从开头大约四分之一处开始。我把 lineWidth 变小了,效果很好。

    【讨论】:

      猜你喜欢
      • 2011-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-16
      相关资源
      最近更新 更多