NSTimers VS NSThreading

发布于 2024-12-06 07:51:55 字数 171 浏览 2 评论 0原文

我正在开发一款 iPhone 游戏,玩家通过倾斜 iPhone 来让角色移动,但不幸的是,我用来制作场景动画的所有计时器都会减慢我的游戏速度。有人告诉我使用 NSThreads,但我真的对它们一无所知。我的问题是,使用 NSThreads 和 NSTimers 有什么区别?或者说使用NSThreads有什么好处?它们有什么用?

I'm working on an iPhone Game where the player tilts the iPhone to make the character move, but unfortunately all of the timers I'm using to animate the scenario are slowing down my game. I've been told to use NSThreads, but I really don't know anything about them. My question is, What are the differences between using NSThreads and NSTimers? Or what are the advantages of using NSThreads? How are they useful?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

小兔几 2024-12-13 07:51:55

计时器用于异步而不是并发执行代码。

计时器会等待一定的时间间隔,然后触发,向目标对象发送指定的消息。例如,您可以创建一个 NSTimer 对象,该对象向窗口发送消息,告诉它在一定时间间隔后自行更新。

线程用于并发执行代码。

NSThread 对象控制执行线程。当您希望 Objective-C 方法在其自己的执行线程中运行时,请使用此类。当您需要执行冗长的任务,但不希望它阻止应用程序其余部分的执行时,线程特别有用。特别是,您可以使用线程来避免阻塞应用程序的主线程,该主线程处理用户界面和事件相关的操作。线程还可以用于将大型作业划分为多个较小的作业,这可以提高多核计算机上的性能。


另请参阅:

Timers are used for asynchronous, not concurrent, execution of code.

A timer waits until a certain time interval has elapsed and then fires, sending a specified message to a target object. For example, you could create an NSTimer object that sends a message to a window, telling it to update itself after a certain time interval.

Threads are for concurrent execution of code.

An NSThread object controls a thread of execution. Use this class when you want to have an Objective-C method run in its own thread of execution. Threads are especially useful when you need to perform a lengthy task, but don’t want it to block the execution of the rest of the application. In particular, you can use threads to avoid blocking the main thread of the application, which handles user interface and event-related actions. Threads can also be used to divide a large job into several smaller jobs, which can lead to performance increases on multi-core computers.


See also:

枯寂 2024-12-13 07:51:55

计时器是稍后简单地调用方法的对象。线程是要处理的附加“流”,处于伪并行状态。这就像比较苹果和橙子——它们并没有真正的相关性。我能看到的唯一关系是,如果你想在后台进行一些处理,但你不应该在后台线程中进行 UI 调用,为什么你要使用计时器来让你的角色移动?知道了这一点,我们也许能够提供替代解决方案。

Timers are objects that simply call methods at a later time. Threads are additional "streams" of stuff to be processed, in psuedo-parallel. It's like comparing apples to oranges—they're not really related. The only relation I can see is if you want to do some processing in the background, but you shouldn't be doing UI calls in background threads, Why are you using timers to make your character move? Knowing that, we might be able to supply an alternate solution.

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