首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >检测iOS UIDevice方向

检测iOS UIDevice方向
EN

Stack Overflow用户
提问于 2012-02-03 08:35:33
回答 7查看 57.1K关注 0票数 75

我需要检测设备何时处于纵向,这样我才能发出特殊的动画。但我不希望我的视图自动旋转。

当设备旋转为纵向时,如何覆盖自动旋转的视图?我的应用程序只需要在风景中显示它的视图,但似乎我也需要支持肖像,如果我想要能够检测到肖像的旋转。

EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2012-02-03 08:55:57

在加载应用程序或视图时尝试执行以下操作:

代码语言:javascript
复制
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter]
   addObserver:self selector:@selector(orientationChanged:)
   name:UIDeviceOrientationDidChangeNotification
   object:[UIDevice currentDevice]];

然后添加以下方法:

代码语言:javascript
复制
- (void) orientationChanged:(NSNotification *)note
{
   UIDevice * device = note.object;
   switch(device.orientation)
   {
       case UIDeviceOrientationPortrait:
       /* start special animation */
       break;

       case UIDeviceOrientationPortraitUpsideDown:
       /* start special animation */
       break;

       default:
       break;
   };
}

以上操作将允许您在不启用视图自动旋转的情况下注册设备的方向更改。

备注

在iOS中的所有情况下,当您添加观察者时,也要在适当的时间删除它(可能,但并非总是,当视图出现/消失时)。你只能有“对”的观察/不观察代码。如果你不这样做,应用程序将崩溃。选择在何处观察/不观察超出了本QA的范围。然而,你必须有一个"unobserve“来匹配上面的"observe”代码。

票数 178
EN

Stack Overflow用户

发布于 2015-07-20 06:33:03

1)大卫答案的Swift版本2)如果你仍然想在没有方向改变的情况下检测方向(Moe对How Do I detect the orientation of the device on iOS?的答案的Swift版本)

代码语言:javascript
复制
    // Initial device orientation
    let orientation: UIInterfaceOrientation = UIApplication.sharedApplication().statusBarOrientation
    if(orientation == UIInterfaceOrientation.Unknown){
        // code for Unknown
    }
    else if(orientation == UIInterfaceOrientation.Portrait){
        // code for Portrait
    }
    else if(orientation == UIInterfaceOrientation.PortraitUpsideDown){
        // code for Portrait
    }
    else if(orientation == UIInterfaceOrientation.LandscapeRight){
        // code for Landscape        
    }
    else if(orientation == UIInterfaceOrientation.LandscapeLeft){
        // ode for Landscape
    }

    // To detect device orientation change
    UIDevice.currentDevice().beginGeneratingDeviceOrientationNotifications()
    NSNotificationCenter.defaultCenter().addObserver(
        self,
        selector: "orientationChanged:",
        name: UIDeviceOrientationDidChangeNotification,
        object: UIDevice.currentDevice())

orientationChanged函数

代码语言:javascript
复制
func orientationChanged(note: NSNotification)
{
    let device: UIDevice = note.object as! UIDevice
    switch(device.orientation)
    {
        case UIDeviceOrientation.Portrait:
        // code for Portrait
        break
        case UIDeviceOrientation.PortraitUpsideDown:
        // code for Portrait
        break
        case UIDeviceOrientation.LandscapeLeft:
        // code for Landscape
        break
        case UIDeviceOrientation.LandscapeRight:
        // code for Landscape
        break
        case UIDeviceOrientation.Unknown:
        // code for Unknown
        break
        default:
        break
    }
}
票数 3
EN

Stack Overflow用户

发布于 2012-02-03 08:55:26

如果我没理解错的话,你的应用程序只是个景观应用。您可以简单地在应用程序设置中指定它是仅横向的,因此不需要担心旋转。无论iPad是如何定位的,这款应用程序都会从横向开始,并一直停留在那里。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9122149

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档