11

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?

death7eater
  • 1,094
  • 2
  • 14
  • 35

8 Answers8

22

Before animation:

self.view.userInteractionEnabled = NO;

and in animation completion block:

self.view.userInteractionEnabled = YES;
swebal
  • 431
  • 3
  • 14
  • if user have two button on view then wht..? – Rajneesh071 Sep 06 '12 at 15:28
  • 1
    disabling the view will disable all subviews – swebal Sep 07 '12 at 07:10
  • Yup you are right....but i want to say that if user have two button and want some animation on both button simultaneously ...then how can it possible according to your code.... so i suggest to disable the button is better approach.... – Rajneesh071 Sep 07 '12 at 07:40
  • 1
    Be aware that if you choose this option, your gestures might be handled by any views that lie below the current view. – AWrightIV Sep 11 '14 at 22:30
  • According to https://developer.apple.com/reference/uikit/uiview/1622577-userinteractionenabled "During an animation, user interactions are temporarily disabled for all views involved in the animation, regardless of the value in this property." So this should be unnecessary (but I have the same problem). – wcochran Apr 14 '17 at 16:16
18

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]; 
        }
    }
];
OGHaza
  • 4,795
  • 7
  • 23
  • 29
Mangesh
  • 2,257
  • 4
  • 24
  • 51
6

Simple, you can set setUserInteractionEnabled to NO before the animation starts, and in the animations completion handler set it back to YES.

[myObject setUserInteractionEnabled:NO];
[UIView animateWithDuration:1.0 animations:^{
    [myObject setTransform:CGAffineTransformMakeTranslation(100, 100)];//some animation
}completion:^(BOOL done){
    if (done){
        [myObject setUserInteractionEnabled:YES];
    }
}];
Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
5

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.

Nikita Took
  • 3,980
  • 25
  • 33
  • 3
    The question was how to disable user interacton for everything during the animation. This animation options enables user interaction for the view being animated. – AWrightIV Sep 11 '14 at 22:35
2
yourView.userInteractionEnabled = NO;
[UIView animateWithDuration:1 animations:^
{
    //animations here                    
}
completion:^(BOOL finished)
{
    yourView.userInteractionEnabled = YES;
}];
MikeS
  • 3,891
  • 6
  • 35
  • 51
2

To disable touch event in a view,

 [[UIApplication sharedApplication] beginIgnoringInteractionEvents];

To enable touch event in a view

[[UIApplication sharedApplication] endIgnoringInteractionEvents];
Raja Jimsen
  • 230
  • 3
  • 5
1

Disable userIntrection of Button.

Btn.userInteractionEnabled = NO;
Rajneesh071
  • 30,846
  • 15
  • 61
  • 74
0

I had view controller with icons that open pages. If the user was tapping quickly icon1 and icon2, the 2 pages opened.

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

-(void) on_image_tap:(UITapGestureRecognizer * ) tapGesture
{
    [[UIApplication sharedApplication] beginIgnoringInteractionEvents];
    [[UIApplication sharedApplication] performSelector:@selector(endIgnoringInteractionEvents) withObject:nil afterDelay:0.5f];
user1105951
  • 2,259
  • 2
  • 34
  • 55