美文网首页
iOS开发-设备倾斜方向判断

iOS开发-设备倾斜方向判断

作者: 沙漠无痕 | 来源:发表于2017-05-15 20:39 被阅读0次

    - (void)startMotion {

           self.motionManager = [[CMMotionManager alloc]init];

           if (self.motionManager.accelerometerAvailable) {

           [_motionManager setAccelerometerUpdateInterval:1/5.f];

           NSOperationQueue *operationQueue = [NSOperationQueue mainQueue];

           __weak typeof(self) wSelf = self;

           [_motionManager startAccelerometerUpdatesToQueue:operationQueue withHandler:^(CMAccelerometerData     *data,NSError *error)

           {

                  double x = data.acceleration.x;

                  double y = data.acceleration.y;

                  double z = data.acceleration.z;

                  if (fabs(z) < 1.0) {

                       if (fabs(y) > 0.1) {

                       if (y > 0){

                               NSLog(@"左");

                       } else{

                              NSLog(@"右");

                      }

                } else if (fabs(x) > 0.15) {

                    if (x > 0) {

                            NSLog(@"上");

                    } else{

                           NSLog(@"下");

                   }

             }

         }

         }];

     }

    }

    相关文章

      网友评论

          本文标题:iOS开发-设备倾斜方向判断

          本文链接:https://www.haomeiwen.com/subject/obtkxxtx.html