Reputation: 1703
I have a start button with initial text "Start" hooked up to an action in Interface Builder.
However, the animation starts and finishes instantly, instead of taking 3 + 5 seconds. What is wrong with this code?
@interface ViewController()
@property (weak, nonatomic) IBOutlet UIButton *startButton;
@end
@implementation ViewController
- (IBAction)startPressed:(UIButton *)sender
{
[UIView animateWithDuration:3 delay:5
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
[self.startButton setTitle:@"New Title" forState:UIControlStateNormal];
}
completion:nil];
}
@end
Update: Of course, these responses are correct. I used Jack Wu's suggestion, though without hiding the text, setTitle makes the button flash back on the screen.
- (IBAction)startPressed:(UIButton *)sender
{
[UIView animateWithDuration:1.5 delay:5
options:UIViewAnimationOptionCurveEaseInOut
animations:^{ self.startButton.alpha = 0; }
completion:^(BOOL c){ if (c) {
self.startButton.hidden = YES;
[self.startButton setTitle:@"newTitle" forState:UIControlStateNormal];
[UIView animateWithDuration:1.5 delay:0 options:UIViewAnimationOptionCurveEaseInOut
animations:^{
self.startButton.hidden = NO;
self.startButton.alpha = 1;
}
completion:nil];
}}];
}
Upvotes: 1
Views: 2612
Reputation: 131418
Title is not an animatable property of UIButton. As others have suggested, you would need to create an animation yourself by doing something like fading out the button, changing the title, and fading it back.
You could also write custom animation code that would take a bitmap snapshot of the button, lay that on top of the current button, change the button title under the bitmap snapshot, then fade out and remove the bitmap. That would give you a cross-fade effect, but it would be a fair amount of work.
Upvotes: 1
Reputation: 3369
A button's title is not an animatable property, so you're going to see it return immediately. That's by design. You can find the animatable properties of a UIView in its' documentation. Here's what they are as of today.
@property frame
@property bounds
@property center
@property transform
@property alpha
@property backgroundColor
@property contentStretch
Alternately, you can add two subview labels to your button and cross fade them in your animation.
Upvotes: 4