I have a UIView whose height and width, upon rotation to 90degrees, are interchanged. Now, when I try to increase the height or width, I get abnormal looks. How do I change the height of a rotated UIView?
-
I just need a way change the angle of the way it appears on the screen. – Charles D'Monte May 27 '13 at 12:04
-
5`view.transform = CGAffineTransformMakeRotation(M_PI / 2);` – May 27 '13 at 12:05
-
1The 90 degrees was an example. I am rotating the view manually. The height and width are changing when I do so. I need to just change the angle of the view on the screen. – Charles D'Monte May 27 '13 at 12:07
-
So are you currently using the method I've just shown? – May 27 '13 at 12:09
-
I am converting angles to radians and I am entering the value within the brackets – Charles D'Monte May 27 '13 at 12:10
-
As suggested by H2CO3, change the UIView's CGAffineTransform property based on your need. If your using touch to rotate the view, consider setting this rotation with a `UIRotationGestureRecognizer` set on the view. – Vinzzz May 27 '13 at 12:11
-
How can I use UIRotationGestureRecogniser to work with a single finger? – Charles D'Monte May 27 '13 at 12:16
-
Please check this question http://stackoverflow.com/questions/5468559/rotate-image-on-center-using-one-finger-touch – Nitin Gohel May 27 '13 at 12:34
2 Answers
Apple's Documentation states that the frame
property of views becomes undefined when the view's transform
is not the identity transformation.
Rotating the view changes the view's transformation.
Now, why does this invalidate the frame? Apple's documentation is actually a little imprecise: The frame property does not become entirely meaningless for transformed views. Instead, it will now reflect the bounding rectangle of the view, that is, the smallest upright rectangle that the transformed view can fit into.
This is because frame
is actually a derived property. If you want to change the "real height" of a transformed view, that is, the height in its own coordinate system (before the transformation is applied), there is a property for that: bounds
.
So, in a nutshell:
CGRect bounds = myView.bounds;
bounds.size.height = newHeight;
myView.bounds = bounds;

- 9,842
- 3
- 37
- 57
-
So, if I enter these lines in my code, where should I add the CGAffineTransformMakeRotation part? After or before? – Charles D'Monte May 27 '13 at 12:28
-
-
Thank you; glad I could help! For the record (I'm sure you've figured this out yourself by now): If you're changing the bounds, it doesn't matter whether you're changing the transformation before or afterwards. If you're changing the frame, be sure to do this only while `CGAffineTransformIsIdentity(myView.transform)` is YES (i. e. before changing the transformation). Check out this blog post (not by me) for further info about the frames vs. bounds thing: http://ashfurrow.com/blog/you-probably-dont-understand-frames-and-bounds – fzwo May 27 '13 at 13:38
Use following method...
- (void) runSpinAnimationOnView:(UIView*)view duration:(CGFloat)duration rotations:(CGFloat)rotations repeat:(float)repeat;
{
CABasicAnimation* rotationAnimation;
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 /* full rotation*/ * rotations * duration ];
rotationAnimation.duration = duration;
rotationAnimation.cumulative = YES;
rotationAnimation.repeatCount = repeat;
[view.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}

- 4,266
- 3
- 32
- 50