我在AppDelegate中创建了一个视图,我将其添加到窗口中:
[window addSubview:myView];
我希望每次回到此视图时都能检查设备方向,所以我可以对它做一些修改。我怎么能在appDelegate中做到这一点?
答案 0 :(得分:11)
您可以在委托中实现其中一个方法,以查看应用程序何时轮换:
- (void)application:(UIApplication *)application willChangeStatusBarOrientation:(UIInterfaceOrientation)newStatusBarOrientation duration:(NSTimeInterval)duration;
- (void)application:(UIApplication *)application didChangeStatusBarOrientation:(UIInterfaceOrientation)oldStatusBarOrientation;
或者只需根据需要检查UIApplication状态栏的方向:
[[UIApplication sharedApplication] statusBarOrientation];
设备方向可能与界面方向匹配,也可能不匹配:
[[UIDevice currentDevice] orientation];
答案 1 :(得分:1)
以下是我在didFinishLauching中第一次加载应用时首先尝试的内容
[[NSNotificationcenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:@"UIDeviceOrientationDidChangeNotification" object: nil];
- (void)orientationChanged:(NSNotification *)notification
{
[self performSelector:@selector(showScreen) withObject:nil afterDelay:0];
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"UIDeviceOrientationDidChangeNotification" object:nil];
}
-(void)showScreen {
UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];
if (deviceOrientation == UIDeviceOrientationLandscapeLeft || UIDeviceOrientationLandscapeRight) {
CGRect screenRect = [[UIScreen mainScreen] bounds];
}
}
检测到横向,但screenRect显示宽度= 768,高度= 1024(我在Ipad设备中)。
答案 2 :(得分:0)
在Apples示例中,您将通过委托方法获得通知:
- (void)orientationChanged:(NSNotification *)notification
{
// We must add a delay here, otherwise we'll swap in the new view
// too quickly and we'll get an animation glitch
NSLog(@"orientationChanged");
[self performSelector:@selector(updateLandscapeView) withObject:nil afterDelay:0];
}
然后显示纵向屏幕:
- (void)updateLandscapeView
{
PortraitView *portraitView = [[PortraitView alloc] init];
portraitView.delegate = self;
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
if (UIDeviceOrientationIsLandscape(deviceOrientation) && !isShowingLandscapeView)
{
[self presentModalViewController: portraitView animated:YES];
isShowingLandscapeView = YES;
}
else if (deviceOrientation == UIDeviceOrientationPortrait && isShowingLandscapeView)
{
[self dismissModalViewControllerAnimated:YES];
isShowingLandscapeView = NO;
}
[portraitView release];
}
当然,您必须将PortraitView设计为委托类,以便按预期工作。
不是唯一的方法,但我发现它运行良好,它在苹果的例子中。我不会在Appdelegate中这样做,而是你的uiview,我不知道你的设计。