Swift CMMotion - 显示 phone 的角度
Swift CMMotion - Displaying the angle of the phone
所以我一直在寻找有关 Core Motion 的知识,并且正在努力寻找任何可以将其降低到初学者水平的东西。与其他主题相比,似乎没有太多内容。
我的目标:让 UILabel 显示 phone 所在的角度(我相信 z 轴),0 度表示 phone 是横向的。
我看到了this article from NSHipster that i think was sending me in the right ballpark, so i tried to recreate the code that is seen in the .gif in this article。它使 UIImageView 在 phone 倾斜时保持垂直....运气不好。我只是不明白所有这些代码片段是如何协同工作的,我想是因为这是我第一次玩 Core Motion。
我的问题:
1) 谁能为我重新创建那个 link 中看到的移动 imageView,这样我就可以研究它们是如何协同工作的(最好是整个 viewController.swift 文件)。
甚至更好
2) 将一些代码放在一起,显示 phone 所处的角度。
如果我可以更具体一点,请告诉我。
非常感谢!
更新:好吧,我对自己感到惊讶并设法解决了这个问题,然后来到这里 post 它和 TJ Shae 也 post 编辑了答案哈哈...花了很多时间在附近,但我现在正在路上。
import UIKit
import CoreMotion
class AngleGaugeViewController: UIViewController {
@IBOutlet weak var rotX: UILabel!
@IBOutlet weak var rotY: UILabel!
@IBOutlet weak var rotZ: UILabel!
@IBOutlet weak var theAngle: UILabel!
@IBOutlet weak var imageView: UIImageView!
let manager = CMMotionManager()
override func viewDidLoad() {
super.viewDidLoad()
manager.gyroUpdateInterval = 0.1
manager.accelerometerUpdateInterval = 0.1
manager.startGyroUpdates()
manager.startAccelerometerUpdates()
let queue = NSOperationQueue.mainQueue
manager.startGyroUpdatesToQueue(queue()) {
(data, error) in
// ...
}
if manager.deviceMotionAvailable {
manager.deviceMotionUpdateInterval = 0.01
manager.startDeviceMotionUpdatesToQueue(NSOperationQueue.mainQueue()) {
[weak self] data, error in
let rotation = atan2(data!.gravity.x, data!.gravity.y) - M_PI
self?.imageView.transform = CGAffineTransformMakeRotation(CGFloat(rotation))
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
好吧,NSHipster 代码对我来说第一次工作得很好 :] 这是 UIViewController 代码:
import UIKit
import CoreMotion
class ViewController: UIViewController {
@IBOutlet weak var imgView: UIImageView!
let manager = CMMotionManager()
override func viewDidLoad() {
super.viewDidLoad()
if manager.deviceMotionAvailable {
manager.deviceMotionUpdateInterval = 0.01
manager.startDeviceMotionUpdatesToQueue(NSOperationQueue.mainQueue()) {
(data, error) in
if let data = data {
let rotation = atan2(data.gravity.x, data.gravity.y) - M_PI
self.imgView.transform = CGAffineTransformMakeRotation(CGFloat(rotation))
}
}
}
}
}
所以我一直在寻找有关 Core Motion 的知识,并且正在努力寻找任何可以将其降低到初学者水平的东西。与其他主题相比,似乎没有太多内容。
我的目标:让 UILabel 显示 phone 所在的角度(我相信 z 轴),0 度表示 phone 是横向的。
我看到了this article from NSHipster that i think was sending me in the right ballpark, so i tried to recreate the code that is seen in the .gif in this article。它使 UIImageView 在 phone 倾斜时保持垂直....运气不好。我只是不明白所有这些代码片段是如何协同工作的,我想是因为这是我第一次玩 Core Motion。
我的问题:
1) 谁能为我重新创建那个 link 中看到的移动 imageView,这样我就可以研究它们是如何协同工作的(最好是整个 viewController.swift 文件)。
甚至更好
2) 将一些代码放在一起,显示 phone 所处的角度。
如果我可以更具体一点,请告诉我。 非常感谢!
更新:好吧,我对自己感到惊讶并设法解决了这个问题,然后来到这里 post 它和 TJ Shae 也 post 编辑了答案哈哈...花了很多时间在附近,但我现在正在路上。
import UIKit
import CoreMotion
class AngleGaugeViewController: UIViewController {
@IBOutlet weak var rotX: UILabel!
@IBOutlet weak var rotY: UILabel!
@IBOutlet weak var rotZ: UILabel!
@IBOutlet weak var theAngle: UILabel!
@IBOutlet weak var imageView: UIImageView!
let manager = CMMotionManager()
override func viewDidLoad() {
super.viewDidLoad()
manager.gyroUpdateInterval = 0.1
manager.accelerometerUpdateInterval = 0.1
manager.startGyroUpdates()
manager.startAccelerometerUpdates()
let queue = NSOperationQueue.mainQueue
manager.startGyroUpdatesToQueue(queue()) {
(data, error) in
// ...
}
if manager.deviceMotionAvailable {
manager.deviceMotionUpdateInterval = 0.01
manager.startDeviceMotionUpdatesToQueue(NSOperationQueue.mainQueue()) {
[weak self] data, error in
let rotation = atan2(data!.gravity.x, data!.gravity.y) - M_PI
self?.imageView.transform = CGAffineTransformMakeRotation(CGFloat(rotation))
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
好吧,NSHipster 代码对我来说第一次工作得很好 :] 这是 UIViewController 代码:
import UIKit
import CoreMotion
class ViewController: UIViewController {
@IBOutlet weak var imgView: UIImageView!
let manager = CMMotionManager()
override func viewDidLoad() {
super.viewDidLoad()
if manager.deviceMotionAvailable {
manager.deviceMotionUpdateInterval = 0.01
manager.startDeviceMotionUpdatesToQueue(NSOperationQueue.mainQueue()) {
(data, error) in
if let data = data {
let rotation = atan2(data.gravity.x, data.gravity.y) - M_PI
self.imgView.transform = CGAffineTransformMakeRotation(CGFloat(rotation))
}
}
}
}
}