2013-10-23 82 views
2

喜有一個簡單的核心動畫之前:延遲重複CAAnimation

NSString *keyPath2 = @"anchorPoint.y"; 
CAKeyframeAnimation *kfa2 = [CAKeyframeAnimation animationWithKeyPath:keyPath2]; 
[kfa2 setValues:[NSArray arrayWithObjects: 
       [NSNumber numberWithFloat:-.05], 
       [NSNumber numberWithFloat:.1], 
       [NSNumber numberWithFloat:-.1], 
       [NSNumber numberWithFloat:.1], 
       [NSNumber numberWithFloat:-.05], 
       nil]]; 
//[kfa2 setRepeatCount:10]; 
[kfa2 setRepeatDuration:30]; 
[kfa2 setDuration:.35]; 
[kfa2 setAdditive:YES]; 
[kfa2 setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]]; 

我怎麼可以設置動畫之前的延遲得到重複?

如果有人可以解釋repeatCount和repeatDuration之間的區別。

我不想使用@selector。

謝謝大家。

回答

1

CAMediaTiming protocol's documentationrepeatCountrepeatDuration應該同時設置,repeatCount意味着這意味着什麼,repeatDuration只是另一種方式來設置repeatCount,即repeatCount = repeatDuration/duration

您可以通過添加最後一個附加值來模擬CAKeyframeAnimation的延遲。例如,您有以下動畫

kfa.values = @[@1, @3, @7]; // Using object literal syntax (Google it!), the way to go 
kfa.keyTimes = @[@.0, @.5, @1]; // 0.5 = 5/10; 1 = 10/10; 
kfa.duration = 10; // 10 sec, for demonstration purpose 

現在您希望它在重複之前延遲1秒。只需將其改爲:

kfa.values = @[@1, @3, @7, @7]; // An additional value 
kfa.keyTimes = @[@.0, @.4546, @.9091, @1]; // 0.4546 = 5/11; 0.9091 = 10/11; 1 = 11/11 
kfa.duration = 11; 

該計算有點麻煩,但相當簡單。

+0

謝謝Khanh!完美我會試試這個! –

-1

您也可以使用CAAnimationGroup。

let scale   = CABasicAnimation(keyPath: "transform.scale") // or CAKeyFrameAnimation 
scale.toValue = 2.0 
scale.fillMode = kCAFillModeForwards 
scale.duration = 0.4 
scale.beginTime = 1.0 // Repeat delay. 
let scaleGroup = CAAnimationGroup() 
scaleGroup.duration = scale.duration + scale.beginTime 
scaleGroup.fillMode = kCAFillModeForwards 
scaleGroup.repeatCount = Float.infinity 
scaleGroup.animations = [scale] 
scaleGroup.beginTime = CACurrentMediaTime() + 0.8 // Initial delay. 

let view = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 200)) 
view.backgroundColor = UIColor.orange 

let viewb = UIView(frame: CGRect(x: 50, y: 50, width: 100, height: 100)) 
viewb.backgroundColor = UIColor.blue 
view.addSubview(viewb) 

viewb.layer.add(scaleGroup, forKey: "") 

// If you want to try this on Playground 
PlaygroundPage.current.liveView = view 
+0

scale.beginTime表示僅在第一個循環之前的延遲,但不是在每個動畫循環之前。 – eXhausted

+0

@eXhausted你可以再試一次示例代碼嗎? 我修改了示例代碼並在Xcode8.2.1 Playground上試了一下,我可以確認應用於每個動畫循環的scale.beginTime。 – Tueno