xcode – 运动管理器不工作

xcode – 运动管理器不工作,第1张

概述我试图让设备运动(俯仰,滚动,偏航),但是我的函数handleDeviceMotionUpdate没有启动(我在 iphone 5s上尝试它),这里是代码: import UIKitimport CoreMotionclass ViewController: UIViewController {override func viewDidLoad() { super.viewDid 我试图让设备运动(俯仰,滚动,偏航),但是我的函数handleDeviceMotionUpdate没有启动(我在 iphone 5s上尝试它),这里是代码:

import UIKitimport CoreMotionclass VIEwController: UIVIEwController {overrIDe func vIEwDIDLoad() {    super.vIEwDIDLoad()    var motionManager = cmmotionmanager()    motionManager.startDeviceMotionUpdates()    motionManager.deviceMotionUpdateInterval = 0.1    motionManager.startDeviceMotionUpdatesToQueue(        NSOperationQueue.currentQueue()!,withHandler: {            (deviceMotion,error) -> VoID in            if(error == nil) {                self.DeviceMotionUpdate(deviceMotion!)            } else {                print("error")            }    })}overrIDe func dIDReceiveMemoryWarning() {    super.dIDReceiveMemoryWarning()}func DeviceMotionUpdate(deviceMotion:CMDeviceMotion) {    print("function launched")    var attitude = deviceMotion.attitude    var roll = (attitude.roll)    var pitch = (attitude.pitch)    var yaw = (attitude.yaw)    print("Roll: \(roll),Pitch: \(pitch),Yaw:  (yaw)")}}

我没有在startDeviceMotionUpdatesToQueue中得到错误.我在motionManager.deviceMotionActive和motionManager.deviceMotionAvailable上都是如此

解决方法 完成运行vIEwDIDLoad方法后,您的motionManager可能会被销毁.尝试使motionManager成为一个类属性.

class VIEwController: UIVIEwController {    var motionManager = cmmotionmanager()    overrIDe func vIEwDIDLoad() {        super.vIEwDIDLoad()        self.motionManager.startDeviceMotionUpdates()        ...
总结

以上是内存溢出为你收集整理的xcode – 运动管理器工作全部内容,希望文章能够帮你解决xcode – 运动管理器不工作所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1025105.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-23
下一篇2022-05-23

发表评论

登录后才能评论

评论列表(0条)

    保存