ios - 如何在Swift中为遵循BezierPath的imageView的点1到2和点2到3设置不同的animation.duration时间?

标签 ios swift caanimation

我有一个带有 BezierPath 的 UIView subview 和一个沿着从 a 到 b 的路径的 ImageView。该路径有 8 个点,我需要点 1 到点 2 的持续时间与点 2 到点 3 等的持续时间不同

我已经能够设置 View 和路径,并沿着路径为图像设置动画,但完整动画的持续时间相同。这是我的动画代码,任何帮助将不胜感激。

let pathToFollow = path
    let animation = CAKeyframeAnimation(keyPath: #keyPath(CALayer.position))
    animation.path = pathToFollow?.cgPath
    animation.duration = 10.0
    animation.calculationMode = kCAAnimationPaced
    animation.delegate = self
    self.icons[Icon].layer.add(animation, forKey: nil)

最佳答案

您有一个关键帧动画,这是一个好的开始。但您忘记添加一些实际的框架!为此,请提供一些keyTimes。 (并删除kCAAnimationPaced;这意味着整个动画具有单一速度,这与您所说的相反。)

请注意,对于此类路径动画,帧分割点是用于定义路径的贝塞尔点,因此一切都取决于路径最初的构建方式。

这是一个实际的例子:

enter image description here

观察我们在绕广场行驶时如何在每个拐角处加速。角点是控制点所在的位置。这是我使用的代码:

    let path = CGPath(rect: CGRect(x: 120, y: 120, width: 100, height: 100), 
        transform: nil)
    let anim = CAKeyframeAnimation(keyPath: #keyPath(CALayer.position))
    anim.duration = 10
    anim.path = path
    anim.calculationMode = .linear
    anim.keyTimes = [0.0, 0.7, 0.9, 0.98, 1.0]
    self.v.layer.add(anim, forKey: nil)

关于ios - 如何在Swift中为遵循BezierPath的imageView的点1到2和点2到3设置不同的animation.duration时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53642596/

相关文章:

ios - Swift版本嵌入外部Framework

ios - 快速从 UILocalNotification 迁移到 UNUserNotification

ios - Disclosure Indicator 不调用 seque 而 Detail Disclosure 调用

ios - 自定义 CAGradientLayer 不设置动画

iOS TableView 错误 : This class is not key value > coding-compliant for the key tableView

arrays - SwiftUI 中的同一个 ForEach 中是否可以有 2 个数组?

ios - 拖放 UICollectionView 单元格重用问题

ios - 如何使用任何月份和周数 swift 3 获取周开始日期和结束日期?

ios - 如何知道 UIView 动画何时全部完成

ios - 复制图层并移动它