Question

I'm using this block to animate a pulse on my text and when I do, the text moves to the right some and jerks back. Seems like it is due to some padding or something being added by interface builder.

[UIView animateWithDuration:0.5 animations:^{
    // grow the label up to 130%, using a animation of 1/2s
    myLabel.transform = CGAffineTransformMakeScale(1.3,1.3);
} completion:^(BOOL finished) {
    // When the "grow" animation is completed, go back to size 100% using another animation of 1/2s
    [UIView animateWithDuration:0.5 animations:^{
        myLabel.transform = CGAffineTransformIdentity;
    }];
}];
Was it helpful?

Solution

Reduce the anchorPoint by a factor of 1.3 on the way back. Change your code to this -

CGPoint anchorPoint = myLabel.layer.anchorPoint;
[UIView animateWithDuration:0.5 animations:^{
    // grow the label up to 130%, using a animation of 1/2s
    myLabel.transform = CGAffineTransformMakeScale(1.3,1.3);
} completion:^(BOOL finished) {
    // When the "grow" animation is completed, go back to size 100% using another animation of 1/2s
    myLabel.layer.anchorPoint = CGPointMake(anchorPoint.x/1.3, anchorPoint.y/1.3);
    [UIView animateWithDuration:0.5 animations:^{
        myLabel.transform = CGAffineTransformIdentity;
    }];
}];

OTHER TIPS

I tried your code. It is working fine for the labels which text is fitted in the label properly or those labels have the text alignment center.

So my suggestion is if you have label having static or fixed text then give the background color to label and check for label text if it is too large then make it as such so that text fit to label properly. Then your code will work fine.

Now if you have dynamic text then try to calculate the label width dynamically as below

UIFont *font = [UIFont systemFontOfSize:9.0];
CGSize stringSize;
stringSize = [myLabel.text sizeWithFont:font constrainedToSize:CGSizeMake(1000, myLabel.frame.size.height) lineBreakMode:UILineBreakModeWordWrap];

Then assign stringSize.width as mylabel width. Means here you will change the width of label dynamically. Then you code will work fine as you want.

You should try this code i think it will help you,first it will Zoom your image to 130 % then it will zoom out your image to the original size

 [UIView animateWithDuration:0.5 animations:^{
            myLabel.transform = CGAffineTransformMakeScale(1.3,1.3);
        } completion:^(BOOL finished) {

               [UIView animateWithDuration:0.5 animations:^{
                myLabel.transform = CGAffineTransformMakeScale(1,1);
                myLabel.hidden=YES;
            }];
        }];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top