加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

xcode – 运动管理器不工作

发布时间:2020-12-14 17:57:53 所属栏目:百科 来源:网络整理
导读:我试图让设备运动(俯仰,滚动,偏航),但是我的函数handleDeviceMotionUpdate没有启动(我在 iphone 5s上尝试它),这里是代码: import UIKitimport CoreMotionclass ViewController: UIViewController {override func viewDidLoad() { super.viewDidLoad() var m
我试图让设备运动(俯仰,滚动,偏航),但是我的函数handleDeviceMotionUpdate没有启动(我在 iphone 5s上尝试它),这里是代码:

import UIKit
import CoreMotion

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

        ...

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读