UIButton does not interact during animation

I am trying to revive UIButton. But during its animation, there is no interaction with UIButton. Expected behavior is the ability to click a button while moving it. Here's a snippet of UIButton code 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]; 
Selector

hit simply displays the NSLog to show whether the button responds to it or not. Any help would be appreciated.

+9
source share
5 answers

There have been some questions about this recently. The animation is purely visual. You can click on the button so that it is old, until the animation ends, when it ends, the actual button jumps.

EDIT:

This answer is what I was talking about. Apparently you need to manually move the button using NSTimer. See Related Question / Answer for more.

Other people suggest passing UIViewAnimationOptionAllowUserInteraction as a parameter to UIViewAnimation.

+10
source

Try setting the animation parameter to UIViewAnimationOptionAllowUserInteraction .

 [UIView animateWithDuration:.2 delay: 0 options: UIViewAnimationOptionAllowUserInteraction animations:^{ // animation logic } completion:^(BOOL completed) { // completion logic } ]; 
+25
source

This problem is caused by large block animations. I made a solution based on NSTimer, as suggested above, and it worked ... but the movement was jerky (unless I inserted an animation into each trigger of the timer event).

So, since animation was required anyway, I found a solution that does not require a timer. It enlivens only a small distance, and thus the button press is still accurate, and only a small error, which is my case, is very invisible in the user interface and can be reduced depending on your settings.

Note below that the error at any time is <15.0, which can be reduced for greater accuracy depending on the requirements of the animation speed. You can also shorten duration times for greater speed.

 - (void)conveyComplete:(UIView*)v { [self convey:v delay:0]; } - (void)convey:(UIView*)v delay:(int)nDelay { [UIView animateWithDuration:.5 delay:nDelay options:(UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction) animations: ^ { CGRect rPos = v.frame; rPos.origin.x -= 15.0; v.frame = rPos; } completion: ^(BOOL finished) { [self conveyComplete:v]; }]; } 
0
source

Another option is to add a transparent button above the button you are animating. In your particular case, you may not be able to use this when you move the button, but you can create an overlay button large enough to cover it from the starting position to the last.

I had this problem and in my case using UIViewAnimationOptionAllowUserInteraction did not help. I animated the UIButton alpha inside the UIBarButtonItem (created using initWithCustomView :) to create a pulsating effect, and this setting will not work. I don't like the NSTimer option (not smooth), so in the end I just added an overlay button that I don't like, but works flawlessly.

0
source

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 }) 
0
source

All Articles