2015-07-16 9 views
5

पर सबसे संभावित संभव गति है या नहीं, मैं यह निर्धारित करने के लिए काम कर रहा हूं कि आईफोन 6 कब चल रहा है (सबसे छोटा संभव कदम भी हिला नहीं!) किसी भी दिशा में (एक्स, वाई या जेड)। इसे प्राप्त करने का सबसे अच्छा तरीका क्या है?आईफोन 6 डिवाइस की गति का पता लगाने के लिए कैसे? (यह निर्धारित करें कि आईफोन डिवाइस एक्स, वाई, जेड-)

+0

क्या अपने प्रश्न इस कोड के साथ यहाँ है ? –

+0

मुझे यह जानने की ज़रूरत है कि यह सबसे अच्छा तरीका है या बेहतर विकल्प हैं – Alaa

उत्तर

6

मैं इस कोड का इस्तेमाल किया है और पाया है कि यह उपयोगी है, यह चार कार्य शामिल हैं: - शुरू गति प्रबंधक - गति प्रबंधक बंद करो - अद्यतन गति प्रबंधक - magnitudeFromAttitude

import CoreMotion 
let motionManager: CMMotionManager = CMMotionManager() 
var initialAttitude : CMAttitude! 

//start motion manager 
func StartMotionManager() { 
    if !motionManager.deviceMotionActive { 
     motionManager.deviceMotionUpdateInterval = 1 
    motionManager.startDeviceMotionUpdates() 
    } 
} 
//stop motion manager 
func stopMotionManager() 
{ 
    if motionManager.deviceMotionActive 
    { 
motionManager.stopDeviceMotionUpdates() 
    } 
} 

//update motion manager 
func updateMotionManager (var x : UIViewController) 
{ 

    if motionManager.deviceMotionAvailable { 
     //sleep(2) 
     initialAttitude = motionManager.deviceMotion.attitude 
     motionManager.startDeviceMotionUpdatesToQueue(NSOperationQueue.currentQueue(), withHandler:{ 
      [weak x] (data: CMDeviceMotion!, error: NSError!) in 

      data.attitude.multiplyByInverseOfAttitude(initialAttitude) 

      // calculate magnitude of the change from our initial attitude 
      let magnitude = magnitudeFromAttitude(data.attitude) ?? 0 
      let initMagnitude = magnitudeFromAttitude(initialAttitude) ?? 0 

      if magnitude > 0.1 // threshold 
      { 
       // Device has moved ! 
       // put the code which should fire upon device moving write here 

       initialAttitude = motionManager.deviceMotion.attitude 
      } 
         }) 

     println(motionManager.deviceMotionActive) // print false 
    } 

} 


// get magnitude of vector via Pythagorean theorem 
func magnitudeFromAttitude(attitude: CMAttitude) -> Double { 
    return sqrt(pow(attitude.roll, 2) + pow(attitude.yaw, 2) + pow(attitude.pitch, 2)) 
} 
संबंधित मुद्दे