Reputation: 1393
I have a UIView subclassed object with initial height obj.frame.size.height.
UIImageView *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(...)];
imageView setImage:...
//here comes rotation
I have a variable double newHeight (it is less than obj's initial height) I need to rotate obj by x axis. after rotation obj's height must change to newHeight How could I do that?
Upvotes: 1
Views: 1914
Reputation: 5157
You can use QuartzCore transformations to achieve this. First of all, if not done already, you have to add the QuartzCore framework. Now import <QuartzCore/QuartzCore.h>
in your appropriate file. An example transformation to achieve a flip on the x-Axis and resizing to a new height could look like this:
CALayer *testLayer = test1.layer;
double oldSize = test1.bounds.size.height;
double newSize = 100.0;
testLayer.transform = CATransform3DConcat(CATransform3DMakeRotation(M_PI, 0.0, 1.0, 0.0),
CATransform3DMakeScale(1.0, newSize/oldSize, 1.0)
);
test1
here is the view that needs to be transformed. This is achieved by first rotating by half a circle (PI in radians) around the y axis (this effectively flips left and right). Then a scaling transform leaves x and z as they are but scales y so that the new height will be exactly the required newSize
. Note that this transform only changes the rendering. If you for example now changed the bounds
of the view to the new height, the transformation would be applied to that and you would scale the new (bigger/smaller) object twice.
Depending on the needed rotation you could also rotate along the x axis to flip up and down or do arbitrary rotations around z, e.g. CATransform3DMakeRotation(M_PI_4, 0.0, 0.0, 1.0) to rotate 45 degrees clockwise. Also beware of the order of transformations as this can result in undesirable effects.
Upvotes: 2