Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - animate movement of a label or image

How can I animate the movement of a label or image? I would just like to make a slow transition from one location on the screen to another (nothing fancy).

like image 992
Nippysaurus Avatar asked Sep 08 '10 01:09

Nippysaurus


3 Answers

For ios4 and later you should not use beginAnimations:context and commitAnimations, as these are discouraged in the documentation.

Instead you should use one of the block-based methods.

The above example would then look like this:

[UIView animateWithDuration:0.3 animations:^{  // animate the following:
    myLabel.frame = newRect; // move to new location
}]; 
like image 90
Hjalmar Avatar answered Nov 17 '22 14:11

Hjalmar


You're looking for the -beginAnimations:context: and -commitAnimations methods on UIView.

In a nutshell, you do something like:

[UIView beginAnimations:nil context:NULL]; // animate the following:
myLabel.frame = newRect; // move to new location
[UIView setAnimationDuration:0.3];
[UIView commitAnimations];
like image 12
David Gelhar Avatar answered Nov 17 '22 15:11

David Gelhar


Here is an example with a UILabel - the animation slides the label from the left in 0.3 seconds.

// Save the original configuration. 
CGRect initialFrame = label.frame;

// Displace the label so it's hidden outside of the screen before animation starts.
CGRect displacedFrame = initialFrame;
displacedFrame.origin.x = -100;
label.frame = displacedFrame;

// Restore label's initial position during animation. 
[UIView animateWithDuration:0.3 animations:^{
    label.frame = initialFrame;
}];
like image 3
Zorayr Avatar answered Nov 17 '22 13:11

Zorayr