做animateWithDuration:animation:块主线程?

我已经连接下面的两个方法来分离我的用户界面button,但注意到按下“版本1”button后,我不能再次按下button,直到方法内的animation持续时间结束。 我的理解是animation使用自己的线程,以免阻塞主应用程序。

// VERSION 1 -(IBAction)fadeUsingBlock { NSLog(@"V1: Clicked ..."); [myLabel setAlpha:1.0]; [UIView animateWithDuration:1.5 animations:^{ [myLabel setAlpha:0.0]; }]; } 

旧式版本(如下)确实允许button在animation计时器结束之前被压缩,只需重新设置计时器即可重新开始。 如果这两者都是一样的,我是否错过了一些东西,或者在3.2到4之间有什么变化?

 // VERSION 2 -(IBAction)fadeUsingOld { NSLog(@"V2: Clicked ..."); [myLabel setAlpha:1.0]; [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:1.5]; [myLabel setAlpha:0.0]; [UIView commitAnimations]; } 

干杯加里

用块animation不会阻塞主线程。 我认为你看到的行为是因为,默认情况下,用户交互是用新的块调用禁用持续时间animation。 您可以通过传递UIViewAnimationOptionAllowUserInteraction(调用animationWithDuration:delay:options:animations:completion )来覆盖它,如下所示:

 -(IBAction) fadeUsingBlock { NSLog(@"V1: Clicked ..."); [myLabel setAlpha:1.0]; [UIView animateWithDuration:1.5 delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:^{ [myLabel setAlpha:0.0]; } completion:nil]; } 

对于animateWithDuration:,类的引用没有提到线程,所以我不确定。

对于beginAnimations:context: and commitAnimation:是的,他们运行在一个单独的线程UIView类参考 。

对视图对象的一些属性更改可以是animation的,例如,设置框架,边界,中心和变换属性。 如果在animation块中更改这些属性,则会从当前状态变为新状态。 调用beginAnimations:context:class方法来开始一个animation块,设置你想要animation的属性,然后调用commitAnimations类的方法来结束一个animation块。 animation在单独的线程中运行,并在应用程序返回到运行循环时开始。 其他animation类方法允许您控制块内animation的开始时间,持续时间,延迟和曲线。