Based on the response to the display of Matt links, you can track the position of the endpoint by creating a second “invisible” keyframe animation.
NOTES:
- With this technique, you do not need to calculate the position of the endpoint yourself.
- You can use any form of path.
- it was written in the controller class view of the basic iOS Single View application template in Xcode
Let's start with 3 properties:
@interface ViewController () @property (nonatomic, strong) CADisplayLink *displayLink; @property (nonatomic, strong) CAShapeLayer *pathLayer; @property (nonatomic, strong) CALayer *trackingLayer; @end
displayLink will allow us to run the code every time we refresh the screen. pathLayer provides visual effects that we will animate. trackingLayer provides an invisible layer that we will use to track the position of the strokeEnd animation on pathLayer .
We open our view controller as follows:
@implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self createDisplayLink]; [self createPathLayer]; [self createTrackingLayer]; [self startAnimating]; } ...
Using the following methods ...
First, create a link to display and add it to the run loop (according to Matt's code):
-(void)createDisplayLink { _displayLink = [CADisplayLink displayLinkWithTarget:self selector: @selector(displayLinkDidUpdate:)]; [_displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode]; }
Then we create a visible layer:
-(void)createPathLayer {
Then we create an “invisible” layer (that is, through a frame without dimensions) to track:
-(void)createTrackingLayer { _trackingLayer = [CALayer layer];
Then we create a method that captures the position of the tracking layer:
- (void)displayLinkDidUpdate:(CADisplayLink *)sender {
... and the startAnimating method:
-(void)startAnimating {
This method is very useful if you have paths that you want to follow but don’t want to be bothered by doing the math itself.
Some of the important reasons for this are:
- different / user paths can be used (not just ellipses ...)
- various multimedia synchronization functions can be used (you do not need to independently determine the math for convenience, because of or linear, etc.).
- you can start / stop the animation of the tracking layer at any time (i.e. do not run continuously)
- you can start / stop the image link at any time
- Converting between different coordinates of a layer is quite simple, so you can have a layer inside layers inside layers and transfer their coordinates to any other layer.
EDIT Here's a link to the github repository: https://github.com/C4Code/layerTrackPosition
Here is a picture of my simulator:
