Iphone DeviceOrientation签入AppDelegate
我在AppDelegate中创建了一个视图,我将其添加到窗口中,如下所示:我希望每次返回此视图时都能够检查设备方向,以便对其进行一些修改。如何在appDelegate中执行此操作?您可以在委托中实现以下方法之一,以查看应用程序何时旋转:Iphone DeviceOrientation签入AppDelegate,iphone,objective-c,Iphone,Objective C,我在AppDelegate中创建了一个视图,我将其添加到窗口中,如下所示:我希望每次返回此视图时都能够检查设备方向,以便对其进行一些修改。如何在appDelegate中执行此操作?您可以在委托中实现以下方法之一,以查看应用程序何时旋转: [window addSubview:myView]; - (void)application:(UIApplication *)application willChangeStatusBarOrientation:(UIInterfaceOrientation
[window addSubview:myView];
- (void)application:(UIApplication *)application willChangeStatusBarOrientation:(UIInterfaceOrientation)newStatusBarOrientation duration:(NSTimeInterval)duration;
- (void)application:(UIApplication *)application didChangeStatusBarOrientation:(UIInterfaceOrientation)oldStatusBarOrientation;
或者根据需要使用以下工具检查UIApplication状态栏的方向:
[[UIApplication sharedApplication] statusBarOrientation];
设备方向(可能与接口方向匹配,也可能与接口方向不匹配)为:
[[UIDevice currentDevice] orientation];
在Apple示例中,您通过委托方法得到通知:
- (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];
}
当然,您必须将肖像视图设计为一个委托类,这样才能按预期工作
这不是唯一的方法,但我发现它工作得很好,在苹果的例子。虽然我不会在Appdelegate中这样做,但我更喜欢你的uiview,我不知道你的设计。这是我在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 show width=768,height=1024(我在Ipad设备中)。请查看我的新答案。我将解释你的解决方案中的一个新问题。我认为问题在于你只是在检查它是否已旋转。你没有用它做任何事。如果您呈现一个新视图,您应该看到边界是您所期望的。我怀疑操作系统处理它的方式与如果它不响应方向的改变是一样的,除非你在showseen中做些什么。如果这有意义的话……不,我真的不明白,你告诉我要显示一个新视图,但是如果我的screenRect仍然指示纵向模式,我怎么做呢?创建一个新的UIView,并像其他任何ViewController一样显示它,除非你在nib中执行任何操作,在横向中进行。您在代码中所做的是,如果方向为横向,则显示横向中的新视图,否则关闭横向视图