动画期间UIButton不交互

| 我正在尝试为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];
hit
选择器仅显示NSLog以显示按钮是否响应。任何帮助,将不胜感激。     
已邀请:
        最近在这里有一些疑问。动画是纯视觉的。您可以在旧框架中点击按钮,直到动画完成为止。完成后,实际的按钮会跳转。 编辑: 这个答案就是我所指的。显然,您需要使用NSTimer手动移动按钮。有关更多信息,请参见链接的问题/答案。 其他人建议传递ѭ2作为UIViewAnimation选项。     
        尝试将动画选项设置为
UIViewAnimationOptionAllowUserInteraction
[UIView animateWithDuration:.2
                      delay: 0
                    options: UIViewAnimationOptionAllowUserInteraction
                 animations:^{ 
                     // animation logic
                 }
                 completion:^(BOOL completed) { 
                     // completion logic
                 }
 ];
    
        此问题是由每个块的较大动画引起的。我制作了一个基于NSTimer的解决方案,就像上面建议的那样,并且奏效了……但是动作却很生涩(除非我在每个计时器事件触发器中插入了动画)。 因此,由于仍然需要动画,因此我找到了不需要计时器的解决方案。它仅对短距离进行动画处理,因此按钮单击仍然准确,只有一个小错误,这是我的情况在UI中非常不明显,并且可以根据您的参数进行减少。 请注意,在任何给定时间的误差均小于15.0,可以根据动画速度要求降低误差,以提高准确性。您还可以减少持续时间以提高速度。
- (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];
                    }];

}
    
        另一种选择是在要设置动画的按钮上方添加一个透明按钮。在您的特殊情况下,您可能无法使用此按钮,因为您正在移动按钮,但是您可能能够创建一个足够大的覆盖按钮以覆盖从起始位置到最后一个的覆盖按钮。 我遇到了这个问题,在我的情况下,使用UIViewAnimationOptionAllowUserInteraction无法正常工作。我正在对UIBarButtonItem(使用initWithCustomView:创建)中的UIButton的Alpha进行动画处理,以产生脉动效果,并且该选项不起作用。我也不喜欢NSTimer选项(不流畅),所以最后我只是添加了一个叠加按钮,我不喜欢它,但是可以完美地工作。     
        对于Swift 4,此代码有效,
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

    })
    

要回复问题请先登录注册