animateWithDuration:animations:block主线程? [英] does animateWithDuration:animations: block main thread?

查看:908
本文介绍了animateWithDuration:animations:block主线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经连接下面的两个方法分开按钮在我的用户界面,但已经注意到,按下VERSION 1按钮后,我不能再次按下按钮,直到方法中的动画持续时间结束。我的理解是,动画使用自己的线程,以免阻塞主应用程序。

I have connected the two methods below to separate buttons in my UI but have noticed that after pressing the "VERSION 1" button that I could not press the button again until the animation duration within the method had ended. My understanding was that the animation uses its own thread so as not to block the main application.

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

旧式样(下面)允许按钮在动画定时器结束,只需重置定时器再次启动。如果这两个工作方式相同,我是否缺少一些东西,或者在3.2和4之间的操作是否有变化?

The older style version (below) does allow the button to be repressed before the animation timer ends, simply resetting the timer to start again. Should these both work the same, am I missing something or has there been a change in operation between 3.2 and 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];
}

Cheers gary

Cheers gary

推荐答案

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

Animating with blocks doesn't block the main thread. I think the behavior you're seeing is because, by default, user interaction is disabled duration animation with the new block calls. You can override this by passing UIViewAnimationOptionAllowUserInteraction (calling animationWithDuration:delay:options:animations:completion), like this:

-(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:animations:block主线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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