[英]Determine the Device Orientation and its resulting Angle on one Dimension?
I have the following setup: 我有以下设置:
An iPhone lies with the display to the ceiling on a table (alpha = 0 degrees). iPhone将显示器放在桌子上的天花板上(alpha = 0度)。 When the iPhone is moved upwards like shown in the image above the alpha angle increases.
当iPhone向上移动时,如上图所示,alpha角度增加。
How do I compute the value of the alpha angle without taking care of any other axes which could change. 如何在不考虑可能发生变化的其他轴的情况下计算α角的值。 I am only interested in this one axis.
我只对这一轴感兴趣。
How do I get the correct alpha angle the iPhone has when lifting up from the table? 如何从桌子上抬起iPhone时获得正确的alpha角度? How do I get notified when the value of alpha changes?
当alpha的值发生变化时,如何收到通知?
You can use the CMMotionManager
class to monitor device motion changes. 您可以使用
CMMotionManager
类来监视设备运动更改。
Objective C 目标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. NSHipster (一如既往)是一个很好的信息来源,关于CMDeviceMotion的文章也不例外。
Swift 4: 斯威夫特4:
let motionManager = CMMotionManager()
if motionManager.isDeviceMotionAvailable {
motionManager.deviceMotionUpdateInterval = 0.1
motionManager.startDeviceMotionUpdates(to: OperationQueue()) { [weak self] (motion, error) -> Void in
if let attitude = motion?.attitude {
print(attitude.pitch * 180 / Double.pi)
DispatchQueue.main.async{
// Update UI
}
}
}
print("Device motion started")
}
else {
print("Device motion unavailable")
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.