2016-06-19 12 views
5

장치 동작 (피치, 롤, 요우)을 얻으려고 시도했지만 내 기능 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에 대해 설명합니다.

답변

6

motionManager가 viewDidLoad 메서드 실행을 마친 후에 손상되었을 수 있습니다. motionManager를 클래스 속성으로 만들어보십시오.

class ViewController: UIViewController { 
    var motionManager = CMMotionManager() 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     self.motionManager.startDeviceMotionUpdates() 

     ... 
+0

감사합니다. – Theilya