Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animate UILabel not smooth

I am trying to animate a UIlabel to first grow larger, then shrink back to its original frame. Enlarging work as expected, but not shrinking. When I shrink the label with the below code, the size adjusts first before the origin is shifted. This causes a two step animation which isn't 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){
                 }];
like image 979
prostock Avatar asked Jun 14 '11 00:06

prostock


2 Answers

You could try applying a transform to the label inside your animation block instead of adjusting the rect. Something like the following lines for the grow/shrink animations:

label.transform = CGAffineTransformMakeScale(1.5, 1.5); //grow
label.transform = CGAffineTransformMakeScale(1, 1);     //shrink
like image 142
jmosesman Avatar answered Sep 30 '22 21:09

jmosesman


Please tryout this solution, I guess it is what you are looking for. I have tested it for working but please try and let me know if you are looking for this 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];
}
like image 24
Janak Nirmal Avatar answered Sep 30 '22 21:09

Janak Nirmal