Ios 卡巴斯基化自动翻转速度提高一倍
我使用此代码添加带有自动翻转的脉冲圆:Ios 卡巴斯基化自动翻转速度提高一倍,ios,swift,animation,core-animation,cabasicanimation,Ios,Swift,Animation,Core Animation,Cabasicanimation,我使用此代码添加带有自动翻转的脉冲圆: let scaleAnimation = CABasicAnimation(keyPath: "transform.scale") scaleAnimation.duration = 6 scaleAnimation.repeatCount = 200 scaleAnimation.autoreverses = true scaleAnimation.fromValue = 0.1 scaleAnimation.toValue = 0.8
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")
我想在这里做的是:
以2x速度运行动画fromValue=0.1
toValue=0.8
,
然后以1x速度向后设置动画从value=0.8
到value=0.1
有没有一个简单的方法来实现这一点 有两种方法可以做到这一点:
(您的最佳选择):
专为使用多个关键帧设置单个关键路径的动画而设计,每个间隔上都有自定义的时间功能。正是你需要的
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)
(一种变通方法)
设计用于为单个层分组动画(可能使用不同的keyPath
s)
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)
您是否尝试过cakeyLeanimation
?谢谢!帮了我很多忙。我用CAAnimationGroup
来解决我的问题。顺便问一下,有没有办法在每次动画开始时获得回调?我尝试使用CAAnimationDelegate
,但它只为组本身提供启动/停止回调,而不提供组内的动画。