9

I have a View with UIButton, UITextField, and a UIImageView for Background.

in viewDidLoad i try animate UIImageView from alpha=0 to alpha =1 using block. it's pretty basic, here's the code:

[UIView animateWithDuration:1.5 animations:^(void){

    ((UIView*)[self.view viewWithTag:123]).alpha = 1;
}completion:^(BOOL finished){
}];

which is working fine. but during that 1.5 seconds of animation, my touch in current view seems to be disabled. i can't click any of the buttons or textFields until animation finish.

Thanks in Advance

HelmiB
  • 12,303
  • 5
  • 41
  • 68

6 Answers6

31

You should use option UIViewAnimationOptionAllowUserInteraction as in the following example:

[UIView animateWithDuration:1.0 
                      delay:0 
                    options:UIViewAnimationOptionAllowUserInteraction 
                 animations:^{ myView.alpha = 0.5; } 
                 completion:NULL];
d.lebedev
  • 2,305
  • 19
  • 27
  • i believe nothing wrong with my code as well as yours. the differ only it has delay and options. tried though, not working... – HelmiB Oct 14 '11 at 08:41
  • 3
    >> the differ only it has delay and options. -- Please be sure, that you don't set alpha to zero. Use at least 0.1 instead of 0.0 `animations:^{ myView.alpha = 0.1; } ` – Yauheni Shauchenka Mar 28 '13 at 09:26
3

UIView userinteraction in animation using .allowUserInteraction for Swift 3+,iOS 10

Add to the animation call the UIViewAnimationOptions flag .allowUserInteraction

UIView.animate(withDuration: 1.0, delay: 0.0, usingSpringWithDamping: 0.4, initialSpringVelocity: 0.0, options: [.curveEaseIn,.allowUserInteraction], animations: {

            //your animation block

            }, completion: { isAnimated in
            //after animation is done
    })
evya
  • 3,381
  • 1
  • 25
  • 28
Rafat touqir Rafsun
  • 2,777
  • 28
  • 24
1

Seems like animating buttons in general makes their click states pretty finicky. The solution I came up with for this was to have the animating element just be a UIView with all the button styles. Before the UIView animates I add a real button to the view above the UIView with a clear background to the location I want the user interaction to occur. Definitely adds an extra step but is very dependable.

//create button and view
UIButton *viewNotificationBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, ([UIScreen mainScreen].bounds.size.height -70), 320, 50)];
UIView *viewNotificationView = [[UIView alloc] initWithFrame:CGRectMake(0, ([UIScreen mainScreen].bounds.size.height), 320, 50)];

//add elements to screen
[self.view addSubview:viewNotificationView];
[self.view insertSubview:viewNotificationBtn aboveSubview:viewNotificationView];


[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
    [viewNotificationView setFrame:CGRectMake(0, ([UIScreen mainScreen].bounds.size.height - 70), 320, 50)];
} completion:^(BOOL finished) {
    [UIView animateWithDuration:0.5 delay:10.0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
        viewNotificationView.alpha = 0;
    } completion:^(BOOL finished) {
        [viewNotificationView removeFromSuperview];
        [viewNotificationBtn removeFromSuperview];
    }];
}];
Bueno
  • 1,840
  • 2
  • 15
  • 17
0

Isn’t it something that happens on standart iOS apps like Settings back button, menu swipe. There is no way to touch to screen while it is animating whereas you can touch in Android.

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 15 '21 at 07:52
0

Use animateWithDuration:delay:options:animations:completion: method and set UIViewAnimationOptionAllowUserInteraction as option

Martin Ullrich
  • 94,744
  • 25
  • 252
  • 217
0

I ended up not doing this. You can't touch a view that still animating. that's my conclusion. Would be happy to hear some thought on this though.

HelmiB
  • 12,303
  • 5
  • 41
  • 68