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); }]; }];
时间: 2024-11-06 03:30:37