Animate UIButton's title change

妖精的绣舞 提交于 2019-12-03 00:09:19

Use blocks:

[UIView transitionWithView:self.flipLabelButton duration:1 options:UIViewAnimationOptionTransitionFlipFromRight animations:^{

    [self.flipLabelButton setTitle:newText forState:UIControlStateNormal];

} completion:nil];

Swift

UIView.transition(with: button, duration: 0.5, options: .transitionCrossDissolve, animations: {
  self.button.setTitle("WOW, new title", for: .normal)
  self.button.setTitleColor(UIColor.red, for: .normal)
}, completion: nil)

In addition to AnimationGroups, there are generally two kinds of animations, one is for property animation, the other is for animating something like your content,transition or anything that cannot use property to do animation. So the second solution is your choice,and you can achieve this by two ways: use CATransition:

CATransition *transition = [CATransition animation];
transition.type = kCATransitionFade;
transition.duration = 1;
[button.layer addAnimation:transition forKey:kCATransition];
[button setTitle:newTitle forState:UIControlStateNormal];
[button setTitleColor:newColor forState:UIControlStateNormal];

the other way is using a UIKit block as @jjv360 said, but you can't animate color inside it because obviously color can't be flipped.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!