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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 22:28:52  来源:igfitidea点击:

How to delay a CABasicAnimation?

iosuikitcore-animationcabasicanimation

提问by openfrog

I have a CABasicAnimationand want to start it after a delay. In UIKit I can specify delays. The CAMediaTimingprotocol has a timeOffsetproperty 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