首页 > 代码库 > CMDeviceMotion的使用

CMDeviceMotion的使用

CMDeviceMotion的使用

by 伍雪颖

manager = [[CMMotionManager alloc] init];
ViewController *__weak weakSelf=self;
// 1
if(manager.deviceMotionAvailable){
    manager.deviceMotionUpdateInterval=0.01f;
    [manager startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue]
         withHandler:^(CMDeviceMotion*data,NSError*error){
             double rotation=atan2(data.gravity.x,data.gravity.y)-M_PI;
             weakSelf.imageView.transform=CGAffineTransformMakeRotation(rotation);
             }];
}
// 2
NSOperationQueue *queue = [[NSOperationQueue alloc] init];[manager startDeviceMotionUpdatesToQueue:queue
   withHandler:^(CMDeviceMotion *data, NSError *error) {
       
       [[NSOperationQueue mainQueue] addOperationWithBlock:^{
           double rotation=atan2(data.gravity.x,data.gravity.y)-M_PI;
           weakSelf.imageView.transform=CGAffineTransformMakeRotation(rotation);
       }];
   }];


CMDeviceMotion的使用