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?
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.
The bounds rectangle, which describes the view's location and size in its own coordinate system.
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;
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"];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With