如何防止 UIButton 在更新标题时闪烁

当我在 UIButton 上调用 setTitle 时,iOS7中的按钮会闪烁。我试着设置 myButton.highred = NO,但是这并没有阻止按钮的闪烁。

[myButton setTitle:[[NSUserDefaults standardUserDefaults] stringForKey:@"elapsedLabelKey"] forState:UIControlStateNormal];


myButton.highlighted = NO;

下面是我如何设置更新标题的计时器:

- (void)actionTimer {
if (myTimer == nil) {


myTimer = [NSTimer scheduledTimerWithTimeInterval: 1.0
target: self
selector: @selector(showActivity)
userInfo: nil
repeats: YES];
}
}

下面是实际更新标题的方法:

- (void)showActivity {


NSString *sym = [[NSLocale currentLocale] objectForKey:NSLocaleCurrencySymbol];


if (pauseInterval == nil) {


// Update clock
seconds = [[NSDate date] timeIntervalSinceDate:startInterval] - breakTime;


// Update total earned
secRate = rate.value / 60 / 60;
total = secRate * seconds;
[totalLabel setTitle:[NSString stringWithFormat:@"%@%.4f",sym,total] forState:UIControlStateNormal];


days = seconds / (60 * 60 * 24);
seconds -= days * (60 * 60 * 24);
int hours = seconds / (60 * 60);
fhours = (float)seconds / (60.0 * 60.0);
seconds -= hours * (60 * 60);
int minutes = seconds / 60;
seconds -= minutes * 60;


// Update the timer clock
[elapsed setTitle:[NSString stringWithFormat:@"%.2i:%.2i:%.2i:%.2i",days,hours,minutes,seconds] forState:UIControlStateNormal];
}
}
25920 次浏览

See the responses from How to stop unwanted UIButton animation on title change? :

[UIView setAnimationsEnabled:NO];
[elapsed setTitle:[NSString stringWithFormat:@"%.2i:%.2i:%.2i:%.2i",days,hours,minutes,seconds] forState:UIControlStateNormal];
[UIView setAnimationsEnabled:YES];

*Please note *

when "buttonType" of _button is "UIButtonTypeSystem", below code is invalid

[UIView setAnimationsEnabled:NO];
[_button setTitle:@"title" forState:UIControlStateNormal];
[UIView setAnimationsEnabled:YES];

when "buttonType" of _button is "UIButtonTypeCustom", above code is valid.

Set the button type to UIButtonTypeCustom and it'll stop flashing

Default "setTitle" behavior is definitely hateful!

My solution is:

[UIView setAnimationsEnabled:NO];
[_button layoutIfNeeded];
[_button setTitle:[NSString stringWithFormat:@"what" forState:UIControlStateNormal];
[UIView setAnimationsEnabled:YES];

Also, in storyboard, under button's property I uncheck:

  • Reverses On Highlight
  • Highlighted Adjust Image

And check:

  • Disabled Adjust Image

Tested and working on iOS 8 too.

Update - Swift

I suggest you to create your custom button and override setTitle method.

class UnflashingButton: UIButton {
   

override func setTitle(title: String?, forState state: UIControlState) {
UIView.setAnimationsEnabled(false)
self.layoutIfNeeded()
super.setTitle(title, forState: state)
UIView.setAnimationsEnabled(true)
}


}

A better approach than [UIView setAnimationsEnabled:NO] which may affect other animations is to only disable the specific title animation.

Objective-C:

[UIView performWithoutAnimation:^{
[myButton setTitle:text forState:UIControlStateNormal];
[myButton layoutIfNeeded];
}];

Swift:

UIView.performWithoutAnimation {
myButton.setTitle(text, for: .normal)
myButton.layoutIfNeeded()
}

You can simply just make another function for UIButton for easy future usage.

extension UIButton {
func setTitleWithoutAnimation(_ title: String?, for controlState: UIControlState) {
UIView.performWithoutAnimation {
self.setTitle(title, for: controlState)
self.layoutIfNeeded()
}
}
}

That is it!

And just set the title as you would before but replace setTitle with setTitleWithoutAnimation

I'm new to coding and Stackoverflow, so don't have enough reputation to comment directly to expand on https://stackoverflow.com/users/4673064/daniel-tseng excellent answer. So I have to write my own new answer, and it is this:

extension UIButton {
func setTitleWithoutAnimation(_ title: String?, for controlState: UIControlState) {
UIView.performWithoutAnimation {
self.setTitle(title, for: controlState)
self.layoutIfNeeded()
}
}
}

Works great, EXCEPT:

If all my calls later on in the code to "setTitleWithoutAnimation" do not specify a sender, then I get these weird messages related to CoreMedia or CoreData, e.g., "Failed to inherit CoreMedia permissions from 2526: (null)"

This is probably pretty basic to coding and iOS, but for me as a new coder, it sent me on a rabbit trail for a while, as in: Today Extension Failed to inherit CoreMedia permissions from, where people had interesting answers but that did NOT reach the root of my problem.

So I finally found that I had to go through and give my parameter-less functions parameters, i.e., I had to specify a sender. That sender could be UIButton, Any, or AnyObject. All of those worked, but ultimately there appears to be a conflict between adding a button extension with "self" and not specifically saying later on that a button is using it.

Again, probably basic, but new to me, so I figured it would be worth sharing.

Try this, this works in newer versions of IOS:

class CustomButtonWithNoEffect : UIButton {
override func setTitle(_ title: String?, for state: UIControlState) {
UIView.performWithoutAnimation {
super.setTitle(title, for: state)
super.layoutIfNeeded()
}
}


}

Another trick

@IBOutlet private weak var button: UIButton! {
didSet {
button.setTitle(title, for: .normal)
}
}

This works only if you know the value of the title without making any API calls. The button's title will be set before the view is loaded so you won't see the animation