Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIButton not interacting during animation

I'm trying to animate a UIButton. But during it's animation, there is no interaction with the UIButton. Expected behavior is to be able to click on the button while it's moving. Here's the code snippet of the UIButton and animation:

UIImage *cloudImage = [UIImage imageNamed:@"sprite.png"];    
UIButton moveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[moveBtn setFrame:CGRectMake(0.0, 80.0, cloudImage.size.width, cloudImage.size.height)];
[moveBtn setImage:cloudImage forState:UIControlStateNormal];
[moveBtn addTarget:self action:@selector(hit:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:moveBtn];
CGPoint newLeftCenter = CGPointMake( 300.0f + moveBtn.frame.size.width / 2.0f, moveBtn.center.y);
[UIView beginAnimations:nil context:nil]; 
[UIView setAnimationDuration:5.0f];
[UIView setAnimationRepeatCount:HUGE_VALF];
moveBtn.center = newLeftCenter;
[UIView commitAnimations];

hit selector just displays an NSLog to show if the button respond to it or not. Any help would be appreciated.

like image 365
Frank Avatar asked Jun 14 '11 15:06

Frank


Video Answer


2 Answers

Try setting the animation option to UIViewAnimationOptionAllowUserInteraction.

[UIView animateWithDuration:.2
                      delay: 0
                    options: UIViewAnimationOptionAllowUserInteraction
                 animations:^{ 
                     // animation logic
                 }
                 completion:^(BOOL completed) { 
                     // completion logic
                 }
 ];
like image 56
csano Avatar answered Nov 09 '22 23:11

csano


For swift 4 this code works,

UIView.animate(withDuration: 2, delay: 0, options: [.autoreverse, .repeat, .allowUserInteraction],
                   animations: {
                    self.btnCashGame?.frame.origin.y -= 15



    },completion:  { (finished: Bool) in
        self.btnCashGame?.frame.origin.y += 15

    })
like image 32
Narasimha Nallamsetty Avatar answered Nov 09 '22 23:11

Narasimha Nallamsetty