问题
I'm trying to animate the strokeEnd property of one layer, and position of another layer. I've tried to set up two CABasicAnimations with the same duration, but one finishes earlier than the other. I can't for the life of me understand why.
CALayer *trackingDotPresentationLayer = (CALayer *)trackingDot.presentationLayer;
CABasicAnimation *trackingDotMovementAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
trackingDotMovementAnimation.duration = lineDrawDuration;
trackingDotMovementAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
trackingDotMovementAnimation.fromValue = [NSValue valueWithCGPoint:trackingDotPresentationLayer.position];
trackingDot.position = futureTrackingDotFrame.origin;
trackingDotMovementAnimation.toValue = [NSValue valueWithCGPoint:futureTrackingDotFrame.origin];
trackingDotMovementAnimation.fillMode = kCAFillModeForwards;
[trackingDot addAnimation:trackingDotMovementAnimation forKey:@"trackingDotMovement"];
CABasicAnimation *lineAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
lineAnimation.duration = lineDrawDuration;
lineAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
lineAnimation.fromValue = @0.0;
lineLayer.strokeEnd = 1.0;
lineAnimation.toValue = @1.0;
lineAnimation.fillMode = kCAFillModeForwards;
[lineLayer addAnimation:lineAnimation forKey:@"strokeEnd"];
Any suggestions?
回答1:
Why are you using the presentationLayer? Usually this is not needed for any kind of animation. Furthermore you set the lineLayer.strokeEnd directly (and therefore in an implicit animation). Try removing the line lineLayer.strokeEnd = 1.0;
Alternatively you could use a CATransaction like this:
lineLayer.strokeEnd = 0.0;
[CATransaction begin];
[CATransaction setAnimationDuration:lineDrawDuration];
[CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
trackingDot.position = futureTrackingDotFrame.origin;
lineLayer.strokeEnd = 1.0;
[CATransaction commit];
回答2:
So it turns out that there was nothing wrong with the setup of my animations. I had another issue with one of the objects that was being animated which was resolved in another question: CALayer rendering issues with diagonal dashed line. I should have realised that closing the subpath was extending the line which was being drawn in the animation, therefore causing what seemed to be incorrect timing.
来源:https://stackoverflow.com/questions/24624379/trouble-timing-two-cabasicanimations-together