how to detect direction of shake in iphone

半世苍凉 提交于 2019-12-19 11:39:09

问题


I have an image of a bottle, whenever user shakes the device i want to move that image in that direction. like up, down, left or right.

For example: if user is shaking the device in left direction i want to move that image in left direction.

i can detect shake event using

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
if(event.subtype==UIEventSubtypeMotionShake)
{/*some code*/} }

but i dont know how to detect the direction.

i dont even if this is possible or not. please help me.


回答1:


A shake is typically a back and forth movement. Use the accelerometer data to detect a sudden movement in one particular direction (e.g. left) or a tilt towards a different orientation.




回答2:


You can get user motion values using CMMotionManager class.Please refer the following link

http://developer.apple.com/library/ios/#documentation/CoreMotion/Reference/CMMotionManager_Class/Reference/Reference.html#//apple_ref/occ/cl/CMMotionManager

Filter your acceleration and gyro dat detect direction of motion.



来源:https://stackoverflow.com/questions/6223296/how-to-detect-direction-of-shake-in-iphone

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