1

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?

Oleg
  • 1,383
  • 4
  • 19
  • 35
  • rotation don't have to be animating. I just have to place an object on my UIView rotated by x axis – Oleg Nov 24 '11 at 15:51

1 Answers1

2

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.

Dennis Bliefernicht
  • 5,147
  • 1
  • 28
  • 24
  • If test1 view contain buttons will they work normally when they will be transformed? – Oleg Nov 25 '11 at 13:37
  • Just tested transforming an UIButton: apparently they do work normally and even react to the changed region. Beware though that the actual rendered graphics are being transformed and butotns might look strange if scaled too much. – Dennis Bliefernicht Nov 25 '11 at 13:43
  • I've posted new question about rotation. Maybe You will help? [link](http://stackoverflow.com/questions/8271416/objective-calayer-uiview-real-rotation) – Oleg Nov 25 '11 at 15:49