What is the best way (performance-wise) to flash t

2019-06-11 06:19发布

问题:

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.

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?

回答1:

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

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.

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.