手动检测旋转到横向

Tho*_*oos 11 iphone rotation device

我正在为每个页面开发基于UITabBarController和UIViewControllers的iPhone应用程序.该应用程序只需要以纵向模式运行,因此每个视图控制器+应用程序委托都使用以下代码行:

- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation {
 return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
Run Code Online (Sandbox Code Playgroud)

有一个视图控制器,我想在iPhone旋转到landscapeleft时弹出UIImageView.虽然宽度和高度都是320x460(因此它的肖像),但图像的设计看起来很有风景.

如何在一个特定的视图控制器中手动检测这种类型的旋转,而不是在整个视图上自动旋转?

托马斯

更新:

谢谢!

我在viewDidLoad中添加了这个监听器:

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRotate:)name:UIDeviceOrientationDidChangeNotification object:nil];
Run Code Online (Sandbox Code Playgroud)

didRotate看起来像这样:

- (void) didRotate:(NSNotification *)notification

    {   
        UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];

        if (orientation == UIDeviceOrientationLandscapeLeft)
        {
            //your code here

        }
    }
Run Code Online (Sandbox Code Playgroud)

小智 20

我在一个旧项目中需要它 - 希望它仍然有效......

1)注册通知:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(detectOrientation)
                                             name:UIDeviceOrientationDidChangeNotification
                                           object:nil]; 
Run Code Online (Sandbox Code Playgroud)

2)然后你可以测试变化时的旋转:

-(void) detectOrientation {
    if (([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft) || 
        ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight)) {
        [self doLandscapeThings];
    } else if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait || [[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown) {
        [self doPortraitThings];
    }   
}
Run Code Online (Sandbox Code Playgroud)

希望有所帮助!

  • 谢谢!我在viewDidLoad中添加了这个监听器:[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRotate :) name:UIDeviceOrientationDidChangeNotification object:nil]; didRotate看起来像这样: - (void)didRotate:(NSNotification*)notification {UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; if(orientation == UIDeviceOrientationLandscapeLeft){//您的代码在这里}} (3认同)