【问题标题】:CABasicAnimation autoreverse twice as fastCABasicAnimation 自动反转快两倍
【发布时间】:2016-10-28 17:07:13
【问题描述】:

我正在使用此代码通过自动反转添加脉冲圆:

let scaleAnimation = CABasicAnimation(keyPath: "transform.scale")        
scaleAnimation.duration = 6
scaleAnimation.repeatCount = 200
scaleAnimation.autoreverses = true
scaleAnimation.fromValue = 0.1
scaleAnimation.toValue = 0.8
scaleAnimation.timingFunction = CAMediaTimingFunction(controlPoints: 0.42, 0.0, 0.58, 1.0)
animationView.layer.add(scaleAnimation, forKey: "scale")

我想在这里做的是:

运行动画fromValue = 0.1toValue = 0.82x speed, 并在fromValue = 0.8 toValue = 0.1 1x speed 上向后设置动画。

有没有简单的方法来实现这一点?

【问题讨论】:

  • 你试过CAKeyframeAnimation吗?

标签: ios swift animation core-animation cabasicanimation


【解决方案1】:

您有两种方法:

CAKeyframeAnimation(您的最佳选择):

专门设计用于为具有多个关键帧的单个keyPath 设置动画,并在每个间隔上使用自定义时间函数。正是你所需要的

let scaleAnimation = CAKeyframeAnimation(keyPath: "transform.scale")
scaleAnimation.duration = 18 // 6 seconds for the first part, 12 for the second
scaleAnimation.repeatCount = 200
scaleAnimation.values = [0.1, 0.8, 0.1] // make sure first and last values are equal in order to get seamless animation
scaleAnimation.keyTimes = [0, 0.333, 1] // keyframes scaled to [0; 1] interval
scaleAnimation.timingFunctions = [
    CAMediaTimingFunction(controlPoints: 0.42, 0.0, 0.58, 1.0), //first interval
    CAMediaTimingFunction(controlPoints: 0.58, 0.0, 0.42, 1.0)  //second interval (reversed)
]
layer.add(scaleAnimation, forKey: nil)

CAAnimationGroup(一种解决方法)

旨在为单个图层组合动画(可能具有不同的keyPaths)

let scaleUpAnimation = CAKeyframeAnimation(keyPath: "transform.scale")
//setup first animation as you did

let scaleDownAnimation = CAKeyframeAnimation(keyPath: "transform.scale")
//setup second animation

let groupAnimation = CAAnimationGroup()
groupAnimation.animations = [scaleUpAnimation, scaleDownAnimation]
//setup group if needed
layer.add(groupAnimation, forKey: nil)

【讨论】:

  • 谢谢!帮了我很多。我使用CAAnimationGroup 来解决我的问题。顺便说一句,有没有办法在每个动画开始时获得回调?我试过CAAnimationDelegate,但它只为组本身提供启动/停止回调,而不是组内的动画。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-17
  • 1970-01-01
  • 2014-05-19
  • 1970-01-01
相关资源
最近更新 更多