Panda Pig • Patty original or translated works. Welcome reprint, Reprint please indicate the source.
If you feel that the writing is not good please more advice, if you feel good please support a lot of praise. Thank you! Hopy;)
Original text in:
http://oleb.net/blog/2010/12/animating-drawing-of-cgpath-with-cashapelayer/
The principle is explained in:
Http://www.codeproject.com/Articles/109729/Low-level-text-rendering
The article is very well written and recommended for everyone to read.
But its source code run a little bit of a problem, that is, sometimes the brush display does not come out!
There is no problem when you draw the graphics for the first time, but sometimes the brushes do not appear when you switch to the text outline.
In-(void) Animationdidstop: (Caanimation *) Anim finished: (BOOL) flag at the next breakpoint, it is also interrupted when the view is switched (the previous animation is not completed).
Go to Startanimation method view
-(void) startanimation{[self. PathlayerRemoveallanimations];[Self. PenlayerRemoveallanimations];Self. Penlayer. Hidden= NO;Cabasicanimation *pathanimation = [Cabasicanimation animationwithkeypath:@"Strokeend"];Pathanimation. Duration=10.0;Pathanimation. Fromvalue= [NSNumber numberwithfloat:0.0F;Pathanimation. Tovalue= [NSNumber numberwithfloat:1.0F;[Self. PathlayerAddanimation:pathanimation forkey:@"Strokeend"];Cakeyframeanimation *penanimation = [Cakeyframeanimation animationwithkeypath:@"Position"];Penanimation. Duration=10.0;Penanimation. Path= Self. Pathlayer. Path;Penanimation. Calculationmode= kcaanimationpaced;Penanimation. Delegate= Self;[Self. PenlayerAddanimation:penanimation forkey:@"Position"];}
The deletion of the original animation was found at the beginning, and the deletion of the animation also caused Animationdidstop's callback, although the author specifically enabled the display brush after deletion:
self.penLayer.hiddenNO;
However, this and the order of execution of animationdidstop are not guaranteed! If Animationdidstop is executed later, the previously mentioned brush is hidden and is not displayed.
Knowing the reason to solve it is very simple, we only need to modify the Animationdidstop method as follows:
- (void) animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{ //如果动画不是由于删除而停止的才隐藏画笔! if (flag){ self.penLayer.hiddenYES; }}
Now run, small bugs get repaired, cute brushes and hard work again:
A little improvement in animating Coretext text outlines