如何确定 iPhone 晃动的方向
我正在使用加速度计滚动 UIScrollVIew 中的多个子视图。 我希望当用户向右滑动 iPhone 时视图(纵向)向右滚动,并在向左滑动设备时向左滚动。
我认为我可以通过记下正或负 x 加速度值来做到这一点,但我发现这些值通常是正值和负值的混合。 我已将下限设置为 1.5g,以消除无震动运动,并查看 0.5 秒持续时间内的 x 值。
我确信有一种三角方法可以确定轻弹的整体方向,并且您必须在轻弹运动的持续时间内测量值。 我也确信有人已经解决了这个问题。
有什么想法吗?
谢谢
I am using the accelerometer to scroll multiple subViews in a UIScrollVIew. I want the view (portrait orientation) to scroll to the right when the user flicks the iPhone to the right, and scroll to the left when the device is flicked to the left.
I thought I could do that just by noting positive or negative x acceleration values, but I see that the values are usually a mixture of positive and negative values. I have set the floor at 1.5g to eliminate non-shake movement, and am looking at the x values over the duration of .5 seconds.
I'm sure there is a trigonometrical method for determining the overall direction of a flick, and that you have to measure values over the duration of the flick motion. I'm also sure that someone has already figured this one out.
Any ideas out there?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我开发了一个解决方案,它比建议的解决方案给了我更好的反馈(仅适用于左右摇动)。
我在这里所做的方式非常敏感(识别轻微的震动),但是可以通过更改 tresholdFirstMove 和 tresholdBackMove 来调整灵敏度(增加以降低灵敏度)
在 Swift 中:
(在你的 viewController 中。并添加“import CoreMotion”)
I developed a solution which gives me better feedback than the proposed solution (only for left and right shake).
The way I did it here is quite sensitive (recognizes a small shake), but sensitivity can be adapted by changing tresholdFirstMove and tresholdBackMove (increase for lower sensitivity)
In Swift :
(in your viewController. And add "import CoreMotion")
好的,想出了解决办法。 当我检测到摇晃运动(x 轴上的加速度大于 1.5)时,我启动计时器并将 BOOL 设置为 true。 当 BOOL 为 true 时,我添加加速度值。 当计时器到期时,我停止添加加速度值并通过总加速度的符号确定震动的方向。
注意:我无法得到 < 和> 符号在上面的代码块中正确格式化,因此我用 isLessThan 和 isGreaterThan 替换了符号。
OK, worked out a solution. When I detect a shake motion (acceleration greater than 1.5 on the x axis), I start a timer and set a BOOL to true. While the BOOL is true I add acceleration values. When the timer expires, I stop adding acceleration values and determine direction of the shake by the sign of the total acceleration.
Note: I couldn't get the < and > symbols to format correctly in the codeblock above, so I substituted isLessThan and isGreaterThan for the symbols.