前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >iOS 监听设备方向旋转(iOS 9)

iOS 监听设备方向旋转(iOS 9)

作者头像
半纸渊
发布2018-09-04 16:48:48
1.3K0
发布2018-09-04 16:48:48
举报
文章被收录于专栏:Code_iOSCode_iOS
代码语言:javascript
复制
typedef NS_ENUM(NSInteger, VFOrientation) {
    VFOrientationPortrait = 1,
    VFOrientationLandscape = -1,
};

// 处理设备旋转
- (void)viewWillTransitionToSize:(CGSize)size 
withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator{
    
    NSLog(@"T ^ T");
    if ([self isPortraitOrLandscape:size] == VFOrientationPortrait) {
        // do something
    }
    
    if ([self isPortraitOrLandscape:size] == VFOrientationLandscape) {
        // do something
    }
    
}

- (VFOrientation) isPortraitOrLandscape:(CGSize)size {
    
    return (size.width > size.height ? VFOrientationLandscape : 
                                        VFOrientationPortrait);
    
}
代码语言:javascript
复制
- (void) addDeviceRotateObserver {
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(rotateViews:)
                                                 name:UIDeviceOrientationDidChangeNotification
                                               object:nil];
}

- (void) rotateViews:(NSObject *)sender {
    
    UIDevice* device = [sender valueForKey:@"object"];
    NSLog(@"%d",device.orientation);
    
 //   switch (device.orientation) {
 //       case UIDeviceOrientationPortrait: {
//            NSLog(@"P");
//            break;
//        }
//        case UIDeviceOrientationLandscapeLeft: {
//            NSLog(@"LL");
//            break;
//        }
//        case UIDeviceOrientationLandscapeRight: {
//            NSLog(@"LR");
//            break;
//        }
//        default:
//            break;
//    }
    
    switch (device.orientation) {
        case UIDeviceOrientationUnknown: {
            // do something
            break;
        }
        case UIDeviceOrientationPortrait: {
            // do something
            break;
        }
        case UIDeviceOrientationPortraitUpsideDown: {
            // do something
            break;
        }
        case UIDeviceOrientationLandscapeLeft: {
            // do something
            break;
        }
        case UIDeviceOrientationLandscapeRight: {
            // do something
            break;
        }
        case UIDeviceOrientationFaceUp: {
            // do something
            break;
        }
        case UIDeviceOrientationFaceDown: {
            // do something
            break;
        }
    }
}

- (void) removeDeviceRotateObserver {
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIDeviceOrientationDidChangeNotification
                                                  object:nil];
    [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
}

- (void)dealloc {
    [self removeDeviceRotateObserver];
}

http://blog.csdn.net/jpcfei/article/details/8995531

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016.06.01 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档