views:

838

answers:

3

Hi!

I want to implement my own way of autorotating the interface: Code:

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 NSLog(@"should!");
 [UIView beginAnimations:nil context:NULL];
 [UIView setAnimationDuration:0.4];
 if (interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
  trash.transform = CGAffineTransformMakeRotation(M_PI*0.5);
 }
 else if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
  trash.transform = CGAffineTransformMakeRotation(-M_PI*0.5);
 }
 else if (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
  trash.transform = CGAffineTransformIdentity;//CGAffineTransformMakeRotation(M_PI);
 }
 else {
  trash.transform = CGAffineTransformIdentity;

 }
 [UIView commitAnimations];
 return NO;
}

As you see I've a UIView (in fact UIImageView) - trash and I'm rotating it using CGAffineTransformMakeRotation when interface orientation is changed. It works great except, the method never gets called when the interface orientation is default portrait (up side down works). So the trash is rotated to the previous orientation instead of default. I'm worried a little bit about the comment above the method (automatically added when creating UIViewController), that says "to allow orientations other than the default portrait orientation".

When I return the YES instead of NO of course the method is called for every orientation, but I don't want to animate the whole interface, but only some elements.

Plz help.

+1  A: 

-shouldAutorotateToInterfaceOrientation: is not meant for you to do anything but to return YES or NO. It is not a call to tell you that the iPhone has been rotated, or will be rotated. It is a question being asked of your viewController to see if it would allow for an orientation other than the current one.

If you don't have a view controller or subclass, then you're not going to need this call. Either way, you return YES or NO, and leave it at that.

The rest of your code, you can use when a button is tapped, or if you detect movement from the accelerometer, etc.

(Generally view controllers do you a favor and do the rotation for you, and the shouldAutorotateToInterfaceOrientation is just your approval. If you are working without view controllers or want to rotate some elements yourself, then you just find the right moment in your code to do the rotation...)

mahboudz
A: 

I just want to rotate the subviews when user changes the orientation of device, so what would you recommand?

Kris
+1  A: 

If you just want to know current orientation of the device (which may be different than rotation of the UI!), use beginGeneratingDeviceOrientationNotifications and orientation methods of UIDevice.

If you want to change interface when it rotates, implement didRotateFromInterfaceOrientation in UIViewController.

porneL