Iphone 重复之前的关键帧初始化延迟
我有一个Iphone 重复之前的关键帧初始化延迟,iphone,ios,core-animation,core-graphics,Iphone,Ios,Core Animation,Core Graphics,我有一个CAKeyframeAnimation动画,我想用repeatCount=mage_VALF永远重复它。动画的持续时间为2秒,但我希望在每个循环之前暂停3秒 我能想到的唯一两种方法是: 使整个动画持续5秒,并添加额外的关键点时间和值,以便在5s动画的最后3秒中获得我想要的暂停。这感觉有点不舒服 让动画只重复一次,然后添加使用类似performSelector:afterDelay:2的方法再次运行动画,依此类推。这感觉也很脏。这也意味着我需要每隔5秒调用addAnimation:,我不确
CAKeyframeAnimation
动画,我想用repeatCount=mage_VALF
永远重复它。动画的持续时间为2秒,但我希望在每个循环之前暂停3秒
我能想到的唯一两种方法是:
performSelector:afterDelay:2
的方法再次运行动画,依此类推。这感觉也很脏。这也意味着我需要每隔5秒调用addAnimation:
,我不确定这在性能方面是否最佳有没有其他我可能错过的选择?这两种方法中有一种比另一种好吗?通过转储苹果的
MKUserLocationView
动画,我可以看到他们是如何做到的。原来这就是CAAnimationGroup
的目的。通过将2秒动画封装到5秒动画组中,您将得到2秒动画,然后是3秒延迟:
CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
animationGroup.duration = 5;
animationGroup.repeatCount = INFINITY;
CAMediaTimingFunction *easeOut = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
CABasicAnimation *pulseAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale.xy"];
pulseAnimation.fromValue = @0.0;
pulseAnimation.toValue = @1.0;
pulseAnimation.duration = 2;
pulseAnimation.timingFunction = easeOut;
animationGroup.animations = @[pulseAnimation];
[ringImageView.layer addAnimation:animationGroup forKey:@"pulse"];
samvermette在Swift 3中的回答:
let animationGroup = CAAnimationGroup()
animationGroup.duration = 5;
animationGroup.repeatCount = .infinity
let easeOut = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)
let pulseAnimation = CABasicAnimation(keyPath: "transform.scale.xy")
pulseAnimation.fromValue = 0
pulseAnimation.toValue = 1.0
pulseAnimation.duration = 2
pulseAnimation.timingFunction = easeOut
animationGroup.animations = [pulseAnimation]
ringImageView.layer.add(animationGroup, forKey: "pulse")
在Swift 4.2上测试 只需实现以下扩展,就可以在任何UI组件上获得漂亮的pulse动画
//MARK:- Pulse animation
extension UIView {
func applyPulse(_ apply: Bool = true) {
if apply {
let animation = CABasicAnimation(keyPath: "transform.scale")
animation.toValue = 1.5
animation.duration = 0.8
animation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeOut)
animation.autoreverses = true
animation.repeatCount = Float.infinity
self.layer.add(animation, forKey: "pulsing")
} else {
self.layer.removeAnimation(forKey: "pulsing")
}
}
}
用途:
聪明的解决方案。又好又干净。你应该接受你自己的答案哇。。谢谢你,先生。。到目前为止一直在完美工作……)