码迷,mamicode.com
首页 > 移动开发 > 详细

ios 检测屏幕方向

时间:2016-04-27 12:25:03      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:

方法一:通知中心监听

-(void)notifitionSatatus{
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarOrientationChange:) name:UIApplicationDidChangeStatusBarOrientationNotification
object:nil]; } -(void)statusBarOrientationChange:(NSNotification*)notification{ UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; if (orientation == UIInterfaceOrientationLandscapeRight) // home键靠右 { } if (orientation ==UIInterfaceOrientationLandscapeLeft) // home键靠左 { } if (orientation == UIInterfaceOrientationPortrait)//home在下 { } if (orientation == UIInterfaceOrientationPortraitUpsideDown) { } NSLog(@"%ld",orientation); }

注意:在没有打开屏幕旋转开关的情况下,该通知无效

方法二:陀螺仪监测

- (void)startMotionManager{
    if (_motionManager == nil) {
        _motionManager = [[CMMotionManager alloc] init];
    }
  //检测时间间隔 _motionManager.deviceMotionUpdateInterval
= 1.0; if (_motionManager.deviceMotionAvailable) { NSLog(@"Device Motion Available"); [_motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue] withHandler: ^(CMDeviceMotion *motion, NSError *error){ [self performSelectorOnMainThread:@selector(handleDeviceMotion:) withObject:motion waitUntilDone:YES]; }]; } else { NSLog(@"No device motion on device."); [self setMotionManager:nil]; } } - (void)handleDeviceMotion:(CMDeviceMotion *)deviceMotion{ double x = deviceMotion.gravity.x; double y = deviceMotion.gravity.y; if (fabs(y) >= fabs(x)) { if (y >=0){ //UIDeviceOrientationPortraitUpsideDown; [self.delegate actionWithDeviceOrientation:UIDeviceOrientationPortraitUpsideDown]; } else{ // UIDeviceOrientationPortrait; [self.delegate actionWithDeviceOrientation:UIDeviceOrientationPortrait]; } } else { if (x >= 0){ // UIDeviceOrientationLandscapeRight; [self.delegate actionWithDeviceOrientation:UIDeviceOrientationLandscapeRight]; } else{ // UIDeviceOrientationLandscapeLeft; [self.delegate actionWithDeviceOrientation:UIDeviceOrientationLandscapeLeft]; } } }
//停止检测
-(void)endMotionManager{ [_motionManager stopDeviceMotionUpdates]; }

ios 检测屏幕方向

标签:

原文地址:http://www.cnblogs.com/chaochaobuhuifei55/p/5438337.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!