在UIView动画期间阻止用户输入 [英] Preventing user input during UIView animations

查看:99
本文介绍了在UIView动画期间阻止用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UIView动画(由 touchesEnded:事件调用),我希望在允许更多用户输入之前完成执行。在调用动画之前,我检查标记 isMoving 以查看动画是否在进行中。然后,将 isMoving 设置为 YES 并按以下方式执行动画(是的,我知道它不是块格式的)。 ..

I have a UIView animation (called by a touchesEnded: event) that I want to finish executing before more user input is allowed. Before the animation is called, I inspect a flag "isMoving" to see if the animation is in progress. I then set isMoving to YES and execute the animation as follows (yes, I know it's not in block format)...

- (void) methodCalledFromViewControllerTouchesEnded {
    if (isMoving == YES) {
        NSLog (@"Don't start another animation!");
        return;
    }   
    isMoving = YES;

    ......

    [UIView beginAnimations:@"move" context:NULL];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView setAnimationDuration:0.25f];
    ...
    // DO MOVE STUFF
    ...
    [UIView commitAnimations];

    isMoving = NO;
}

然后我设置 isMoving ,以便随后从 touchesEnded:进行的调用不会在当前动画正在进行时开始另一个动画。但是,如果用户在进行初始动画的过程中不断触摸,则失败和动画将重复执行多次。

I then set isMoving to NO so subsequent calls from touchesEnded: don't start another animation while the current one is in progress. However, this FAILS and the animations are done repeated times if the user keeps touching while the initial animation is in progress.

我想知道这是否

我也以调试模式运行,并注意到动画实际上不是提交,直到控件离开 methodCalledFromViewControllerTouchesEnded 并返回到 touchesEnded:,从而呈现 isMoving = NO; 标志没有用。

I also ran in debug mode and noticed that the animations aren't actually "committed" until control has left methodCalledFromViewControllerTouchesEnded and returned to touchesEnded:, thus rendering the isMoving = NO; flag useless.

在防止重复播放动画方面的任何帮助将不胜感激。

Any help in preventing the repeated animations would be much appreciated.

推荐答案

如果使用UIView的类方法

If you use UIView's class method

+ (void)animateWithDuration:(NSTimeInterval)duration 
                 animations:(void (^)(void))animations

默认情况下,用户交互是禁用的,直到动画结束。这样可以节省很多工作。对于您来说,您想使用完整的方法,以便可以使用EaseIn选项。看起来像这样

user interaction is disabled by default until the animation finishes. It would save you a lot of work. For you, you'd want to use the full method so you could use your EaseIn option. It would look like this

- (void) methodCalledFromViewControllerTouchesEnded {

        [UIView animateWithDuration:0.25f 
                              delay:0.0 
                            options:UIViewAnimationOptionCurveEaseIn 
                         animations:^(void){
                                     // You animations here
                         } 
                         completion: nil];

}

如果按照目前的方式操作,则需要改变一些事情。请记住,动画没有阻塞,因此,当您将isMoving设置为NO时,在动画开始后立即发生,而不是在结束后立即发生。现在,让我重申上面的代码比您的方法要简单得多,但是如果您确实想继续前进,您将需要为动画设置animationStopped完成方法,然后在该方法中将isMoving设置为NO。看起来像这样:

If you did it the way you currently are, you need to change a few things. Keep in mind that the animation is not blocking, so when you set isMoving to NO at the end, that happens IMMEDIATELY after the animation begins, not after it ends. Now, let me reiterate that the above code is much simpler than your method, but if you did want to continue down your path, you will want to set the animationStopped completion method for your animation, and in that method, set isMoving to NO. It would look like this:

isMoving = YES;
[UIView beginAnimations:@"move" context:NULL];
//...
[UIView setAnimationDidStopSelection:@selector(animationDidStop:finished:context:)];
// ... DO MOVE STUFF
//....
[UIView commitAnimations];

然后您将实现选择器,如下所示:

And then you would implement the selector like this:

- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
     isMoving = NO;
}

这篇关于在UIView动画期间阻止用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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