Animate UILabel text between two numbers?

后端 未结 6 725
遇见更好的自我
遇见更好的自我 2020-11-28 19:01

I\'m new to iPhone and Mac programming (developed for Windows before), and I\'ve got a question:

How do I animate the text property of an UILabel<

6条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-28 19:51

    It works well!

    Objective-C

    [UIView transitionWithView:self.label 
                      duration:.5f 
                       options:UIViewAnimationOptionCurveEaseInOut | 
                               UIViewAnimationOptionTransitionCrossDissolve 
                    animations:^{
    
        self.label.text = rand() % 2 ? @"111!" : @"42";
    
    } completion:nil];
    

    Swift 2

    UIView.transitionWithView(label, duration: 0.25, options: [.CurveEaseInOut, .TransitionCrossDissolve], animations: {
        self.label.text = (arc4random() % 2 == 0) ? "111" : "222"
    }, completion: nil)
    

    Swift 3, 4, 5

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

提交回复
热议问题