I am trying to animate loading animation along the path of rounded rect,using following code. (I need to animate border of square as loader ,partial border.) It does not happen to be smooth ,as soon as it reaches the end of path it jumps to the starting start stroke.
shapeLayer = [CAShapeLayer layer];
CGRect shapeRect = _border.bounds;//CGRectMake(0.0f, 0.0f, 200.0f, 100.0f);
[shapeLayer setBounds:shapeRect];
[shapeLayer setContents:_border.image];
shapeLayer.contentsGravity = kCAGravityCenter;
NSLog(@"center of border %@",NSStringFromCGPoint(_border.center) );
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:shapeLayer.bounds cornerRadius:BORDER_ANGLE];
[shapeLayer setPath:path.CGPath];
shapeLayer.position = _border.center;
[[self.view layer] addSublayer:shapeLayer];
[[self.view layer] insertSublayer:shapeLayer above:_border.layer];
[shapeLayer setFillColor:[[UIColor clearColor] CGColor]];
[shapeLayer setStrokeColor:[[UIColor whiteColor] CGColor]];
[shapeLayer setLineWidth:2.5f];
shapeLayer.strokeStart = 0;
shapeLayer.strokeEnd = 0.1;
UIImageView * image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ball"]];
image.center = CGPointMake(187, 220.5);
startAnim = [CABasicAnimation animationWithKeyPath:@"strokeStart"];
startAnim.duration = SPEED;
startAnim.delegate = self;
startAnim.repeatCount=200;
startAnim.removedOnCompletion = FALSE;
startAnim.toValue = [NSNumber numberWithFloat:0.3];
endAnim = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
endAnim.toValue = [NSNumber numberWithFloat:0.99];
endAnim.duration = SPEED;
endAnim.repeatCount = 200;
endAnim.removedOnCompletion = NO;
endAnim.cumulative = NO;
[shapeLayer addAnimation:startAnim forKey:nil];
[shapeLayer addAnimation:endAnim forKey:nil];
But above code jitters after reaching at endAnim's tovalue and starts again,its not smooth.
Please help me know which parts going wrong. I saw many tutorials for loader but all those were for circular path not square. [enter image description here][1]