61

我有一个CABasicAnimation并且想在延迟后启动它。在 UIKit 我可以指定延迟。该CAMediaTiming协议有一个timeOffset属性,但我看不到效果。我的下一个尝试是使用 GCD 来延迟它,但感觉有点矫枉过正。

4

2 回答 2

155

您不应该使用该[CAMediaTiming beginTime]属性(参考)吗?

请参阅Core Animation Programming Guide中的自定义动画时序

CABasicAnimation *animation;
animation.beginTime = CACurrentMediaTime() + 0.3; //0.3 seconds delay
于 2013-02-27T16:21:56.403 回答
22

在 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")
}

延迟的关键线是:

  scalePulseAnimation.beginTime = CACurrentMediaTime() + myDelay
于 2016-11-16T00:23:45.950 回答