CAShapeLayer path animation — shrinking a circle

混江龙づ霸主 提交于 2020-01-14 05:40:11

问题


I'm trying to shrink a circular CAShapeLayer but using the key path 'path' does not seem to be working.

CGPathRef startPath = [UIBezierPath bezierPathWithOvalInRect:startRect].CGPath;
CGPathRef endPath   = [UIBezierPath bezierPathWithOvalInRect:CGRectInset(startRect, 15, 15)].CGPath;

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"path"];
animation.duration          = 1.0;
animation.fromValue         = (__bridge id)startPath;
animation.toValue           = (__bridge id)endPath;
animation.autoreverses      = YES;
animation.repeatCount       = CGFLOAT_MAX;
animation.timingFunction    = [CAMediaTimingFunction functionWithControlPoints:0.6 :0.3 :0.8 :0.45];

//circleLayer is a CAShapeLayer
[self.circleLayer addAnimation:animation forKey:@"pathAnimation"];

I think i must be misunderstanding about how a path animation works because pretty much identical code seems to work fine if i am try to animate, say, the opacity or the transform.

Any ideas?


回答1:


I recently had the same question and ended up solving it by creating the circle, then animating the scale CGAffineTransform. In the view whose layer is the circle, I simply used

CGAffineTransform transform = CGAffineTransformIdentity;
transform = CGAffineTransformScale(transform, scaleX, scaleY);

[UIView animateWithDuration: 0.5 animations: ^{
    self.transform = transform;
}];

Hope this helps!



来源:https://stackoverflow.com/questions/22212481/cashapelayer-path-animation-shrinking-a-circle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!