Animate text change in UILabel

后端 未结 14 1270
[愿得一人]
[愿得一人] 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:56

    Objective-C

    To achieve a true cross-dissolve transition (old label fading out while new label fading in), you don't want fade to invisible. It would result in unwanted flicker even if text is unchanged.

    Use this approach instead:

    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"
    

    Also see: Animate UILabel text between two numbers?

    Demonstration in iOS 10, 9, 8:


    Tested with Xcode 8.2.1 & 7.1, ObjectiveC on iOS 10 to 8.0.

    ► To download the full project, search for SO-3073520 in Swift Recipes.

提交回复
热议问题