Animate text change in UILabel

后端 未结 14 1255
[愿得一人]
[愿得一人] 2020-11-28 17:56

I\'m setting a new text value to a UILabel. Currently, the new text appears just fine. However, I\'d like to add some animation when the new text appears. I\

14条回答
  •  一整个雨季
    2020-11-28 18:54

    I wonder if it works, and it works perfectly!

    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, 4, 5

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

提交回复
热议问题