Question

I'm trying to get the rotation of the device in rubymotion. I added this piece of code in the viewDidLoad.

@motionManager = CMMotionManager.alloc.init
@motionManager.deviceMotionUpdateInterval = 1.0
if (@motionManager.isDeviceMotionAvailable)
  queue = NSOperationQueue.currentQueue

  @motionManager.startDeviceMotionUpdatesToQueue(queue, withHandler:lambda do |motion, error|
    NSLog "error = %@", error
    NSLog "rotation rate = [%f, %f, %f]", motion.rotationRate.x, motion.rotationRate.y, motion.rotationRate.z
  end)
else
  NSLog "Device Motion is not available: You're likely running this in a simulator"
end

But it always logs this:

rotation rate = [0.000000, 0.000000, 0.000000]

What is wrong with what I do?

Était-ce utile?

La solution

In RubyMotion, floats are actually objects, so you need to use %@ instead:

NSLog "rotation rate = [%@, %@, %@]", motion.rotationRate.x, motion.rotationRate.y, motion.rotationRate.z

If you wanted to format the numbers (e.g. to 3 decimal places), you would need to format the arguments separately:

NSLog "rotation rate = [%@, %@, %@]", "%.3f" % motion.rotationRate.x, "%.3f" % motion.rotationRate.y, "%.3f" % motion.rotationRate.z

See this twitter conversation with one of the RubyMotion developers.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top