UIButton在动画期间没有交互

Fra*_*ank 10 iphone core-animation uibutton caanimation

我正在尝试为UIButton制作动画.但在动画期间,没有与UIButton的互动.预期的行为是能够在移动时单击按钮.这是UIButton和动画的代码片段:

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];
Run Code Online (Sandbox Code Playgroud)

hit选择器只显示一个NSLog,以显示按钮是否响应它.任何帮助,将不胜感激.

csa*_*ano 26

尝试将动画选项设置为UIViewAnimationOptionAllowUserInteraction.

[UIView animateWithDuration:.2
                      delay: 0
                    options: UIViewAnimationOptionAllowUserInteraction
                 animations:^{ 
                     // animation logic
                 }
                 completion:^(BOOL completed) { 
                     // completion logic
                 }
 ];
Run Code Online (Sandbox Code Playgroud)


Mos*_*she 11

最近这里有一些问题.动画纯粹是视觉效果.您可以点击旧框架中的按钮,直到动画完成,完成后,实际按钮会跳跃.

编辑:

这个答案就是我所说的.显然,您需要使用NSTimer手动移动按钮.有关更多信息,请参阅链接的问题/答案

其他人建议传递UIViewAnimationOptionAllowUserInteractionUIViewAnimation选项.