Even though the title of this post mentions performance, I could not find substantial evidence or data to back my speculation. So, I might update this post later on if extra related performance tests and measurements can be found.
But, first of all, what is the waving effect mentioned in the title like?
In fact, making such an effect programmatically is not that difficult. Most of, if not all, public repos which give you the ability to have such a waving effect from Github.com are using a timer (CADisplayLink or NSTimer) to drive the animation.
Relying on such a timer to update the wave when there are just a few wave views might be ok. But, what if there are a whole bunch of them, like adding such a waving effect to each one of your table view cell? Will performance overhead occur?
So, besides using a timer mechanism to drive and pace the animation, using CAReplicatorLayer, CABasicAnimation and CAShapeLayer might just do the trick.
Using replicator layer to hold two instances of wave-shape layer;
Each instance of wave-shape layer has one or more complete waves (when using sin(x) function to draw the wave path, x must be one or multiple 2Pi); This is needed to make sure the second wave-shape layer instance’s very start merges perfectly with the first’s end;
Using a transform.translation.x basic animation to drive the wave-shape layer instance;
The wave path is drawn using the math sin(x) function; And, the parameters used in this function are chosen to serve the demo. If you want a different wave form, such as larger amplitude, you can tweak the parameters.
The most important thing is sin((xPos)/canvasWidth * M_PI * 2). Here (xPos)/canvasWidth * M_PI * 2 guarantees the wave will have just one complete cycle. So, when the transform.translation.x animation ends and repeats itself, the first shape-layer returns to itself original position, visually the wave never stops moving to left.
In the Demo, JWTimerWaveView is used to mimic the behavior of a timer-driven wave animation.
So, my speculation was replicator-layer-based animation will be somewhat better than timer-driven wave animation in terms of performance when used in a mass scale. So, my test was to put this two kinds of wave view into table view cells and scroll the table.
However, when using Core Animation Profiler, the FPS value is almost the same for both of them.
The only difference is CPU Usage, the timer-based animation uses about 7-10% of the cpu time constantly during the animation even when the table is not being scrolled. The replicator-layer-based animation only uses cpu time when scrolling the table, but when the table isn’t being scrolled no cpu usage can be seen.
The cpu usage can support my speculation in some way. But, I just feel the support isn’t enough. If FPS drops largely when using time-driven animation, this might better prove my idea. But because timer-driven wave animation takes extra cpu time, when the App is doing something cpu-heavy, it’s more likely to suffer a FPS drop than replicator-layer-based wave animation.
This is open to discussion. More test ideas and opinions are welcome.