Two straightforward actions: Rotate and Animate UIViews
Posted: April 7th, 2010 | Author: admin | Filed under: Objective-C | Tags: iPhone, Objective-C, Rotate | No Comments »When adding subviews to a superview doing addSubview and then bringSubviewToFront, I have found that subviews do not autorotate accordingly, therefore I needed to do it programmatically. The next piece of code does a landscape rotation of a UIView to the left or to the right. Note that the UIView has to be resized and located accordingly. For that, I set up the bounds to 480×320 because I always expect a +90 or -90 degrees rotation.
-
-
#define DegreesToRadian(__ANGLE__) ((__ANGLE__) / 180.0 * M_PI)
-
-
void rotateLandscape(UIView *viewToRotate, BOOL left) {
-
viewToRotate.transform = CGAffineTransformIdentity;
-
if (left) viewToRotate.transform = CGAffineTransformMakeRotation(DegreesToRadian(-90));
-
else viewToRotate.transform = CGAffineTransformMakeRotation(DegreesToRadian(+90));
-
viewToRotate.bounds = CGRectMake(0.0, 0, 480, 320);//resize the view
-
viewToRotate.center = CGPointMake(160.0f, 240.0f); //set the position of the view
-
}
-
An example on how to use it is:
-
rotateLanscape(appDelegate.mapController.view,TRUE);
The second common action, dealing with UIViews’ transitions on iPhone development, is to be able to set up an animation in the transition between views. The next code do that:
-
void setUpAnimation(UIView *animateWindow, UIViewAnimationTransition transitionType, float duration) {
-
-
[UIView beginAnimations:@"Animation" context:nil];
-
[UIView setAnimationDuration:duration ];
-
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
-
[UIView setAnimationTransition: transitionType forView:animateWindow cache:YES];
-
[UIView commitAnimations];
-
}
An example on how to use it is:
-
setUpAnimation(appDelegate.window, UIViewAnimationTransitionCurlDown, 0.5);
Embedding these two actions into functions sounds like a good idea since they will be repeated throughout your application many times. Also these are basic versions, they could allow more complex animations and rotations but I leave that for another day.