如何将 DeviceMotion 功能添加到 Swift Playground? [英] How Can I Add DeviceMotion Capabilities to a Swift Playground?

查看:42
本文介绍了如何将 DeviceMotion 功能添加到 Swift Playground?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Swift 游乐场工作,我正在尝试使用此代码来获取设备运动.

I am working on a Swift playground and I am trying to use this code to get the device motion.

@objc func update()
{
    if let deviceMotion = motionManager.deviceMotion {
        print("Device Motion Yaw: \(deviceMotion.attitude.yaw)")
    }
}

然而,似乎设备运动在 Swift 游乐场上不起作用,即使它在 iOS 中工作.我将如何更改游乐场以支持设备运动?我正在使用运行 iOS 12 的 iPad 和最新版本的 Swift Playgrounds 以及用于代码的 Mac.我知道这个方法被完美调用,当我把它作为 iOS 应用程序的一部分放在 iPad 和 iPhone 上时,代码运行得很好.我将如何修改 Playground 以支持这一点,据我所知,默认情况下它不会?

However, it seems that device motion does not work on a Swift playground even though it works in iOS. How would I change a playground to support device motion? I am using an iPad running iOS 12 and the latest version of Swift Playgrounds and a Mac for the code. I know that the method gets called perfectly, and the code runs perfectly when I put it as part of an iOS app on both an iPad and an iPhone. How would I modify a playground to support this, as from my understanding it does not by default?

推荐答案

完全有可能.我已经做过好几次了.你需要一个 CMMotionManager 类.有很多方法可以做到这一点,但我建议使用计时器.下面是一些示例代码,摘自 Apple 的开发人员文档 并进行了修改以适应问题.

It is entirely possible. I’ve done it on several occasions. You’ll need a CMMotionManager class. There are many ways to do this, but I would recommend using a timer. Here is some example code, taken from Apple’s developer documentation and modified to fit the question.

let motion = CMMotionManager()
func startDeviceMotion() {
    if motion.isDeviceMotionAvailable {
        //How often to push updates
        self.motion.deviceMotionUpdateInterval = 1.0/60.0
        self.motion.showsDeviceMovementDisplay = true
        self.motion.startDeviceMotionUpdates(using: .xMagneticNorthZVertical)

        // Configure a timer to fetch the motion data.
        self.timer = Timer(fire: Date(), interval: (1.0 / 60.0), repeats: true,
                           block: { (timer) in
                            if let data = self.motion.deviceMotion {      
let x = data.attitude.pitch
let y = data.attitude.roll
let z = data.attitude.yaw
//Use the data                                
                            }
        })
        RunLoop.current.add(self.timer!, forMode: RunLoop.Mode.default)
    }
}
startDeviceMotionUpdates()

要么这样做,要么尝试类似的方法,同样来自文档

Either do that or try something like this, also from the documentation

func startQueuedUpdates() {
   if motion.isDeviceMotionAvailable {       self.motion.deviceMotionUpdateInterval = 1.0 / 60.0
      self.motion.showsDeviceMovementDisplay = true
      self.motion.startDeviceMotionUpdates(using: .xMagneticNorthZVertical, 
               to: self.queue, withHandler: { (data, error) in
         // Make sure the data is valid before accessing it.
         if let validData = data {
            // Get the attitude relative to the magnetic north reference frame. 
            let roll = validData.attitude.roll
            let pitch = validData.attitude.pitch
            let yaw = validData.attitude.yaw

            // Use the motion data in your app.
         }
      })
   }
}

这篇关于如何将 DeviceMotion 功能添加到 Swift Playground?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆