哪种方法确定设备从纵向模式到横向模式的旋转

时间:2009-09-19 05:46:23

标签: iphone

我有这个方法,但我应该把这个方法放在哪里???

[[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];
}

2 个答案:

答案 0 :(得分:1)

取决于你想要做什么。如果您只想更改视图的方向以匹配设备,则在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));
}

答案 1 :(得分:0)

请参阅this thread