2

嗨有一个简单的核心动画:

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。

谢谢大家。

4

2 回答 2

2

根据CAMediaTiming 协议的文档repeatCountrepeatDuration应该同时设置,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;

计算有点混乱,但相当简单。

于 2013-10-23T23:28:23.330 回答
-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
于 2016-07-27T04:08:24.233 回答