CAKeyframe动画和音频同步

发布于 2024-12-06 20:02:19 字数 946 浏览 2 评论 0原文

我正在使用 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;

I am using CAKeyframeAnimation to perform few animations in my app. I would like to play an audio file (sometimes during and sometimes after) along with the animations. How can I achieve this? Is there a AnimationDidStopSelector for CAKeyframeAnimation or is there another approach for doing this?

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吃素的狼 2024-12-13 20:02:19

其实是有的。首先将自己指定为委托:

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
    }
}

Actually there is. First assign yourself as delegate:

animGroup.delegate = self

If you want to use different animations you can use KVC to differentiate between them:

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

Then implement this method

- (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
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文