繁体   English   中英

CoreMotion无法在Swift Playgrounds中运行

[英]CoreMotion not working within Swift Playgrounds

import UIKit
import ARKit
import PlaygroundSupport
import SceneKit
import CoreMotion

class MyViewController: UIViewController {

let motionManager = CMMotionManager()
var timer: Timer!
var label = UILabel(frame: CGRect(x: 50, y: 400, width: 400, height: 100))

override func loadView() {

    let view = UIView()

    view.addSubview(label)
    label.text = "Show Yaw Here"
    self.view = view
    view.backgroundColor = .white

    motionManager.startAccelerometerUpdates()
    motionManager.startGyroUpdates()
    motionManager.startMagnetometerUpdates()
    motionManager.startDeviceMotionUpdates()

    timer = Timer.scheduledTimer(timeInterval: 0.2, target: self, selector: #selector(MyViewController.update), userInfo: nil, repeats: true)
}

@objc func update() {
    self.label.text = "Obama"
    if let accelerometerData = motionManager.accelerometerData {
        print("Device Accelerometer: \(accelerometerData)")
    }
    if let gyroData = motionManager.gyroData {
        print("Device Gyro: \(gyroData)")
    }
    if let magnetometerData = motionManager.magnetometerData {
        print("Device Magnetometer: \(magnetometerData)")
    }
    if let deviceMotion = motionManager.deviceMotion {
        print("Device Motion: \(deviceMotion)")
        let yaw = (Int)(Double(floor(1000*deviceMotion.attitude.yaw)/1000) * 180.0/Double.pi)
        self.label.text = "Yaw: \(yaw) Degrees"
    }
}
}
PlaygroundPage.current.liveView = MyViewController()

这(上图)是我用作游乐场的代码。 我直接从iOS应用程序获得了代码,当我在同一台设备上运行iOS应用程序时,它运行得很好,这是一台从2018年初开始运行iOS 12的iPad。这是我用于实际应用程序的代码,完全适用于iPad兼容。 它只是将Yaw值以度为单位写入文本框。 当我运行其中任何一个时,我都不会收到错误或警告。 视图控制器确实显示在操场上,初始标签文本也是如此,但是当我移动iPad时,代码不会更新设备动作,而是在应用版本中更新。

import UIKit
import CoreMotion

class ViewController: UIViewController {

let motionManager = CMMotionManager()
var timer: Timer!
@IBOutlet weak var label: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

    label.text = "ayy lmao"

    motionManager.startAccelerometerUpdates()
    motionManager.startGyroUpdates()
    motionManager.startMagnetometerUpdates()
    motionManager.startDeviceMotionUpdates()

    timer = Timer.scheduledTimer(timeInterval: 0.2, target: self, selector: #selector(ViewController.update), userInfo: nil, repeats: true)
}

@objc func update() {
    if let accelerometerData = motionManager.accelerometerData {
        print("Device Accelerometer: \(accelerometerData)")
    }
    if let gyroData = motionManager.gyroData {
        print("Device Gyro: \(gyroData)")
    }
    if let magnetometerData = motionManager.magnetometerData {
        print("Device Magnetometer: \(magnetometerData)")
    }
    if let deviceMotion = motionManager.deviceMotion {
        print("Device Motion: \(deviceMotion)")
        let yaw = (Int)(deviceMotion.attitude.yaw * 180.0/Double.pi)
        self.label.text = "Yaw: \(yaw) Degrees"
    }
}
}

核心位置API是异步的,因此在调用startUpdatingLocation()之后必须保持游乐场运行。 所以给它一个旋转

PlaygroundPage.current.needsIndefiniteExecution = true 

更多关于needsindefiniteExecution

Core Motion框架适用于iPad上的Swift Playgrounds应用程序,适用于除Device Motion之外的所有服务。 StartDeviceMotionUpdates(to:){}无法提供任何更新。 可悲的是,我没有找到解释为什么这应该是! 当然,GPS只能通过CoreLocation访问,并且需要应用程序无法进行安全验证,因此无法访问GPS数据。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM