2013-02-27 97 views
35

我有一個CABasicAnimation,並希望延遲後啓動它。在UIKit中,我可以指定延遲。 CAMediaTiming協議有一個timeOffset屬性,但我看不到效果。我的下一個嘗試是使用GCD來延遲它,但它感覺像是過度殺傷。如何延遲CABasicAnimation?

+0

我只想用GCD。 'dispatch_after(popTime,dispatch_get_main_queue(),^(void){#code在延遲#後要在主隊列上執行#> });' – BooRanger 2013-02-27 16:22:15

+1

@BooRanger beginTime屬性有什麼問題? – 2013-02-27 16:32:10

+0

[This answer](http://stackoverflow.com/a/11625742/608157)應該回答你的問題 – 2013-02-27 16:36:12

回答

77

您是不是應該使用[CAMediaTiming beginTime]屬性(reference)?

請參閱Core Animation Programming Guide中定製動畫的時機

CABasicAnimation *animation; 
animation.beginTime = CACurrentMediaTime() + 0.3; //0.3 seconds 
+70

只要記住它是'CACurrentMediaTime()+ yourDelay' – 2013-02-27 17:01:44

+1

我無法將其用於AVVideoCompositionCoreAnimationTool。唯一可行的是AVCoreAnimationBeginTimeAtZero。我想爲我的動畫做一些延遲... – Nil 2014-11-13 05:24:43

+0

@grisleyB,我面臨的同樣的問題。你有沒有找到什麼? – 2015-06-24 13:01:23

10

在雨燕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