2016-10-11 8 views
1

CMDeviceMotion 데이터에 액세스하는 데 문제가 있습니다. 필요한 것은 무엇이든 다 포함되어 있지만 startDeviceMotionUpdates 함수가 전달 된 것 같습니다 (처리기가 잘못되었다고 생각합니다). 여기 내 코드는 다음과 같습니다.CMDeviceMotion 데이터 액세스

let manager = CMMotionManager() 
     if manager.isDeviceMotionAvailable { 
      manager.startDeviceMotionUpdates() 
      manager.deviceMotionUpdateInterval = 0.1 
      manager.startDeviceMotionUpdates(to: OperationQueue.current!, withHandler: { 
        (data, error) -> Void in 
        self.digit.text = String (describing: data!.gravity.z) 
        self.digit2.text = String (describing: data!.gravity.y) 
        self.digit3.text = String (describing: data!.gravity.z) 
        }) 

digit, digit2 및 digit3은 중력 데이터를 기록하려는 편집 텍스트 필드입니다. 모든 것이 iPhone 6에서 테스트됩니다. - deviceMotion은 사용 가능하고 능동적입니다. startMotionUpdates 함수없이 데이터에 액세스 할 수 있었지만 NIL 값만 가져 왔습니다. 어떤 생각이 잘못 됐습니까? 감사!

답변

1

좋아, 알겠습니다. 스위프트 3.0에서 액세스 핵심 모션에 (내 경우에는 사용 CMDeviceMotion 클래스뿐만 아니라 CMGyroData 또는 CMAccelerometerData될 수 있습니다) :

let manager = CMMotionManager() 
if manager.isDeviceMotionAvailable { 
     manager.deviceMotionUpdateInterval = 0.05 //The lower the interval is, the faster motion data is read 
     manager.startDeviceMotionUpdates(to: OperationQueue.current!, withHandler: { 
      (motion: CMDeviceMotion?, Error) -> Void in 

      //Do whatever U want with data provided by CMDeviceMotion 

      } 
     }) 
    } 
    else{ 
     print("Core Motion access denied") 
    } 

그리고 데이터를 검색하는 중지하는 것을 잊지 마세요 코어 모션에서 더 이상 필요하지 않은 경우 예 :

ovveride func viewWillDisappear(_ animated: Bool) { 
manager.stopDeviceMotionUpdates() 
} 

기본적으로 내 주요 문제는 핸들러 구현. 희망이 도움이됩니다!

manager.startDeviceMotionUpdates() 

그것을 잘라 :

+0

우아한 솔루션! – RediOne1

+0

핸들러 구현과 관련이 없습니다. – matt

0

문제점은이 라인이다. 당신은 당신이 말할 때, 나중에 업데이트를 시작합니다 : 최대 당신을 엉망 이죠

manager.startDeviceMotionUpdates(to: //... 

모두을 말하고있다.

+0

자, 이제 알았습니다. U가 볼 수 있듯이, 나는 그것을 내 솔루션에서 잘라 냈습니다. –

+0

그리고 솔직히 말해서 나는이 선을 제거하기 전에 (manager.startDeviceMotionUpdates 만 남겨 두었습니다 (// : ...) 그리고 작동하지 않았더라도 ... –

+0

원래 코드를보고 있습니다. 여분의'manager.startDeviceMotionUpdates()'줄 이외에는 차이가 없습니다. – matt