哪种方法确定设备从纵向到横向模式的旋转
问题描述:
我有这种方法,但我应该在哪里放这个方法?哪种方法确定设备从纵向到横向模式的旋转
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(receivedRotate:) name: UIDeviceOrientationDidChangeNotification
object: nil];
// This method is called by NSNotificationCenter when the device is rotated.
-(void) receivedRotate: (NSNotification*) notification
{
UIDeviceOrientation interfaceOrientation = [[UIDevice currentDevice] orientation];
if(interfaceOrientation != UIDeviceOrientationUnknown)
[self deviceInterfaceOrientationChanged:interfaceOrientation];
}
答
取决于你正在尝试做什么。如果您只想更改视图的方向以匹配设备,则在您的viewController中实现willRotateToInterface和shouldAutorotateToInterfaceOrientation。这比任何事情都简单得多。在shouldAutorotateToInterfaceOrientation中,您肯定会为您愿意接受的每个方向重新编写一个YES。这里是例程的样子:
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return ((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) ||
(interfaceOrientation == UIInterfaceOrientationLandscapeRight));
}