Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rotate a UIView without its frame changing [closed]

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?

like image 358
Charles D'Monte Avatar asked May 27 '13 12:05

Charles D'Monte


People also ask

When to use bounds iOS?

Since frame relates a view's location in its parent view, you use it when you are making outward changes, like changing its width or finding the distance between the view and the top of its parent view. Use the bounds when you are making inward changes, like drawing things or arranging subviews within the view.

What is bounds in swift?

The bounds rectangle, which describes the view's location and size in its own coordinate system.


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;
like image 70
fzwo Avatar answered Sep 29 '22 07:09

fzwo


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"];
}
like image 23
Shardul Avatar answered Sep 29 '22 08:09

Shardul