【问题标题】:Target action is lost after adding animation to button向按钮添加动画后目标动作丢失
【发布时间】:2014-11-07 11:19:43
【问题描述】:

以下代码是循环中的动画按钮。

[UIView setAnimationRepeatCount: 0];

self.introButton.transform = CGAffineTransformMakeScale(1.1, 1.1);
[UIView animateWithDuration: 0.4
                      delay: 0.0
                      options: UIViewAnimationOptionCurveEaseOut | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat
                      animations: ^{
                        self.introButton.transform = CGAffineTransformIdentity;
                      }completion: ^(BOOL finished){
                        //Code
                      }];

但是,在添加此动画后,我无法再在动作内部进行按钮修饰。如何制作动画并获得动作?

【问题讨论】:

    标签: ios animation uibutton


    【解决方案1】:

    在您的动画中包含UIViewAnimationOptionAllowUserInteraction 选项。

    UIView animateWithDuration: 0.4
                      delay: 0.0
                      options: UIViewAnimationOptionCurveEaseOut | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat | UIViewAnimationOptionAllowUserInteraction
                      animations: ^{
                        self.introButton.transform = CGAffineTransformIdentity;
                      }completion: ^(BOOL finished){
                        //Code
                      }];
    

    文档说:

    在动画期间,用户交互会暂时禁用动画视图。 (在 iOS 5 之前,整个应用程序都禁用了用户交互。)如果您希望用户能够与视图交互,请在 options 参数中包含 UIViewAnimationOptionAllowUserInteraction 常量

    【讨论】:

      【解决方案2】:

      看来你需要将动画视图和响应点击的视图分开

      UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
      [UIView animateWithDuration: 0.4
                            delay: 0.0
                          options: UIViewAnimationOptionCurveEaseOut | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat
                       animations: ^{
                           button.transform = CGAffineTransformMakeScale(1.1, 1.1);
                       }completion: ^(BOOL finished){
                           //Code
                       }];
      
      UIView *frontView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
      frontView.backgroundColor = [UIColor clearColor];
      [self.view addSubview:frontView];
      UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(refresh:)];
      [frontView addGestureRecognizer:gr];
      

      【讨论】:

      • 这个可以,但是真的没必要去这么麻烦。
      • @JesseRusak 是的,你是对的。在这里学习新东西:)
      猜你喜欢
      • 1970-01-01
      • 2018-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多