2

我已经阅读了许多关于如何检测震动的帖子,但我不确定:

  1. 目前检测抖动的最佳方法是什么?

    • 和 -
  2. 如何仅在用户摇晃 iPhone 时播放音频文件?

有人有建议、示例/教程或示例代码吗?

谢谢

4

3 回答 3

3

您可以在控制器(或实际上的任何 UIResponder ......)中实现以下类似的东西。这些在 3.0 及更高版本中可用。如果你不想做高级的东西,你不必降低到加速度计的水平,这取决于你想要从震动中获得多少细节。

- (void)viewDidAppear:(BOOL)animated {
    [self becomeFirstResponder];
}

- (BOOL)canBecomeFirstResponder {
    return YES;
}

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    if (motion==UIEventSubtypeMotionShake) {
        if ([self paused]) {
            [self play];
        }
    }
}

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    if (motion==UIEventSubtypeMotionShake) {
        if ([self playing]) {
            [self pause];
        }
    }
}

- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    if (motion==UIEventSubtypeMotionShake) {
        if ([self playing]) {
            [self pause];
        }
    }
}
于 2010-04-03T09:02:14.373 回答
2

Use the UIAccelerometer and implement the UIAccelerometerDelegate protocol in your class. Perhaps you could do something like this:

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acc {
  float twoGs = 2.0;
  if ((acc.x * acc.x) + (acc.y * acc.y) + (acc.z * acc.z) > twoGs * twoGs) {
    if ([self paused]) {
      [self play];
    }
  } else {
    if ([self playing]) {
      [self pause];
    }
  }
}

for suitable implementations of the pause, paused, play, playing selectors.

于 2010-04-03T07:07:11.613 回答
0

这在之前已经回答过。我正在使用那里提出的代码的修改版本,效果很好。请注意,在即将发布的操作系统版本中,您将能够通过标准 API 检测摇晃手势。

于 2010-04-03T08:57:57.057 回答