Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animating UILabel size decrease

When increasing the height of label, everything is fine and smooth. When decreaseing, the label is instantly changing the size then repositioning with animation.

@interface
@property (nonatomic, retain) IBOutlet UILabel *explanationLabel;

@implementation
CGRect frmExpl = explanationLabel.frame;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.75];

frmExpl.size.height -= height;
explanationLabel.frame = frmExpl;

[UIView commitAnimations];  

I've tried replacing UILabel with UIView and of course there is no such problem with UIView.

Is there any special way to animate UILabel size decrease?

Here is a minimal project demonstrating the issue described. Download

like image 200
Pablo Avatar asked Jul 21 '10 11:07

Pablo


3 Answers

The issue is that the UILabel redraws itself as soon as its size changes. (It can't redraw every frame of the animation because text rendering happens on the CPU, not the GPU where UIView animations run.) You can keep it from redrawing by changing the label's contentMode property to, e.g., UIViewContentModeCenter.

like image 198
davehayden Avatar answered Nov 04 '22 01:11

davehayden


I think what you are wanting to change is the bounds, rather than the frame. From the docs:

"The bounds rectangle determines the origin and scale in the view’s coordinate system within its frame rectangle and is measured in points. Setting this property changes the value of the frame property accordingly." - UIView Class; bounds property

Try something like:

- (void)animate:(id)sender
{
    ...
    CGRect newBounds = testLabel.bounds;
    newBounds.size.height += 50;
    testLabel.bounds = newBounds;
    ...
}
like image 34
Matthew Leffler Avatar answered Nov 04 '22 02:11

Matthew Leffler


Use a CGAffineTransform to do this.

[UIView animateWithDuration:1.0 animations:^{
    // Scale down 50%
    label.transform = CGAffineTransformScale(label.transform, 0.5, 0.5);
} completion:^(BOOL finished) {
    [UIView animateWithDuration:1.0 animations:^{
        // Scale up 50%
        label.transform = CGAffineTransformScale(label.transform, 2, 2);
    }];
}];
like image 2
trapper Avatar answered Nov 04 '22 02:11

trapper