【问题标题】:CAShapeLayer path animation -- shrinking a circleCAShapeLayer 路径动画——缩小圆
【发布时间】:2014-03-06 00:33:53
【问题描述】:

我正在尝试缩小圆形 CAShapeLayer,但使用关键路径“路径”似乎不起作用。

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"];

我想我一定误解了路径动画的工作原理,因为如果我尝试制作不透明度或变换的动画,几乎相同的代码似乎可以正常工作。

有什么想法吗?

【问题讨论】:

  • 在我的脑海中,我无法发现哪里出了问题。我以前可以让very similar code 工作。那么,您能否验证这两条路径是否合理且有效(主要验证 startRect 是否足够大,可以像那样插入)。您可以通过简单地将 endPath 分配给圆形层并验证它是否有效(没有动画)来测试它
  • startRect 是什么?如果小于 30x30,则插入时为 CGRectZero,因此 endPath 可能未定义。因为除此之外,一切看起来都很完美。

标签: ios core-animation calayer


【解决方案1】:

我最近遇到了同样的问题,并最终通过创建圆圈来解决它,然后为比例 CGAffineTransform 设置动画。在图层为圆圈的视图中,我简单地使用了

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

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

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-22
    • 1970-01-01
    相关资源
    最近更新 更多