2012-11-05 16 views
15

Hier fand ich, wie UIButton ‚s Titeländerung mit inzwischen veraltet beginAnimations:context: Methode zu animieren:Animate UIButton Titel ändern

UIBUtton title animation for iPhone

Wie das gleiche zu tun mit aktuellen APIs?

Update:

Ich habe versucht, blockbasierte Animationen zu verwenden:

NSTimeInterval animationDuration = animated ? 1.f : 0.f; 
[UIView animateWithDuration:animationDuration delay:0.f options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionAllowUserInteraction animations:^{ 
    [button setTitle:newTitle forState:UIControlStateNormal]; 
    [button setTitleColor:newTitleColor forState:UIControlStateNormal]; 
} completion:nil]; 

Der Farbwechsel animiert nicht.

+0

Bitte geben Sie die Dokumentation überprüfen, tt alle auf UIView erläutert ist: http://developer.apple.com/library/ios/#documentation/uikit/reference/uiview_class/uiview /uiview.html mit einer klaren Erläuterung des 'beginAnimations: context:' -Ersatzes. –

Antwort

44

Verwenden Blöcke:

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

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

} completion:nil]; 
+0

Und das ist es? Ich dachte, dass nur Eigenschaften, die als "animierbar" dokumentiert sind, auf diese Weise animiert werden können. Oder nicht? –

+0

Wenn Sie die Animationsfunktionen ya verwenden, funktionieren die Übergangsfunktionen jedoch für alles, da es sich auf die gesamte Ansicht bezieht. – jjv360

+0

Ich habe meine Frage aktualisiert. Es animiert immer noch nicht. Irgendwelche Ideen? –

5

Neben AnimationGroups gibt es im Allgemeinen zwei Arten von Animationen, ist für Eigenschaft Animation, die andere ist so etwas wie Ihre Inhalte, Übergangs- oder etwas für die Animation, die nicht Eigentum verwenden können, Animation machen. So die zweite Lösung ist Ihre Wahl, und Sie können dies auf zwei Wegen erreichen: Verwendung 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]; 

andersrum ein UIKit Block wird unter Verwendung als @ jjv360 sagte aber Sie können nicht die Farbe animieren drin, weil offensichtlich Farbe nicht umgedreht werden kann.

8

Swift 3

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)