【问题标题】:Core Animation - Play 2 Animations in Sequence核心动画 - 按顺序播放 2 个动画
【发布时间】:2013-04-04 07:59:20
【问题描述】:

我正在尝试制作一个有点像速度计的动画。

第一个动画得到针

CFTimeInterval localMediaTime = [needle convertTime:CACurrentMediaTime() fromLayer:nil];

CABasicAnimation *needAni = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
needAni.duration = 0.5f;
needAni.fromValue = [NSNumber numberWithFloat:0.0];
needAni.toValue = [NSNumber numberWithFloat:(rotVal * M_PI / 180.0)];
needAni.timingFunction = [CAMediaTimingFunction functionWithControlPoints:0.195: 0.600: 0.790: 0.405];
needAni.fillMode = kCAFillModeBackwards;

[needle.layer addAnimation:needAni forKey:nil];

播放完之后,我想让针在达到全速时来回弹跳一点。这个动画就是这样做的:

CABasicAnimation *needAni2 = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
needAni2.duration = 0.1f;
needAni2.fromValue = [NSNumber numberWithFloat:(rotVal * M_PI / 180.0)];
needAni2.toValue = [NSNumber numberWithFloat:((rotVal+5) * M_PI / 180.0)];
needAni2.fillMode = kCAFillModeBackwards;
needAni2.autoreverses = YES;
needAni2.repeatCount = HUGE_VAL;
needAni2.beginTime = localMediaTime + 0.5f;

[needle.layer addAnimation:needAni2 forKey:nil];

现在,当我把它放在一起时,只会播放第二个动画。

我厌倦了将这两个动画放在一个组中,但我似乎不能只重复第二个动画,它会重复整个过程。如果我将组持续时间设置为 HUGE_VAL 是否会出现性能问题,或者是否有更好的方法来做到这一点?

谢谢

【问题讨论】:

    标签: ios animation


    【解决方案1】:

    我认为你可以坚持你的方法,因为这似乎是 CAGroupAnimation 的预期用途。只需确保为组设置适当的持续时间即可。如果您使组的持续时间短于您的预期动画,它将缩短您的动画。如果您将持续时间设置为比动画长,则动画对象可能会在内存中逗留,直到该持续时间结束。

    另一种选择是实现委托方法 -animationDidStop:finished: ,并在第一个动画调用该方法时添加第二个动画。

    CAAnimation 参考: https://developer.apple.com/library/ios/#documentation/GraphicsImaging/Reference/CAAnimation_class/Introduction/Introduction.html#//apple_ref/occ/cl/CAAnimation

    【讨论】:

    • 也考虑过。我一直在读到这样做是一种不好的形式。这有什么道理吗?
    • 好吧,在这种情况下它应该可以正常工作,但它有点恶心.. 如果没有别的,它不是一种可以很好地扩展到更复杂动画的方法
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多