ios - 通过倾斜设备移动节点

标签 ios iphone objective-c ios7

我想通过左右倾斜我的设备来移动一个球体。我现在拥有的代码正在运行,除了我必须上下倾斜设备 - 当我声明 data.acceleration.y --> Y 轴?

这是我的代码:

 motionManager = [[CMMotionManager alloc] init];
if ([motionManager isAccelerometerAvailable] == YES) {
    [motionManager startAccelerometerUpdatesToQueue:[[NSOperationQueue alloc] init]
                                        withHandler:^(CMAccelerometerData *data, NSError *error)
     {
         float destX, destY;
         float currentX = sphereNode.position.x;
         float currentY = sphereNode.position.y;
         BOOL shouldMove = NO;

         if(data.acceleration.y < -0.25) { // tilting the device to the right
             destX = currentX + (data.acceleration.y * kPlayerSpeed);
             destY = currentY;
             shouldMove = YES;
         } else if (data.acceleration.y > 0.25) { // tilting the device to the left
             destX = currentX + (data.acceleration.y * kPlayerSpeed);
             destY = currentY;
             shouldMove = YES;
         }
         if(shouldMove) {
             SKAction *action = [SKAction moveTo:CGPointMake(destX, destY) duration:1];
             [sphereNode runAction:action];
         }
     }];
}

非常感谢任何帮助!

最佳答案

Y 轴确实与设备的顶部和底部对齐。您很可能正在寻找 CMAccelerometerData 数据对象的 x 轴。

这一切都在 Event handling guide for iOS : Motion Events 中进行了解释文档。

也许看看一些教程(例如 iOS Programming Recipe 19: Using Core Motion to Access Gyro and Accelerometer )也可以帮助您。

关于ios - 通过倾斜设备移动节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21832506/

相关文章:

ios - UIWebview 未获取链接的 CSS 资源

ios - React-Native navigation.addListener 不是函数

objective-c - 如何确定 NSString 中子字符串的顺序?

sql - iOS SQLite 搜索。速度问题

ios - 使用iOS CIDetector人脸检测触发功能

ios - 解析 .deleteInBackground 不起作用(Swift)

iphone - AGImagePickerController 难度

iPhone 和 iPad - 可以在 Xcode 上更改产品名称吗?

ios - 如何使用 "-"关键字拆分字符串?

objective-c - 使用 Cocoa 自动化浏览器任务?