简体   繁体   English

使用NSTimer在屏幕上移动对象

[英]using NSTimer to move objects on screen

I am building a simple ios gaming app where I have to move a few objects on screen, and these moving objects react to swipe gestures and change their direction based on that. 我正在构建一个简单的ios游戏应用程序,其中我必须在屏幕上移动一些对象,并且这些移动的对象会对滑动手势做出反应,并据此更改其方向。 I am not using spritekit or any other gaming framework for this, and the way I am moving the objects is by triggering an NSTimer that fires 60 times a second. 我没有为此使用spritekit或任何其他游戏框架,而移动对象的方式是通过触发每秒触发60次的NSTimer。 I am not using any threads explicitly so I guess the timer is executed by the main thread ? 我没有显式使用任何线程,所以我猜计时器是由主线程执行的吗? The code block that the timer triggers updates the location of objects based on their speed (defined as pixels per time unit in x/y direction). 计时器触发的代码块根据对象的速度(定义为x / y方向上每个时间单位的像素)更新对象的位置。 It is working fine and I am almost at the end of my project, but while testing the app I've realized that although the movement of objects seem pretty good most of the times, however sometimes the movement is a little rough/patchy. 一切正常,我快要完成项目了,但是在测试应用程序时,我意识到尽管大多数情况下对象的运动看起来都不错,但是有时运动还是有些粗糙/斑驳。 My question is that is it the right way to do something like this, how can I improve the performance ? 我的问题是做这样的事情是正确的方法,如何改善性能? will using something like GCD can help with the issue of jittery movement ? 使用GCD之类的方法可以解决抖动的问题吗? (I haven't looked into GCD in detail but I am willing to spend time on it if I know that will help) (我没有详细研究GCD,但如果我知道会有所帮助,我愿意花时间在它上面)

Thanks for any help ! 谢谢你的帮助 !

and the way I am moving the objects is by triggering an NSTimer that fires 60 times a second 我移动对象的方式是触发NSTimer,每秒触发60次

I would recommend not doing that. 我建议不要这样做。 Various forms of animation are built in (View animation, Layer animation, CADisplayLink, UIKit Dynamics), and of course there's always Sprite Kit. 内置了各种形式的动画(查看动画,图层动画,CADisplayLink,UIKit Dynamics),当然总会有Sprite Kit。 Use the tools you are given for this purpose. 使用为此目的提供的工具。 Don't reinvent the unicycle when you already have a Ducati at your disposal. 当您已经拥有杜卡迪时,不要重塑独轮车。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM