Use CADisplayLink as dynamic frequency timer

落爺英雄遲暮 提交于 2019-12-03 18:15:49

The display link is going to call your callback method checkTimer approximately every 1/30th of a second. That's all it does. The exact timing is inexact and unknown. Keeping track of how much time has elapsed since the last time it called you back, and deciding whether this means it's time to do another animation, is completely up to you. You have to use the display link's timestamp, for example, to know just how long it's been since the last callback. You aren't doing that so your timing is off. (Also you may be testing in the Simulator, where CADisplayLink doesn't work. You have to test on the device.)

However, for a measurement as gross as "every 5 seconds", CADisplayLink is a complete waste. CADisplayLink is intended for when you yourself are animating every frame of the animation (that is, you are changing something, yourself, explicitly, every 1/30th of a second). That's not appropriate here. You don't care whether the timer is a fraction of a second off with respect to 5 seconds, so there's no big deal. Stick with your current code. If your animation is stuttering, figure out why; it has something to do with the animation, but it has nothing to do with the timer, which after all is merely saying "start" and then retiring. You have misunderstood what the various posts say; they don't say "don't use NSTimer", they say "don't use NSTimer to dictate the individual frames of an animation", and you were never doing that in the first place.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!