如何在animationDidStop委托中识别CAAnimation? [英] How to identify CAAnimation within the animationDidStop delegate?

查看:109
本文介绍了如何在animationDidStop委托中识别CAAnimation?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,我有一系列重叠的CATransition / CAAnimation序列,所有这些都是我需要在动画停止时执行自定义操作,但我只需要一个animationDidStop的委托处理程序。

I had a problem where I had a series of overlapping CATransition / CAAnimation sequences, all of which I needed to perform custom operations when the animations stopped, but I only wanted one delegate handler for animationDidStop.

但是,我遇到了问题,似乎没有办法在animationDidStop委托中唯一标识每个CATransition / CAAnimation。

However, I had a problem, there didn't appear to be a way to uniquely identify each CATransition / CAAnimation in the animationDidStop delegate.

我通过作为CAAnimation的一部分公开的键/值系统解决了这个问题。

I solved this problem via the key / value system exposed as part of CAAnimation.

当你开始动画时,使用CATransition / CAAnimation上的setValue方法来设置你的标识符和animationDidStop触发时使用的值:

When you start your animation use the setValue method on the CATransition / CAAnimation to set your identifiers and values to use when animationDidStop fires:

-(void)volumeControlFadeToOrange
{   
    CATransition* volumeControlAnimation = [CATransition animation];
    [volumeControlAnimation setType:kCATransitionFade];
    [volumeControlAnimation setSubtype:kCATransitionFromTop];
    [volumeControlAnimation setDelegate:self];
    [volumeControlLevel setBackgroundImage:[UIImage imageNamed:@"SpecialVolume1.png"] forState:UIControlStateNormal];
    volumeControlLevel.enabled = true;
    [volumeControlAnimation setDuration:0.7];
    [volumeControlAnimation setValue:@"Special1" forKey:@"MyAnimationType"];
    [[volumeControlLevel layer] addAnimation:volumeControlAnimation forKey:nil];    
}

- (void)throbUp
{
    doThrobUp = true;

    CATransition *animation = [CATransition animation]; 
    [animation setType:kCATransitionFade];
    [animation setSubtype:kCATransitionFromTop];
    [animation setDelegate:self];
    [hearingAidHalo setBackgroundImage:[UIImage imageNamed:@"m13_grayglow.png"] forState:UIControlStateNormal];
    [animation setDuration:2.0];
    [animation setValue:@"Throb" forKey:@"MyAnimationType"];
    [[hearingAidHalo layer] addAnimation:animation forKey:nil];
}

在你的animationDidStop委托中:

In your animationDidStop delegate:

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

    NSString* value = [theAnimation valueForKey:@"MyAnimationType"];
    if ([value isEqualToString:@"Throb"])
    {
       //... Your code here ...
       return;
    }


    if ([value isEqualToString:@"Special1"])
    {
       //... Your code here ...
       return;
    }

    //Add any future keyed animation operations when the animations are stopped.
 }

这方面的另一个方面是它允许你在密钥中保持状态值配对系统,而不是必须将其存储在您的委托类中。代码越少越好。

The other aspect of this is that it allows you to keep state in the key value pairing system instead of having to store it in your delegate class. The less code, the better.

请务必查看关键值对编码的Apple参考

是否有更好的CAAnimation / CATransition技术animationDidStop委托中的识别?

Are there better techniques for CAAnimation / CATransition identification in the animationDidStop delegate?

谢谢,
- 巴特

Thanks, --Batgar

推荐答案

Batgar的技术太复杂了。为什么不利用addAnimation中的forKey参数?它的目的是为了这个目的。只需取出对setValue的调用并将键字符串移动到addAnimation调用即可。例如:

Batgar's technique is too complicated. Why not take advantage of the forKey parameter in addAnimation? It was intended for this very purpose. Just take out the call to setValue and move the key string to the addAnimation call. For example:

[[hearingAidHalo layer] addAnimation:animation forKey:@"Throb"];

然后,在animationDidStop回调中,您可以执行以下操作:

Then, in your animationDidStop callback, you can do something like:

if (theAnimation == [[hearingAidHalo layer] animationForKey:@"Throb"]) ...

这篇关于如何在animationDidStop委托中识别CAAnimation?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆