gpt4 book ai didi

ios - 运动管理器在 Swift 中不起作用

转载 作者:IT王子 更新时间:2023-10-29 05:33:53 27 4
gpt4 key购买 nike

我尝试在 Swift 中使用运动管理器,但我的更新 block 中的日志从未打印出来。

    var motionManager: CMMotionManager = CMMotionManager()
motionManager.accelerometerUpdateInterval = 0.01
println(motionManager.deviceMotionAvailable) // print true
println(motionManager.deviceMotionActive) // print false
motionManager.startDeviceMotionUpdatesToQueue(NSOperationQueue.currentQueue(), withHandler:{
deviceManager, error in
println("Test") // no print
})

println(motionManager.deviceMotionActive) // print false

我的 Objective-C 实现工作正常。有谁知道为什么我的更新 block 没有被调用?

最佳答案

那是因为当方法返回时运动管理器实例被抛出。你应该在你的类上创建一个属性来包含运动管理器。此外,看起来您只是在更改马槽的 accelerometerUpdateInterval,然后监视设备运动变化。您应该改为设置 deviceMotionUpdateInterval 属性。

import CoreMotion

class ViewController: UIViewController {
let motionManager = CMMotionManager()

override func viewDidLoad() {
super.viewDidLoad()

motionManager.deviceMotionUpdateInterval = 0.01
motionManager.startDeviceMotionUpdates(to: OperationQueue.current!) { deviceManager, error in
print("Test") // no print
}

print(motionManager.isDeviceMotionActive) // print false
}
}

关于ios - 运动管理器在 Swift 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24114164/

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