简体   繁体   English

iphone开发:在动画期间禁用用户交互

[英]iphone development: disabling user interaction during the animation

in my app I have some animations. 在我的应用程序中,我有一些动画。 for example I have a button in my main menu and when you click it animation begins (like moving some place etc.) and at the end of the animation it is navigated to an another page. 例如,我在主菜单中有一个按钮,当你点击它时动画开始(比如移动一些地方等),在动画结束时它会导航到另一个页面。 What I need is disabling the user interaction during the animation. 我需要的是在动画期间禁用用户交互。 because during the animation If I press the starting point of my button, the page which is supposed to be navigated is opened twice. 因为在动画期间如果我按下按钮的起点,应该导航的页面打开两次。 To sum up, If I do not let any kind of user interaction during the animation, my problem will be solved. 总而言之,如果我在动画过程中不让任何用户交互,我的问题就会得到解决。 How can I do that? 我怎样才能做到这一点?

Before animation: 动画前:

self.view.userInteractionEnabled = NO;

and in animation completion block: 并在动画完成块中:

self.view.userInteractionEnabled = YES;

This might help: 这可能有所帮助:

// for ignoring event
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
[[UIApplication sharedApplication] endIgnoringInteractionEvents]; 

Code will look like: 代码如下:

[UIView animateWithDuration:1.0 animations:^{
        //some animation
        [[UIApplication sharedApplication] beginIgnoringInteractionEvents];
    }
    completion:^(BOOL done){
        if (done){
            [[UIApplication sharedApplication] endIgnoringInteractionEvents]; 
        }
    }
];

Simple, you can set setUserInteractionEnabled to NO before the animation starts, and in the animations completion handler set it back to YES . 很简单,您可以在动画开始之前将setUserInteractionEnabled设置为NO ,并在动画完成处理程序中将其设置为YES

[myObject setUserInteractionEnabled:NO];
[UIView animateWithDuration:1.0 animations:^{
    [myObject setTransform:CGAffineTransformMakeTranslation(100, 100)];//some animation
}completion:^(BOOL done){
    if (done){
        [myObject setUserInteractionEnabled:YES];
    }
}];

You don't have to hack around with the completion block - there's an animation option which does just this exactly: 你不必破解完成块 - 有一个动画选项可以完全做到这一点:

[UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction
    animations:^{
        // animations here
    }
    completion:nil];

If you had set the UIViewAnimationOptionAllowUserInteraction , then user interaction would have been allowed. 如果您已设置UIViewAnimationOptionAllowUserInteraction ,则允许用户交互。

yourView.userInteractionEnabled = NO;
[UIView animateWithDuration:1 animations:^
{
    //animations here                    
}
completion:^(BOOL finished)
{
    yourView.userInteractionEnabled = YES;
}];

To disable touch event in a view, 要在视图中禁用触摸事件,

 [[UIApplication sharedApplication] beginIgnoringInteractionEvents];

To enable touch event in a view 在视图中启用触摸事件

[[UIApplication sharedApplication] endIgnoringInteractionEvents];

禁用Button的userIntrection。

Btn.userInteractionEnabled = NO;

I had view controller with icons that open pages. 我有视图控制器与图标打开页面。 If the user was tapping quickly icon1 and icon2, the 2 pages opened. 如果用户快速点击icon1和icon2,则打开2页。

to prevent that I had this 2 lines to the beginning of the tap event this make sure the whatever happen, the endIgnoring will call 为了防止我在tap事件的开头有这两行,这确保无论发生什么,endIgnoring都会调用

-(void) on_image_tap:(UITapGestureRecognizer * ) tapGesture
{
    [[UIApplication sharedApplication] beginIgnoringInteractionEvents];
    [[UIApplication sharedApplication] performSelector:@selector(endIgnoringInteractionEvents) withObject:nil afterDelay:0.5f];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM