我已将以下两种方法连接到我的 UI 中的单独按钮,但注意到在按下“版本 1”按钮后,我无法再次按下该按钮,直到方法中的动画持续时间结束。我的理解是动画使用自己的线程,以免阻塞主应用程序。
// VERSION 1
-(IBAction)fadeUsingBlock {
NSLog(@"V1: Clicked ...");
[myLabel setAlpha:1.0];
[UIView animateWithDuration:1.5 animations:^{
[myLabel setAlpha:0.0];
}];
}
旧样式版本(如下)确实允许在动画计时器结束之前按下按钮,只需重置计时器以重新开始。这两者是否应该相同,我是否遗漏了什么或者在 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];
}
干杯加里
最佳答案
使用 block 动画不会阻塞主线程。我认为您看到的行为是因为默认情况下,用户交互在新的 block 调用中被禁用持续时间动画。您可以通过传递 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];
}
关于iphone - animateWithDuration :animations: block main thread?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3237431/