gpt4 book ai didi

ios - CoreMotion 无法在 Swift Playgrounds 中工作

转载 作者:行者123 更新时间:2023-11-29 05:54:58 24 4
gpt4 key购买 nike

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()

这(上面)是我用作 Playground 的代码。我直接从 iOS 应用程序获取代码,当我在同一设备(即 2018 年初运行 iOS 12 的 iPad)上运行 iOS 应用程序时,它可以完美运行。这是我用于实际应用程序的代码,它可以在iPad。它所做的只是将偏航值(以度为单位)写入文本框。当我运行它们中的任何一个时,我都没有收到错误或警告。 View Controller 确实显示在 Playground 中,初始标签文本也显示在 Playground 中,但是当我移动 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() 后保持 Playground 运行。所以尝试一下

PlaygroundPage.current.needsIndefiniteExecution = true 

更多关于 needsindefiniteExecution

关于ios - CoreMotion 无法在 Swift Playgrounds 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55255580/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com