以不同速度闪光的最佳方式(性能方面)是什么? [英] What is the best way (performance-wise) to flash things at a varying speed?

查看:113
本文介绍了以不同速度闪光的最佳方式(性能方面)是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,我以恒定速度闪现字。说它设置为每分钟60次。然后每个单词每个显示1秒。使用 NSTimer 很容易实现。

I have an app where I flash words at a constant speed. Say it's set to 60 times a minute. Each word then shows for 1 second each. It was pretty easy to accomplish with NSTimer.

但是,我想让它变得更加智能化。较长的单词显示的时间略长于较短的单词。我已经计算出如何计算这个数学,但我不确定在Objective-C中如何表示一个单词,0.15秒,然后另一个单词为0.18秒,然后是第三个单词为0.04秒,等等。,取决于单词的长度。

However, I want to make it a little more intelligent now. Longer words show for slightly longer than shorter words. I've figured out the math on how to calculate this, but I'm not sure how in Objective-C to present a word for say, 0.15 seconds, then another word for 0.18 seconds, then a third word for 0.04 seconds, etc., depending on the length of the word.

只是使用延迟才是最好的方法吗?

Would just using a delay be the best way to do this?

推荐答案

您可以使用 performSelector 来延迟,但管理起来不一定非常容易。

You could use performSelector to delay, but it isn't necessarily very easy to manage.

您可以使用 NSTimer ,重复,并为每个新更新设置 fireDate 需要。这是相对昂贵的,但不如重复创建新的计时器。

You could use NSTimer, repeating, and set the fireDate for each new update required. This is relatively expensive but less so than repeatedly creating new timers.

你可以使用 CADisplayLink 持续时间 frameInterval 以屏幕刷新率的倍数获取更新。这应该是最高效和最准确的。

You could use CADisplayLink with a combination of duration and frameInterval to get updates at multiples of the screen refresh rate. This should probably be the most performant and accurate.

但是,总的来说,在你有一些问题的证据和/或做了一些事情之前,你不应该担心性能剖析。请考虑一下您需要哪些功能以及每种解决方案实施起来的容易程度。

But, overall, you shouldn't worry about performance until you have some evidence of a problem and / or have done some profiling. Think instead about what features you need and how easy they are to implement with each solution.

这篇关于以不同速度闪光的最佳方式(性能方面)是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆