UILabel animation is not smooth

I am trying to animate UIlabel in order to grow first, and then go back to the original frame. An extension of the work was expected, but not a reduction. When I squeeze the shortcut with the code below, the size changes first until the origin is shifted. This causes a two-stage animation that is not smooth.

Here is my code:

CGRect rect = label.frame; [UIView animateWithDuration:.2 delay: 0.1 options: UIViewAnimationOptionBeginFromCurrentState animations:^{ label.frame = CGRectMake(rect.origin.x + 4, rect.origin.y + 4, rect.size.width-8, rect.size.height-8); } completion:^(BOOL finished){ }]; 
+6
source share
3 answers

You can try applying the transform to the label inside the animation block instead of setting rect. Something like the following lines to animate growth / compression:

 label.transform = CGAffineTransformMakeScale(1.5, 1.5); //grow label.transform = CGAffineTransformMakeScale(1, 1); //shrink 
+2
source

Try this solution, I think this is what you are looking for. I tested it for work, but please try and let me know if you are looking for it or not.

 -(IBAction)growanimate:(id)sender { [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:1]; [UIView setAnimationCurve:UIViewAnimationCurveLinear]; [UIView setAnimationDelegate:self]; [UIView setAnimationDidStopSelector:@selector(shrinkanimate)]; label.transform = CGAffineTransformMakeScale(2.0f, 2.0f); //This will double label from current size. [UIView commitAnimations]; } -(void)shrinkanimate { [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:1]; [UIView setAnimationCurve:UIViewAnimationCurveLinear]; label.transform = CGAffineTransformMakeScale(1.0f, 1.0f); //This will get it back to original size. [UIView commitAnimations]; } 
+2
source

I assume the problem is that changing the frame size leads to a change in the size of the notifications. With a reduction, there may be more interruptions as new areas of observation are identified.

In this case, the conversion method is much better.

I also assume that using the conversion method, paths and glyph layouts (line breaks, etc.) are not recalculated and cached. CGPath is simply converted during rendering.


About the problem of centering, I do not see any problems.

I would like to add that if you plan to widely use this effect, you can create a static class for this effect or containing several predefined animations with statically saved transformations inside.

Then you make such calls: [MyPopEffect popView: mylabel];

It avoids creating and releasing transformations and allows instant use of any kind or in other projects.

Anyway, here is the animation code ... Greetings

[

 UIView animateWithDuration:0.5f delay: 0.0f options:UIViewAnimationOptionCurveEaseOut+UIViewAnimationOptionBeginFromCurrentState animations:^{ label.transform=CGAffineTransformMakeScale(2.0f,2.0f); } completion:^(BOOL finished){ [UIView animateWithDuration:0.5f delay: 0.0f options:UIViewAnimationOptionCurveEaseIn+UIViewAnimationOptionBeginFromCurrentState animations:^{ label.transform=CGAffineTransformMakeScale(1.0f,1.0f); } completion:^(BOOL finished){}]; }]; 

]

+1
source

Source: https://habr.com/ru/post/890453/


All Articles