Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Width and height increase after CGAffineTransformRotate applied to a UIView

I want to rotate a view programmatically for that I have used below code:

CGAffineTransform rotateTransform = contentView.transform;
CGAffineTransform rotatenewTransform  = CGAffineTransformRotate(rotateTransform,aFloatRotate);
contentView.transform = rotatenewTransform;

When I do this the width and height of contentView (my UIView) increase.

What should I do so that contentView remains as it is and it rotates too?

thanks in advance.

Here i am explaining my problem in more details :

my contentview contains a buttons and a imageView in it. this contentView added in a background and this contentView should not go out of this background boundry.

but when i rotate this contentview by upper code, widht and height of contentView increases and so some times it goes out of background boundry.

I hope this will be clear to you. :)

like image 400
Prashant Bhayani Avatar asked Feb 01 '12 09:02

Prashant Bhayani


1 Answers

The width and height (presumably you are taking this from frame.size) have to change because they describing the smallest rectangle that holds the entire rotated view - if you rotate a rectangle by 45 degrees, then the rectangle to hold the rotated rectangle is wider and taller than the original rectangle.

The "real" size of your rotated view will still be available in the bounds rectangle - this is expressed in the view's internal coordinate system which is not rotated.

So, if your original frame was origin (100,100), size (100,50), your rotated view would have a frame where the origin and size was a rectangle that could fit your rotated view within it, described in the superview's coordinate system. If you now did this:

CGFloat width = contentView.frame.size.width;

You would get your changed value. However, if you did this:

CGFloat width = contentView.bounds.size.width;

You would get your original 100 width value.

like image 177
jrturton Avatar answered Sep 28 '22 04:09

jrturton