Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/48.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 在第一次结束动画后触发其他动画(Objective-C)_Iphone_Objective C_Core Animation - Fatal编程技术网

Iphone 在第一次结束动画后触发其他动画(Objective-C)

Iphone 在第一次结束动画后触发其他动画(Objective-C),iphone,objective-c,core-animation,Iphone,Objective C,Core Animation,我有一个简单的动画,可以简单地修改按钮的位置: [UIView beginAnimation:nil context:nil]; [UIView setAnimationsDuration:3.0]; [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; mybutton.frame = CGREctMake(20, 233, 280, 46); [UIView commitAnimations]; 我想在这个动画完成后执行一些其他

我有一个简单的动画,可以简单地修改按钮的位置:

[UIView beginAnimation:nil context:nil];
[UIView setAnimationsDuration:3.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
mybutton.frame = CGREctMake(20, 233, 280, 46);
[UIView commitAnimations];

我想在这个动画完成后执行一些其他动画,如何执行?

使用+[UIVIew setAnimationDelegate:]和+[UIVIew setAnimationDidStopSelector:]方法将类配置为在动画结束时接收消息。

您可以查看。所有动画都在各自的线程中运行,因此
[UIView commitAnimations]
是一个非阻塞调用。因此,如果您在提交第一个动画后立即开始另一个动画,在适当设置动画是否从当前状态开始后,我认为您将获得所需的行为。也就是说:

[UIView beginAnimation:nil context:nil];
// set up the first animation
[UIView commitAnimations];

[UIView beginAnimation:nil context:nil];
[UIView setAnimationBeginsFromCurrentState:NO];
// set up the second animation
[UIView commitAnimations];
或者,您可以通过执行以下操作来提供回调

[UIView beginAnimation:nil context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)
// set up the first animation
[UIView commitAnimations];

//...

- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
    // set up the second animation here
}

用于入门的代码段。。设置初始(第一个)动画:

- (void) aniPath:(AnimationPath *) path
{   
    [UIView beginAnimations:path->aniname context:path];
    [UIView setAnimationDuration:path->interval];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(aniDone:finished:context:)];

    // your initial animation

    [UIView commitAnimations];
}
第一个动画完成后链接到另一个动画:

- (void) aniDone:(NSString *) aniname finished:(BOOL) finished context:(void *) context
{
    AnimationPath *path = (AnimationPath *) context;
    if ([aniname isEqualToString:path->aniname]) {
        [UIView beginAnimations:path->aniname context:context];
        [UIView setAnimationDuration:path->interval];
        [UIView setAnimationDelegate:self];
        [UIView setAnimationDidStopSelector:@selector(aniDone:finished:context:)];

        // more animations, even recursively 

        [UIView commitAnimations];          
    }
}
这相当容易

你可以

- (void)animationDidContiune:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
    UIView *currentObj = context;
    [self callTheAnimation: currentObj];
}
可以轻松地循环动画

为了结束它,我将创建一个animationdiddend并使用它而不是animationDidContiune 一旦您想要停止它(就像在动画中选择继续或结束的简单if)

高频

重要的是:

使用以下类型的动画制作方法:

- (void)callTheAnimation:(UIView*)itemView
{

    [UIView beginAnimations:@"animation" context:itemView];
        .
        .
        .
}

它们非常灵活-由于对象的层次结构,您可以在此处设置任何动画。

什么是
AnimationPath
,为什么要在其上使用
->
操作符?如果AnimationPath是Objective-C类,则绝对不应该使用
->
。除非你有很好的理由,否则你不应该从课堂之外访问ivar。您应该创建访问器方法,或者使用Obj-C属性,并使用这些属性访问
aniname
interval
。AnimationPath是我自己的结构,用于协调动画路径。您可以安全地忽略它,或者用自己的数据结构替换它。