分享一篇NSHipster的CMDeviceMotion
关于CoreMotion的一些基本用法,比较实在;
1,Adding the Accelerometer
Using the Accelerometer
UIImageViewRotationViewController * __weak weakSelf = self;
if (manager.accelerometerAvailable) {
manager.accelerometerUpdateInterval = 0.01f;
[manager startAccelerometerUpdatesToQueue:[NSOperationQueue mainQueue]
withHandler:^(CMAccelerometerData *data, NSError *error) {
double rotation = atan2(data.acceleration.x, data.acceleration.y) - M_PI;
weakSelf.imageView.transform = CGAffineTransformMakeRotation(rotation);
}];
}
CMAccelerometerDataxyz(0, -1, 0)(0, 0, -1)(0.707, -0.707, 0) of the and components from the accelerometer data and then using that rotation in a . Our image should stay right-side up no matter how the phone is turned—here it is in a hypothetical app for the National Air & Space Museum![]()
could
2,Adding the Gyroscope
startGyroUpdates...anddeviceMotionCMDeviceMotion
RotationViewController * __weak weakSelf = self;
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);
}];
}
UIClunkController
userAccelerationCMDeviceMotionuserObjective-C
* = ;
(.) {
. = 0.01f;
[ :[ ]
:^( *, *) {
(.. < -2.5fweakSelfnavigationControllerpopViewControllerAnimatedYES];
}
}];
}

浙公网安备 33010602011771号