在UILabel中更改文字变化

我正在给UILabel设置一个新的文本值。 目前,新的文字显得很好。 不过,我想在出现新文字时添加一些animation。 我想知道我能做些什么来animation新文本的外观。

Objective-C的

为了实现真正的交叉转换(旧标签淡出新标签淡入),您不希望淡入淡出。 即使文本不变,这也会导致不必要的闪烁

改用这种方法:

 CATransition *animation = [CATransition animation]; animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; animation.type = kCATransitionFade; animation.duration = 0.75; [aLabel.layer addAnimation:animation forKey:@"kCATransitionFade"]; // This will fade: aLabel.text = "New" 

另请参阅: 两个数字之间的animationUILabel文本?

iOS 10,9,8演示:

空白,然后1到5淡入淡出过渡


使用Xcode 8.2.1和7.1iOS 10到8.0的 ObjectiveC进行testing。

►要下载完整的项目, 请在Swift Recipes中searchSO-3073520 。

我不知道它是否工作,它完美的作品!

Objective-C的

 [UIView transitionWithView:self.label duration:0.25f options:UIViewAnimationOptionTransitionCrossDissolve animations:^{ self.label.text = rand() % 2 ? @"Nice nice!" : @"Well done!"; } completion:nil]; 

Swift 3

 UIView.transition(with: label, duration: 0.25, options: .transitionCrossDissolve, animations: { [weak self] in self?.label.text = (rand() % 2 == 0) ? "One" : "Two" }, completion: nil) 

Swift 3

淡入淡出UILabel(或任何UIView)的正确方法是使用Core Animation Transition 。 这不会闪烁,如果内容没有改变,也不会变黑。

一个可移植和干净的解决scheme是在Swift中使用Extension (在更改可见元素之前调用)

 // Usage: insert view.fadeTransition right before changing content extension UIView { func fadeTransition(_ duration:CFTimeInterval) { let animation = CATransition() animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut) animation.type = kCATransitionFade animation.duration = duration layer.add(animation, forKey: kCATransitionFade) } } 

调用如下所示:

 // This will fade aLabel.fadeTransition(duration: 0.4) aLabel.text = "text" 

空白,然后1到5淡入淡出过渡


►在GitHub上find这个解决scheme和Swift Recipes的更多细节。

自iOS4以来,它可以显然完成块:

 [UIView animateWithDuration:1.0 animations:^{ label.alpha = 0.0f; label.text = newText; label.alpha = 1.0f; }]; 

这是做这个工作的代码。

 [UIView beginAnimations:@"animateText" context:nil]; [UIView setAnimationCurve:UIViewAnimationCurveEaseIn]; [UIView setAnimationDuration:1.0f]; [self.lbl setAlpha:0]; [self.lbl setText:@"New Text"; [self.lbl setAlpha:1]; [UIView commitAnimations]; 

Swift 2.0:

 UIView.transitionWithView(self.view, duration: 1.0, options: UIViewAnimationOptions.TransitionCrossDissolve, animations: { self.sampleLabel.text = "Animation Fade1" }, completion: { (finished: Bool) -> () in self.sampleLabel.text = "Animation Fade - 34" }) 

要么

 UIView.animateWithDuration(0.2, animations: { self.sampleLabel.alpha = 1 }, completion: { (value: Bool) in self.sampleLabel.alpha = 0.2 }) 

这是一个基于@ SwiftArchitect代码的C#UIView扩展方法。 当涉及到自动布局并且控件需要根据标签的文本移动时,这个调用代码使用标签的超视图作为转换视图而不是标签本身。 我添加了一个lambdaexpression式来使其更加封装。

 public static void FadeTransition( this UIView AView, double ADuration, Action AAction ) { CATransition transition = new CATransition(); transition.Duration = ADuration; transition.TimingFunction = CAMediaTimingFunction.FromName( CAMediaTimingFunction.Linear ); transition.Type = CATransition.TransitionFade; AView.Layer.AddAnimation( transition, transition.Type ); AAction(); } 

调用代码:

  labelSuperview.FadeTransition( 0.5d, () => { if ( condition ) label.Text = "Value 1"; else label.Text = "Value 2"; } ); 

如果你想在Swift做这个,试试这个:

 delay(1.0) { UIView.transitionWithView(self.introLabel, duration: 0.25, options: [.TransitionCrossDissolve], animations: { self.yourLabel.text = "2" }, completion: { finished in self.delay(1.0) { UIView.transitionWithView(self.introLabel, duration: 0.25, options: [.TransitionCrossDissolve], animations: { self.yourLabel.text = "1" }, completion: { finished in }) } }) } 

使用@matt创build的以下函数 – https://stackoverflow.com/a/24318861/1982051

 func delay(delay:Double, closure:()->()) { dispatch_after( dispatch_time( DISPATCH_TIME_NOW, Int64(delay * Double(NSEC_PER_SEC)) ), dispatch_get_main_queue(), closure) } 

这将在Swift 3中成为这个

 func delay(_ delay:Double, closure:()->()) { let when = DispatchTime.now() + delay DispatchQueue.main.after(when: when, execute: closure) } 

根据你的口味和需求,你可以select以下三个代码片段中的一个来为你的UILabel的文本更改添加一些交叉消解animation:

1.使用transition(with:duration:options:animations:completion:)

 import UIKit import PlaygroundSupport class ViewController: UIViewController { let label: UILabel = { $0.frame.origin = CGPoint(x: 50, y: 50) $0.text = "Bob" $0.sizeToFit() return $0 }(UILabel()) override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = .white view.addSubview(label) let tapGesture = UITapGestureRecognizer(target: self, action: #selector(toggle(_:))) view.addGestureRecognizer(tapGesture) } func toggle(_ sender: UITapGestureRecognizer) { let animation = { self.label.text = self.label.text == "Bob" ? "Dan" : "Bob" } UIView.transition(with: label, duration: 1, options: .transitionCrossDissolve, animations: animation, completion: nil) } } let controller = ViewController() PlaygroundPage.current.liveView = controller 

2.使用CATransitionCATransitiontype属性

 import UIKit import PlaygroundSupport class ViewController: UIViewController { let label: UILabel = { $0.frame.origin = CGPoint(x: 50, y: 50) $0.text = "Bob" $0.sizeToFit() return $0 }(UILabel()) let animation: CATransition = { $0.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut) $0.type = kCATransitionFade $0.duration = 1 return $0 }(CATransition()) override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = .white view.addSubview(label) let tapGesture = UITapGestureRecognizer(target: self, action: #selector(toggle(_:))) view.addGestureRecognizer(tapGesture) } func toggle(_ sender: UITapGestureRecognizer) { label.layer.add(animation, forKey: nil) label.text = label.text == "Bob" ? "Dan" : "Bob" label.sizeToFit() } } let controller = ViewController() PlaygroundPage.current.liveView = controller 

3.使用CATransitionadd(_:forKey:) key参数

 import UIKit import PlaygroundSupport class ViewController: UIViewController { let label: UILabel = { $0.frame.origin = CGPoint(x: 50, y: 50) $0.text = "Bob" $0.sizeToFit() return $0 }(UILabel()) let animation: CATransition = { $0.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut) $0.duration = 1 return $0 }(CATransition()) override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = .white view.addSubview(label) let tapGesture = UITapGestureRecognizer(target: self, action: #selector(toggle(_:))) view.addGestureRecognizer(tapGesture) } func toggle(_ sender: UITapGestureRecognizer) { label.layer.add(animation, forKey: kCATransitionFade) label.text = label.text == "Bob" ? "Dan" : "Bob" label.sizeToFit() } } let controller = ViewController() PlaygroundPage.current.liveView = controller