iphone - animateWithDuration :animations: block main thread?

标签 iphone objective-c cocoa-touch animation multithreading

我已将以下两种方法连接到我的 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/

相关文章:

iphone - UIActivityIndi​​catorView 未对齐

iphone - 使用 '$' 的 Objective C 宏库

iOS navigationBar设置透明无效果

ios - 如何调整 UiLabel 的基线偏移?

iphone - 在 iPhone 键盘中添加符号

iphone - 定义屏幕宽度常量

ios - Tableview 与自定义 UIView ,添加标签重叠

iphone - 获取 UITableViewCell 上的 UITextField 的引用?

objective-c - 用于 iPad 应用程序的控件/设计模式,该应用程序具有通向详细 View 的两级表

cocoa-touch - 运行时 xib 下载?