如何使用“倾斜”功能iPhone 上的功能

发布于 2024-10-19 18:56:16 字数 244 浏览 1 评论 0原文

我对此做了很多研究,但还没有找到答案。

基本上,我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

却一份温柔 2024-10-26 18:56:16

您可以通过加速度计来做到这一点。当您倾斜设备时,加速度计将为您提供 xyz 轴值。对此添加一个 UIAccelerometer 类类型的属性。

    UIAccelerometer*  theAccelerometer;

现在您定义频率和代表。您应该从要启动接收的位置编写此代码。

    theAccelerometer = [UIAccelerometer sharedAccelerometer];
    theAccelerometer.updateInterval = 1 / 50;
    theAccelerometer.delegate = self;

现在您必须添加委托方法

    -(void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration{
        myX = acceleration.x;
        myY = acceleration.y;
        myZ = acceleration.z;
    }

好的,现在您已经以频率 50 [意味着 20 毫秒] 频繁更新该值。
现在,如果您想停止接收这些值,显然,如果您离开此视图,您应该停止接收,如下所示:

    theAccelerometer.delegate = nil

您可以再次开始接收加速度计数据,将委托分配给自己,

    theAccelerometer.delegate = self;

如果您还有任何其他问题,可以 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.

    UIAccelerometer*  theAccelerometer;

now you define the frequency and the delegate. you should write this code from where you want to initiate receiving.

    theAccelerometer = [UIAccelerometer sharedAccelerometer];
    theAccelerometer.updateInterval = 1 / 50;
    theAccelerometer.delegate = self;

Now you have to add the delegation method

    -(void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration{
        myX = acceleration.x;
        myY = acceleration.y;
        myZ = acceleration.z;
    }

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:

    theAccelerometer.delegate = nil

Again you can start receiving accelerometer data again assigning the delegate to self

    theAccelerometer.delegate = self;

if you have any further question you can ping me behestee on skype

只等公子 2024-10-26 18:56:16

最好有最新的官方指南:
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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文