CAShapeLayer simple and practical (2)

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/WangErice/article/details/51580955

Recently seen on a Netease news is not very complicated little effect, Netease is the top right corner of the skip button start page, we take a feel.


Right, you're not wrong is the little arrow button in the upper right corner, and now we try to achieve it. Because the control can respond to events, so we let the control inherited from the UIButton to be rewritten.

Defined class inherits from SkipButton UIButton, and then use CAShapeLayer to achieve animation edges.

1) First, we define a callback method



Custom an initialization method;


In order to allow the click event callback method will be passed out by events do unified treatment, we offer property is set to provide an interface, the property is YES, the click event will trigger a callback interface, does not trigger default


It provides a method to start the animation,


2) method implementation

2.1 In the initialization process,


Parameters: blockKey character defines a static variable, which is used as an address value stored key block callback method using

objc_setAssociatedObject ( Self , & blockKey , Finished,  OBJC_ASSOCIATION_COPY ) storing the callback method, easy to use in other methods;

circleColor: storing initialization method in the outer ring of the incoming color;

lineWidth: storing initialization method afferent outer circular line width;

timeCount: memory initialization process incoming duration of the animation;

shapeLayer: display graphical outer annular layer;

progressAni: CABaseAnimation achieve the object of the outer ring.

2.2 using lazy loading to create shapeLayer and progressAni


In the process of creating shapeLayer, we will radian interval - the whole circle counterclockwise path (-M_PI_2, (M_PI * 2+ M_PI_2)) is assigned to the path attribute _shapeLayer;

In the process of creating progressAni, the assignment for the duration property, if the initialization value is not given, the default is 2.0 seconds;

At the same time specify a movie agent, so that we can get through the bottom of the timing of the callback proxy method when the animation ends:



So the focus here, ready to end everything, we have to realize startAnimation method:


If sameActionWithClick property is YES, then set the click event, while calling the callback method in the click event, the animation and ShapeLayer while removing the graphics layer on shapeLayer, then click on the event to avoid the outer ring continues to display.


Want to learn detailed implementation, please download SkipButtonDem O.

Guess you like

Origin blog.csdn.net/WangErice/article/details/51580955