6

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?

fzwo
  • 9,842
  • 3
  • 37
  • 57
Charles D'Monte
  • 370
  • 4
  • 16

2 Answers2

9

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;
fzwo
  • 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
  • Dude, you've made my day. Heck of a thanks, buddy. – Charles D'Monte May 27 '13 at 12:52
  • 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
1

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"];
}
Shardul
  • 4,266
  • 3
  • 32
  • 50