AnimationDidStop 方法的多个 CAAnimations?

发布于 2024-12-10 04:22:40 字数 248 浏览 0 评论 0原文

我知道您必须使用此方法来获取动画完成时的委托方法:

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag {

问题是,我如何区分多个 CAAnimations(例如 2 个或更多)?

我用谷歌搜索了这个,但没有发现任何有用的东西。

请与我分享您是如何实现这一目标的!

谢谢!

I know you have to use this method to get the delegate method for when the animation has finished:

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag {

The problem is, how would I distinguish between multiple CAAnimations like 2 or more?

I googled this and I haven't found anything useful.

Please share with me on how you accomplished this!

Thanks!

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

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

发布评论

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

评论(2

掩耳倾听 2024-12-17 04:22:40

您可以为 CAAnimation 实例设置键/值对象,如下所示:

CABasicAnimation *theAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
[theAnimation setValue:@"animation1" forKey:@"id"]; 
theAnimation.delegate = self;

CABasicAnimation *theAnimation2 = [CABasicAnimation animationWithKeyPath:@"opacity"];
[theAnimation2 setValue:@"animation2" forKey:@"id"];    
theAnimation2.delegate = self;

检查委托方法中调用了哪一个:

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    if([[anim valueForKey:@"id"] isEqual:@"animation1"]) {
        NSLog(@"animation1");
    }
    if([[anim valueForKey:@"id"] isEqual:@"animation2"]) {
        NSLog(@"animation2");
    }
}

You can set key/value objects for CAAnimation instance like this:

CABasicAnimation *theAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
[theAnimation setValue:@"animation1" forKey:@"id"]; 
theAnimation.delegate = self;

CABasicAnimation *theAnimation2 = [CABasicAnimation animationWithKeyPath:@"opacity"];
[theAnimation2 setValue:@"animation2" forKey:@"id"];    
theAnimation2.delegate = self;

Check which one was called in delegate method:

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    if([[anim valueForKey:@"id"] isEqual:@"animation1"]) {
        NSLog(@"animation1");
    }
    if([[anim valueForKey:@"id"] isEqual:@"animation2"]) {
        NSLog(@"animation2");
    }
}
国际总奸 2024-12-17 04:22:40

CAAnimation 对象应该不时地被重用,这就是为什么我不喜欢给它一个特定的键(因为它不是唯一的)。它的独特之处在于与 CALayer 和 addAnimation:forKey: 的关联。因此,我在 animationDidStop 中使用以下代码:

if animation == layer.animationForKey(AnimationKeys.scaleUp) {
   // scaleUp animation has completed
}

A CAAnimation object is supposed to be reused from time to time and that's why I don't like to give it a certain key (since it's not unique). What makes it unique is the association with a CALayer with addAnimation:forKey:. For this reason I use the following code in animationDidStop:

if animation == layer.animationForKey(AnimationKeys.scaleUp) {
   // scaleUp animation has completed
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文