User Tools

Site Tools



There are multiple ways to make UIViews animate. The simplest of these is to use the UIView class method animateWithDuration:animations.

As this is a class method you do not call it on your views, but on UIView itself. The first argument takes a double as the period of time in which to execute the animation. The second argument is a block. Within this block, change one or multiple fields of the frame property of the intended views.

Example, assuming your animating view is called myView and you are moving it down the screen 100 pixels:

[UIView animateWithDuration:0.25 animations:^{
            CGRect oldframe = myView.frame;
            oldFrame.origin.y = oldFrame.origin.y + 100;
            myView.frame = oldFrame;

You cannot change a field of a CGRect without replacing the entire CGRect.

The old frame of the view is first extracted, the y coordinate is modified, then assigned back to the view. The difference between the view's old position and new position on the screen will change over a period of .25 seconds.

ios-labs-s14/advanced-animations.txt · Last modified: 2014/02/25 15:25 by mbarboi