如何在触发器上触发CALayer的动画组?

时间:2014-03-27 16:06:12

标签: ios

我在CALayer上有一个动画,它无限期地重复,但我想改变它,以便动画由某个定时事件触发。

我可以通过在触发器发生时删除并重新添加动画事件来使其工作但是这看起来有点麻烦,是否有其他方法而不必不断地一直移除和添加动画?

这是草图形式的代码:

- (void) init
{
    ….
    CALayer *sublayer = …
    [self.layer addSublayer:sublayer];
    [self createAnimationGroup];
    [sublayer addAnimation: self.animationGroup forKey:@"MyKey"];
}

- (void) createAnimationGroup
{
 CAMediaTimingFunction *defaultCurve = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault];
    self.animationGroup = [CAAnimationGroup animation];
    self.animationGroup.repeatCount = 0.0f; // was previously INFINITY;
    <snip>
   NSArray *animations = @[scaleAnimation, opacityAnimation];
    self.animationGroup.animations = animations;
}


- (void) onTrigger
{
    [self.layer.sublayers[0] removeAnimationForKey:@"MyKey"];
    [self.layer.sublayers[0] addAnimation:self.animationGroup forKey:@"MyKey"];
}

我的问题是我实现onTrigger的方式确定,它有效,但有没有一种直接触发动画的方法,而不是间接通过删除和添加动画?