手动检测旋转到横向 [英] Detecting rotation to landscape manually
问题描述
我正在为每个页面开发基于UITabBarController和UIViewControllers的iPhone应用程序。应用程序只需要以纵向模式运行,因此每个视图控制器+应用程序委托都使用以下代码行:
I am working on an iPhone application based on UITabBarController and UIViewControllers for each page. The app needs to run in portrait mode only, so every view controller + app delegate goes with this line of code:
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
我想在一个视图控制器中弹出一个UIImageView iPhone被转向了landscapeleft。虽然宽度和高度都是320x460(因此它的肖像),但图像的设计看起来很有风景。
There is one view controller where I would like to pop up a UIImageView when the iPhone is rotaed to landscapeleft. The design of the image looks landscape, although the width and height are 320x460 ( so its portrait ).
我怎样才能/应该手动检测这种类型的旋转这个特定的视图控制器,在整个视图中没有自动旋转?
How can/should I detect this type of rotation manually, just in this specific view controller, withouth having an auto rotation on the entire view?
Thomas
更新:
谢谢!
我在viewDidLoad中添加了这个监听器:
I added this listener in the viewDidLoad:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRotate:)name:UIDeviceOrientationDidChangeNotification object:nil];
didRotate看起来像这样:
the didRotate looks like this:
- (void) didRotate:(NSNotification *)notification
{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if (orientation == UIDeviceOrientationLandscapeLeft)
{
//your code here
}
}
推荐答案
我在一个旧项目中需要它 - 希望它仍然可以...
I needed that in an old project - hope it still works...
1)注册通知:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(detectOrientation)
name:UIDeviceOrientationDidChangeNotification
object:nil];
2)然后你可以测试改变时的轮换:
2) Then you can test against the rotation on change:
-(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];
}
}
希望有所帮助!
这篇关于手动检测旋转到横向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!