views:

1533

answers:

3

Hi Everyone:

I have seen other people who have had this question, but most of the responses aren't working on the latest 3.0 build of iPhone OS. Anyway, I'm wondering how I can programatically rotate a UIView without any input from the accelerometer. The code I have found so far:

CGAffineTransform transform = CGAffineTransformMakeRotation(3.14159/2);
view.transform = transform;
CGRect contentRect = CGRectMake(-80, 80, 480, 320);
view.bounds = contentRect;

However, this doesn't work for UIView (in my testing). Is there something I have to do my AppDelegate in order for this/other code to function, or is there a better way of doing the same thing?

Thanks for any help!

A: 

I had success with that:

CATransform3D rotationTransform = CATransform3DIdentity;
[view.layer removeAllAnimations];
rotationTransform = CATransform3DRotate(rotationTransform, angle, 0.0, 0.0, 1);
view.layer.transform = rotationTransform;
Marco
Hi Marco: In the above code, what would the "angle" be?
PF1
In addition, this doesn't seem to be able to build even though I have imported the QuartzCore framework.
PF1
Angle is the angle in radiants the view will be rotated. It won't build maybe because of other errors. What does build log say?
Marco
It says 'CATransform3D' undeclared (first use in this function) and 'rotationTransform' undeclared (first use in this function)
PF1
Did you import QuartzCore.h?
Marco
Yeah, I have, but it still generates that error.
PF1
I know this is super old, but I think PF1 might have been getting confused with the difference between "importing" the QuartzCore framework, and having the actual `#import <QuartzCore/QuartzCore.h>` declaration at the top of the file.
Matt Rix
+2  A: 

this works for me

 CGAffineTransform transform = CGAffineTransformMakeRotation(3.14159/2);
 self.view.transform = transform;

 // Repositions and resizes the view.
 CGRect contentRect = CGRectMake(0,0, 480, 320);
 self.view.bounds = contentRect;
ashish
Use M_PI instead of 3.14159
William Jockusch
A: 

Not sure if this is considered private API, so you may not want to use it, but if you want to force the orientation to change without tilting the device, you can try calling:

[[UIDevice currentDevice] performSelector:@selector(setOrientation:)
                      withObject:(id)UIInterfaceOrientationLandscapeLeft];

I have not tested that it still works on 3.1.2. though I know it works on 3.0.

Matt Long