How to prevent text change animation when changing text in UILabel

Asked 2 years ago, Updated 2 years ago, 129 views

If you animate the display location of UILabel (and View with UILabel as a child), changes to text that are not normally animated will also be animated.

Below is the sample code.

-(void)showErrorViewWithErrorText:(NSString*)errorText{
    // Set string to display in error view, error view has UILabel as child
    self.errorView.errorText=errorText;

    [UIView animationWithDuration: 0.2
                          delay —0.0
                        options:UIViewAnimationOptionTransitionNone
                     animations:^{
                         // Change constraint values to display off-screen error views
                         self.errorViewToNicknameConstraint.constant=55.0;                                                     
                         [self.view layoutIfNeeded];
    }
                     completion:^(BOOL finished) {

    }];
}

Currently, when the error view is displayed, the error display is difficult to understand because the characters in the label displayed in the error view are animated to change from old to new characters.

If you know how to prevent animation from changing text, please let me know.
Thank you for your cooperation.

ios uilabel

2022-09-30 15:33

1 Answers

Self-resolved.

After entering the new text, you will perform an animation that does not allow you to animate anything (it is difficult to understand in Japanese) and describe what you really want to animate after that.

Below is an example of the code.

-(void)showErrorViewWithErrorText:(NSString*)errorText{
    // Set the String to Display in Error View
    self.inputErrorView.errorText=errorText;
    // a do-nothing animation
    UIView animatedWithDuration: 0.0
                     animations:^{

    }
                     completion:^(BOOL finished) {
                         // Here's the real animation.
                         [UIView animationWithDuration: 0.2
                                               delay —0.0
                                             options:UIViewAnimationOptionTransitionNone
                                          animations:^{
                                              // Change constraint values to display off-screen error views
                                              self.inputErrorViewToNicknameConstraint.constant=55.0;
                                              [self.view layoutIfNeeded];
                                          }
                                          completion:^(BOOL finished) {

                                          }];
                     }];
    }


2022-09-30 15:33

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.