简体   繁体   中英

Custom autorotation iPhone SDK

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.

-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...)

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 .

我只想在用户更改设备方向时旋转子视图,那么您会要求什么?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM