Animate UIButton state change

前端 未结 5 753
天涯浪人
天涯浪人 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:10

    This can be done using transition animation of a UIView. It does not matter the isHighlighted property is not animatable, because it transitions the whole view.

    Swift 3

    UIView.transition(with: button,
                      duration: 4.0,
                      options: .transitionCrossDissolve,
                      animations: { button.isHighlighted = true },
                      completion: nil)
    

    Objective-C

    [UIView transitionWithView:button
                      duration:4.0
                       options:UIViewAnimationOptionTransitionCrossDissolve
                    animations:^{ button.highlighted = YES; }
                    completion:nil];
    

提交回复
热议问题