CAKeyframeAnimation和音频同步

时间:2011-09-26 14:49:59

标签: objective-c cakeyframeanimation

我正在使用CAKeyframeAnimation在我的应用中执行一些动画。我想播放一个音频文件(有时在播放期间,有时在播放期间)以及动画。我怎样才能做到这一点?是否有针对CAKeyframeAnimation的AnimationDidStopSelector,还是有另一种方法可以做到这一点?

UIBezierPath *movePath = [UIBezierPath bezierPath];
            [movePath moveToPoint:imageAnimation.center];
            [movePath addQuadCurveToPoint:CGPointMake(839, 339)
                             controlPoint:CGPointMake(671, 316)];

            CAKeyframeAnimation *moveAnim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
            moveAnim.path = movePath.CGPath;
            CAAnimationGroup *animGroup = [CAAnimationGroup animation];
            animGroup.animations = [NSArray arrayWithObjects:moveAnim, nil];
            animGroup.duration = 0.5;
            imageAnimation.layer.position = CGPointMake(839, 339);
            imageAnimation.tag = 0;
            [imageAnimation.layer addAnimation:animGroup forKey:nil];
            break;

1 个答案:

答案 0 :(得分:0)

实际上有。首先将自己指定为代表:

animGroup.delegate = self

如果您想使用不同的动画,可以使用KVC来区分它们:

[yourAnimation setValue:@"firstAnimation" forKey:@"animationName"];

然后实现此方法

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
{
    if (flag && [[anim valueForKey:@"animationName"] isEqualToString:@"firstAnimation"]) {
        //play your sound
    }
    else if (flag && [[anim valueForKey:@"animationName"] isEqualToString:@"secondAnimation"]) {
        //do something else
    }
}