如何使用“倾斜”功能iPhone 上的功能
我对此做了很多研究,但还没有找到答案。
基本上,我正在使用 Objective-C 为 iPhone 制作一个掷骰子应用程序。我希望能够使用“倾斜”功能来掷骰子。例如,如果用户将其设备保持与地面水平,骰子就会沉降。但是,如果用户随后将其设备向右倾斜,所有骰子都会向右“滚动”,直到达到极限。
怎样才能利用这个功能呢?而且,如果可能的话,我想将角度存储在整数、浮点、双精度或 NSString 中,这样我就可以有效地使用它,并轻松测试应用程序。
I have been doing a lot if research into this, but I havent found an answer.
Basically, I'm making a dice rolling application for the iPhone in Objective-C. I want to be able to use the "tilt" feature to roll the dice. For example, if the user holds his device level to the ground, the dice will settle. But, if the user then tilts his device to the right, all of dice will "roll" to the right until they reach their limit.
How can make use of this feature in that way? And, if possible, I want to store the angles in an integer, float, double, or NSString, so I can use it efficiently, and test the app easily.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过加速度计来做到这一点。当您倾斜设备时,加速度计将为您提供 xyz 轴值。对此添加一个 UIAccelerometer 类类型的属性。
现在您定义频率和代表。您应该从要启动接收的位置编写此代码。
现在您必须添加委托方法
好的,现在您已经以频率 50 [意味着 20 毫秒] 频繁更新该值。
现在,如果您想停止接收这些值,显然,如果您离开此视图,您应该停止接收,如下所示:
您可以再次开始接收加速度计数据,将委托分配给自己,
如果您还有任何其他问题,可以 ping 我Skype 上的 Behestee
You can do this by accelerometer. Accelerometer will give you the value for x y z axis value when you tilt the device. to this add a property which is type of UIAccelerometer class.
now you define the frequency and the delegate. you should write this code from where you want to initiate receiving.
Now you have to add the delegation method
Ok, now you have the value updated frequently at frequency 50 [means 20 mili second].
Now, if you want to stop receiving these values and obviously you should stop receiving if you leave this view and that will as follows:
Again you can start receiving accelerometer data again assigning the delegate to self
if you have any further question you can ping me behestee on skype
最好有最新的官方指南:
http://developer.apple.com /library/ios/#documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/MotionEvents/MotionEvents.html
上面将向您展示如何获取一些原始数据;它不会执行实现“滚动”模拟所需的所有计算。
An official, up-to-date guide is preferable:
http://developer.apple.com/library/ios/#documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/MotionEvents/MotionEvents.html
The above will show you how to get some raw data; it will not do all the calculations required to achieve your "rolling" simulation.