ios 如何延迟 CABasicAnimation?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15116712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to delay a CABasicAnimation?
提问by openfrog
I have a CABasicAnimation
and want to start it after a delay. In UIKit I can specify delays. The CAMediaTiming
protocol has a timeOffset
property but I can't see an effect. My next try is to use GCD to delay it but it feels like overkill.
我有一个CABasicAnimation
并想在延迟后启动它。在 UIKit 中,我可以指定延迟。该CAMediaTiming
协议有一个timeOffset
属性,但我看不到效果。我的下一个尝试是使用 GCD 来延迟它,但感觉有点矫枉过正。
回答by trojanfoe
Shouldn't you be using the [CAMediaTiming beginTime]
property (reference)?
您不应该使用该[CAMediaTiming beginTime]
属性(参考)吗?
See Customizing the Timing of an Animationin the Core Animation Programming Guide.
请参阅核心动画编程指南中的自定义动画时间。
CABasicAnimation *animation;
animation.beginTime = CACurrentMediaTime() + 0.3; //0.3 seconds delay
回答by Dave G
In Swift 3.0:
在 Swift 3.0 中:
func animateYourView () {
let myDelay = 5.0
let scalePulseAnimation: CABasicAnimation = CABasicAnimation(keyPath: "transform.scale")
scalePulseAnimation.beginTime = CACurrentMediaTime() + myDelay
scalePulseAnimation.duration = 0.5
scalePulseAnimation.repeatCount = 2.0
scalePulseAnimation.autoreverses = true
scalePulseAnimation.fromValue = 1.0
scalePulseAnimation.toValue = 0.5
myView.layer.add(scalePulseAnimation, forKey: "scale")
}
Where the key line for the delay is:
延迟的关键在于:
scalePulseAnimation.beginTime = CACurrentMediaTime() + myDelay