How can I differentiate a Shake vs a Tilt on the iPhone

≯℡__Kan透↙ 提交于 2019-12-06 12:39:08

问题


I have a page UIScrollView that will scroll 1 page left or right depending on the tilt of the phone.

I would also like to implement "Shake for a random page", but cannot figure out how to do the logic for differentiating a shake motion and a x-axis tilt.

Can these two motions be used in conjunction with one another? I don't need anything complicated like shaking while tilting, I just want both to work independently of one another.


回答1:


To detect a "shake" you would check the g-force generated. It will be higher for a shake vs a tilt.

How do I detect when someone shakes an iPhone?

You can definitely use both at the same time, just check the g-force for a high enough value to indicate a shake (> 1g - adjust to get the sensitivity to your liking), then if no shake is detected fall through to the tilt checking.




回答2:


The iPhone Application Programming Guide offers code samples for detecting gravity (tilt) and sudden acceleration (shakes)




回答3:


A tilt should never exceed 1g, while a shake can easily do so.



来源:https://stackoverflow.com/questions/1019665/how-can-i-differentiate-a-shake-vs-a-tilt-on-the-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!