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

检测iOS UIDevice方向
EN

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

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

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

EN

回答 9

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-09-10 13:30:18

如果您是在寻找如何检测方向更改(而不一定要禁用旋转)的问题时,您还应该知道iOS 8提供的viewWillTransitionToSize

here中的Swift示例

代码语言:javascript
复制
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {

    coordinator.animateAlongsideTransition({ (UIViewControllerTransitionCoordinatorContext) -> Void in

        let orient = UIApplication.sharedApplication().statusBarOrientation

        switch orient {
        case .Portrait:
            println("Portrait")
            // Do something
        default:
            println("Anything But Portrait")
            // Do something else
        }

        }, completion: { (UIViewControllerTransitionCoordinatorContext) -> Void in
            println("rotation completed")
    })

    super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
}

如果你不需要担心实际的方向:

代码语言:javascript
复制
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {

    // do something

    super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
}

来自here的Objective-C示例

代码语言:javascript
复制
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{   
    [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context)
    {
        UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
        // do whatever
    } completion:^(id<UIViewControllerTransitionCoordinatorContext> context)
    { 

    }];

    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
}

如果你不需要担心实际的方向(取自this answer):

代码语言:javascript
复制
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
    // Do view manipulation here.
    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
}

另请参阅

票数 21
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提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9122149

复制
相关文章

相似问题

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