Animate UIButton state change

前端 未结 5 752
天涯浪人
天涯浪人 2020-12-12 21:00

I\'m using a UIButton with images for normal and highlighted states. They work as expected but I want to have some fading/merging transition and not just a sudden swap.

5条回答
  •  抹茶落季
    2020-12-12 21:05

    Here is a self contained solution that also supports a boolean animated flag.

    - (void)setEnabled:(BOOL)enabled animated:(BOOL)animated
    {
      if (_button.enabled == enabled) {
        return;
      }
    
      void (^transitionBlock)(void) = ^void(void) {
        _button.enabled = enabled;
      };
    
      if (animated) {
        [UIView transitionWithView:_button
                          duration:0.15
                           options:UIViewAnimationOptionTransitionCrossDissolve
                        animations:^{
                          transitionBlock();
                        }
                        completion:nil];
      } else {
        transitionBlock();
      }
    }
    

提交回复
热议问题