在ios7中,我的UIButton标题在错误的时间动画进出——迟到。在iOS 6上不出现此问题。我用的是:
[self setTitle:text forState:UIControlStateNormal];
我更希望这能立即发生,而不是一个空白的框架。这种眨眼特别分散注意力,并将注意力从其他动画中转移开。
在ios7中,我的UIButton标题在错误的时间动画进出——迟到。在iOS 6上不出现此问题。我用的是:
[self setTitle:text forState:UIControlStateNormal];
我更希望这能立即发生,而不是一个空白的框架。这种眨眼特别分散注意力,并将注意力从其他动画中转移开。
当前回答
你可以简单地创建自定义按钮,它将停止动画而改变标题。
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setTitle:@"the title" forState:UIControlStateNormal];
你也可以在Storyboard复选框中完成: 选择storyboard中的按钮->选择属性检查器(左边第四个)->在“类型”下拉菜单中,选择“自定义”而不是可能被选中的“系统”。
好运!
其他回答
设置UIButton类型为自定义。这应该会删除淡入和淡出动画。
具有系统类型的UIButton在setTitle(_:for:)上有隐式动画。你可以用两种方法来解决这个问题:
设置按钮类型为自定义,从代码或接口生成器:
let button = UIButton(type: .custom)
从代码中禁用动画:
UIView.performWithoutAnimation {
button.setTitle(title, for: .normal)
button.layoutIfNeeded()
}
Swift 4版Xhacker刘回答
import Foundation
import UIKit
extension UIButton {
func setTitleWithOutAnimation(title: String?) {
UIView.setAnimationsEnabled(false)
setTitle(title, for: .normal)
layoutIfNeeded()
UIView.setAnimationsEnabled(true)
}
}
我发现这个解决方案与UIButtonTypeSystem一样,但只有当按钮出于某种原因被启用时才会工作。
[UIView setAnimationsEnabled:NO];
[_button setTitle:@"title" forState:UIControlStateNormal];
[UIView setAnimationsEnabled:YES];
因此,如果您需要在设置标题时禁用按钮,则必须添加这些。
[UIView setAnimationsEnabled:NO];
_button.enabled = YES;
[_button setTitle:@"title" forState:UIControlStateNormal];
_button.enabled = NO;
[UIView setAnimationsEnabled:YES];
(iOS 7, Xcode 5)
也许生成2个动画和2个按钮是一个更好的解决方案,以避免出现动画和更改按钮文本的问题?
我创建了第二个uibutton并生成了2个动画,这个解决方案没有hickup。
_button2.hidden = TRUE;
_button1.hidden = FALSE;
CGPoint startLocation = CGPointMake(_button1.center.x, button1.center.y - 70);
CGPoint stopLocation = CGPointMake(_button2.center.x, button2.center.y- 70);
[UIView animateWithDuration:0.3 animations:^{ _button2.center = stopLocation;} completion:^(BOOL finished){_button2.center = stopLocation;}];
[UIView animateWithDuration:0.3 animations:^{ _button1.center = startLocation;} completion:^(BOOL finished){_button1.center = startLocation;}];