Animate text change in UILabel

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

    This is a C# UIView extension method that's based on @SwiftArchitect's code. When auto layout is involved and controls need to move depending on the label's text, this calling code uses the Superview of the label as the transition view instead of the label itself. I added a lambda expression for the action to make it more encapsulated.

    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();
    }
    

    Calling code:

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

提交回复
热议问题