簡體   English   中英

iOS 按鈕在動畫期間不可點擊

[英]iOS Button not clickable during animation

當我使用類別方法向按鈕添加動畫時,我無法單擊該按鈕,似乎它已被禁用:

[_compassCalibrateButton pulse:1.5 continuously:YES];
_compassCalibrateButton.userInteractionEnabled=YES;

我有一個包含此內容的 UIView 類別:

- (void)pulse:(float)secs continuously:(BOOL)continuously {
    [UIView animateWithDuration:secs/2 
                          delay:0.0 
                        options:UIViewAnimationOptionCurveLinear
                     animations:^{
                         // Fade out, but not completely
                         self.alpha = 0.3;
                     }
                     completion:^(BOOL finished) { 
                         [UIView animateWithDuration:secs/2 
                                               delay:0.0 
                                             options:UIViewAnimationOptionCurveLinear
                                          animations:^{
                                              // Fade in
                                              self.alpha = 1.0;
                                          }
                                          completion:^(BOOL finished) { 
                                              if (continuously) {
                                                  [self pulse:secs continuously:continuously];
                                              }
                                          }];
                     }];
}

從文檔

在動畫過程中,用戶交互被臨時禁用動畫視圖。 (在 iOS 5 之前,整個應用程序都禁用了用戶交互。)如果您希望用戶能夠與視圖交互,請在 options 參數中包含 UIViewAnimationOptionAllowUserInteraction 常量。

所以你的代碼應該是

- (void)pulse:(float)secs continuously:(BOOL)continuously {
    [UIView animateWithDuration:secs/2 
                          delay:0.0 
                        options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction
                     animations:^{
                         // Fade out, but not completely
                         self.alpha = 0.3;
                     }
                     completion:^(BOOL finished) { 
                         [UIView animateWithDuration:secs/2 
                                               delay:0.0 
                                             options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction
                                          animations:^{
                                              // Fade in
                                              self.alpha = 1.0;
                                          }
                                          completion:^(BOOL finished) { 
                                              if (continuously) {
                                                  [self pulse:secs continuously:continuously];
                                              }
                                          }];
                     }];

}

我在 Swift 4 上測試過

 UIView.animate(withDuration: 0.8, delay: 0, options: [.repeat, .autoreverse, .allowUserInteraction], animations: {
        // Do Something
    }, completion: nil)

試試這個它會幫助你

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
      UITouch *touch = [touches anyObject];
      CGPoint touchLocation = [touch locationInView:self.view];
      for (UIButton *button in self.arrBtn)
      {
          if ([button.layer.presentationLayer hitTest:touchLocation])
         {
        // This button was hit whilst moving - do something with it here
            [button setBackgroundColor:[UIColor cyanColor]];
             NSLog(@"Button Clicked");
             break;
         }
      }
}

請注意,如果您正在動畫UIButton一段時間后淡出,例如:

[UIView animateWithDuration:1.0 delay:3.0 options:|UIViewAnimationOptionAllowUserInteraction animations:^{
    self->btn.alpha = 0;
} completion:nil];

由於最終的 alpha 版本,無論您的“延遲”如何,它從一開始就沒有響應。 解決這個問題的一種方法是:

[UIView animateWithDuration:1.0 delay:3.0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
    self->btn.alpha = 0.1;
} completion:^(BOOL finished){self->btn.alpha = 0;}];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM