如何为UIButton内容对齐中的更改设置动画

And*_*son 2 cocoa-touch objective-c ios

UIButton的几个属性本身并不具有动画效果.其中一个属性是contentHorizontalAlignment财产.在没有运气的情况下搜索SO后,我想出了下面的答案来解决"不可动画"的问题.

Igo*_*gor 6

您应该layoutIfNeeded在更改对齐值后设置动画:

目标C:

button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[UIView animateWithDuration:0.3 animations:^{       
  [self.view layoutIfNeeded];
}];
Run Code Online (Sandbox Code Playgroud)

Swift3:

button.contentHorizontalAlignment = .left
UIView.animate(withDuration: 0.3, animations: {
  self.view.layoutIfNeeded()
})
Run Code Online (Sandbox Code Playgroud)