Sean
Sean

Reputation: 7045

IOS UIView animation

- (void)showLabelAnimation
{
[UIView beginAnimations:@"animationID" context:nil];

[UIView setAnimationDuration:1];

//original rectangle is (0,0,100,50)

//and I want to change label to (100,100,200,100)
label.bounds = CGRectMake(100, 100, 200, 100);
label.frame = CGRectMake(100, 100, 200, 100);

[UIView commitAnimations];
}

It works well, but I just wondering why I have to set both bounds and frame of label. They seem like the same thing. Can anyone explains this for me? Thanks in advance.

[Edit in Nov 3rd] Thanks for your help, but when I remove the setbounds line, it doesn't work well, the label will be large immediately but move to new position by animation. The resize animation does not show up.

That's the real thing which wonder me.

Upvotes: 0

Views: 1893

Answers (3)

Tim
Tim

Reputation: 14446

You should only have to set the frame. Bounds should update on its own.

Edit for additional help: Labels don't scale; changing the frame of it changes where the text is placed, but the size of the text does not scale this way.

However, you can enable the UILabel's adjustsFontSizeToFitWidth property to 'yes' and then before you run the animation, set the font size to be at its largest before running it. As it scales, the font size should change to fit and achieve the effect you're looking for.

Upvotes: 3

Vincent Bernier
Vincent Bernier

Reputation: 8664

That code is working for me

    [UIView animateWithDuration:1
                 animations:^(void) {
                     self.leLabel.frame = CGRectMake(labFrame.origin.x, 
                        labFrame.origin.y + 100.0f, labFrame.size.width + 50.0f, labFrame.size.height);
                 }];

For what you want to do, you should only set the frame. If I change frame by bound in the previous code I will get a very strange behaviour.

So my guess is the reason you need to set both is because you've tested first with bound and added frame after to correct the error introduced by the setting of bound.

Upvotes: 1

Jesse Black
Jesse Black

Reputation: 7986

The frame is the rectangle relative to the view's parent. The bounds is the rectangle relative to itself.

The animation should work without the line

label.bounds = CGRectMake(100, 100, 200, 100);

Upvotes: 1

Related Questions