【问题标题】:how to code for gyroActive or gyroAvailable in watchKit in swift language?如何以swift语言在watchKit中为gyroActive或gyroAvailable编码?
【发布时间】:2015-10-26 06:37:59
【问题描述】:

我已经阅读了下面关于 gyroAvailable 在 watchKit 中的 swift 语言文章。

https://developer.apple.com/library/watchos/documentation/CoreMotion/Reference/CMMotionManager_Class/index.html#//apple_ref/occ/instp/CMMotionManager/gyroActive

但我不知道如何编写代码以及在哪里编写(哪个文件)?

任何帮助将不胜感激。

【问题讨论】:

    标签: ios xcode swift watchkit core-motion


    【解决方案1】:

    您可以将陀螺仪代码添加到手表扩展中的任何类中,但您可能希望将其添加到 WKInterfaceController 的子类中

    这里希望实现可能如下所示:

    import WatchKit
    import CoreMotion    
    
    class GyroInterfaceController: WKInterfaceController {
    
            let motionManager = CMMotionManager()
    
            override func awakeWithContext(context: AnyObject?) {
                super.awakeWithContext(context)
                motionManager.gyroUpdateInterval = 0.1
            }
    
            override func willActivate() {
                super.willActivate()
    
                if (motionManager.gyroAvailable == true) {
                    motionManager.startGyroUpdatesToQueue(NSOperationQueue.currentQueue()!, withHandler: { (data, error) -> Void in
                        guard let data = data else { return }
                        let rotationX = data.rotationRate.x
                        let rotationY = data.rotationRate.y
                        let rotationZ = data.rotationRate.z
                        // do you want to want to do with the data
                    })
                } else {
                    print("Gyro not available")
                }
            }
    
            override func didDeactivate() {
                super.didDeactivate()
                motionManager.stopGyroUpdates()
            }
        }
    

    理论上这应该可行。如果您尝试一下,您会看到它只会打印(“陀螺仪不可用”)。陀螺仪数据似乎还无法访问。让我们希望 Apple 能够在不久的将来实现它。

    与此同时,您至少可以使用加速度计中的数据。那是可以访问的。代码看起来很相似:

    import WatchKit
    import CoreMotion
    
    class AccelerometerInterfaceController: WKInterfaceController {
    
        let motionManager = CMMotionManager()
    
        override func awakeWithContext(context: AnyObject?) {
            super.awakeWithContext(context)
            motionManager.accelerometerUpdateInterval = 0.1
        }
    
        override func willActivate() {
            super.willActivate()
    
            if (motionManager.accelerometerAvailable == true) {
                motionManager.startAccelerometerUpdatesToQueue(NSOperationQueue.currentQueue()!, withHandler: { (data, error) -> Void in
                    guard let data = data else { return }
                    let accelerationX = data.acceleration.x
                    let accelerationY = data.acceleration.y
                    let accelerationZ = data.acceleration.z
                    // do you want to want to do with the data
                })
            }
        }
    
        override func didDeactivate() {
            super.didDeactivate()
            motionManager.stopAccelerometerUpdates()
        }
    }
    

    当然,这只适用于 Apple Watch,不适用于模拟器。

    【讨论】:

    • 谢谢你.....回复很有帮助......我能够从加速度计获得原始数据,但正如你所说,不是来自陀螺仪......
    【解决方案2】:

    我试过这个并寻找一些时间来寻找答案,解决方案将是使用 MotionUpdates。

    motionManager.startDeviceMotionUpdates(to: OperationQueue.current!) { (data, error) in
                guard error == nil else { return }
                guard let allData = data else { return }
    print(allData)// it will return all -> ACC, Gyro,...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多