Animate UILabel text between two numbers?

后端 未结 6 734
遇见更好的自我
遇见更好的自我 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:56

    You can use the automatic transitions. It's working perfectly well :

    // Add transition (must be called after myLabel has been displayed)
     CATransition *animation = [CATransition animation];
    animation.duration = 1.0;
    animation.type = kCATransitionFade;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [myLabel.layer addAnimation:animation forKey:@"changeTextTransition"];
    
    // Change the text
    myLabel.text = newText;
    

    This code works if myLabel is already displayed. If not myLabel.layer will be nil and the animation will not be added to the object.


    in Swift 4 that would be:

    let animation: CATransition = CATransition()
    animation.duration = 1.0
    animation.type = kCATransitionFade
    animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
    myLabel.layer.add(animation, forKey: "changeTextTransition")
    

提交回复
热议问题