我有以下设置:
iPhone的显示器位于桌面的天花板上(alpha = 0度)。当iPhone向上移动时,如上图所示,alpha角度增加。
如何计算alpha角度的值而不处理可能发生变化的任何其他轴。我只对这一轴感兴趣。
如何从桌面抬起iPhone时获得正确的alpha角度?如何在alpha值发生变化时收到通知?
答案 0 :(得分:11)
You can use the CMMotionManager
class to monitor device motion changes.
Objective C
// Ensure to keep a strong reference to the motion manager otherwise you won't get updates
self.motionManager = [[CMMotionManager alloc] init];
if (self.motionManager.deviceMotionAvailable) {
self.motionManager.deviceMotionUpdateInterval = 0.1;
// For use in the montionManager's handler to prevent strong reference cycle
__weak typeof(self) weakSelf = self;
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[self.motionManager startDeviceMotionUpdatesToQueue:queue
withHandler:^(CMDeviceMotion *motion, NSError *error) {
// Get the attitude of the device
CMAttitude *attitude = motion.attitude;
// Get the pitch (in radians) and convert to degrees.
NSLog(@"%f", attitude.pitch * 180.0/M_PI);
dispatch_async(dispatch_get_main_queue(), ^{
// Update some UI
});
}];
NSLog(@"Device motion started");
}
else {
NSLog(@"Device motion unavailable");
}
Swift
// Ensure to keep a strong reference to the motion manager otherwise you won't get updates
motionManager = CMMotionManager()
if motionManager?.deviceMotionAvailable == true {
motionManager?.deviceMotionUpdateInterval = 0.1;
let queue = NSOperationQueue()
motionManager?.startDeviceMotionUpdatesToQueue(queue, withHandler: { [weak self] (motion, error) -> Void in
// Get the attitude of the device
if let attitude = motion?.attitude {
// Get the pitch (in radians) and convert to degrees.
// Import Darwin to get M_PI in Swift
print(attitude.pitch * 180.0/M_PI)
dispatch_async(dispatch_get_main_queue()) {
// Update some UI
}
}
})
print("Device motion started")
}
else {
print("Device motion unavailable");
}
NSHipster is (as always) a great source of information, and the article on CMDeviceMotion is no exception.
答案 1 :(得分:2)
>>> import iconv_codecs
>>> iconv_codecs.register('ansi_x3.110-1983')
>>> "foo".encode('ansi_x3.110-1983')