CoreMotion无法在rubymotion中工作

时间:2014-03-19 02:49:35

标签: ios rubymotion core-motion

我正试图在rubymotion中获取设备的旋转。我在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

但它总是记录下来:

rotation rate = [0.000000, 0.000000, 0.000000]

我的工作有什么问题?

1 个答案:

答案 0 :(得分:0)

在RubyMotion中,浮点数实际上是对象,因此您需要使用%@代替:

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

如果你想格式化数字(例如3个小数位),你需要分别格式化参数:

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

请参阅此twitter conversation with one of the RubyMotion developers