For 2018 ...
It couldn't be simpler.
Do not forget [weak self] , otherwise you will fall.
func animeExample() { CATransaction.begin() let a = CABasicAnimation(keyPath: "fillColor") a.fromValue, duration = ... etc etc CATransaction.setCompletionBlock{ [weak self] in self?.animeExample() self?.ringBell() print("again...") } someLayer.add(a, forKey: nil) CATransaction.commit() }
,,,, NOTE. CRITICAL COUNCIL >>>>>
We are interrupting this post for critical advice:
There MUST be a string setCompletionBlock BEFORE the string someLayer.add.
You can google to read more details about this, but in general it is important to have it in that order. Now back to the post!
& Lt; & Lt; & Lt; & Lt; & Lt; NOTE. - CRITICAL COUNCIL., ,,.
In this example, it just calls itself again.
Of course, you can call any function.
Notes for anyone new to iOS animation:
The "key" (as in forKey ) does not matter and is rarely used . Set it to zero. If you want to install it, set it to any string.
"KeyPath" is actually the actual "thing you animate . " This is literally a layer property, such as "opacity", "backgroundColor", etc., but written as a string . (You cannot just enter โwhatever you wantโ there, it must be the name of the actual property of the layer, and it must be animatable.)
We repeat: the "key" (rarely used - just set it to zero) and "keyPath" are completely unrelated to each other.
You often see sample code where the two are confused (thanks to a silly naming convention), which causes all kinds of problems.
Note that you can use a delegate one at a time, but itโs much easier to just use a completion block, because (A) it is self-sufficient and can be used anywhere, and (B) you usually have several animes, in this case using a delegate is a bore
Fattie Nov 28 '17 at 16:45 2017-11-28 16:45
source share