简体   繁体   English

贝塞尔曲线的动画大小

[英]Animate size of a bezier curve

I want to create a circular arc of a bezier curve and then animate it to reduce the radius of the curve. 我想创建贝塞尔曲线的圆弧,然后对其进行动画处理以减小曲线的半径。

Currently I have the following code to draw my bezier curve in a UIView: 当前,我有以下代码在UIView中绘制贝塞尔曲线:

UIView * curveView = [[UIView alloc] initWithFrame:CGRectMake(50, 550, 100, 100)];
curveView.backgroundColor = [UIColor orangeColor];

UIBezierPath * aPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(150, 150)
                                                     radius:75
                                                 startAngle:0
                                                   endAngle:2 * M_PI
                                                  clockwise:YES];

CAShapeLayer * shapeLayer = [CAShapeLayer layer];
shapeLayer.path = aPath.CGPath;
shapeLayer.fillColor = [UIColor colorWithRed:.5 green:1 blue:.5 alpha:1].CGColor;
shapeLayer.strokeColor = [UIColor blackColor].CGColor;
shapeLayer.lineWidth = 2;

[curveView.layer addSublayer:shapeLayer];

[self.view addSubview:curveView];

This adds a circular bezier curve to the view as expected. 如预期的那样,这将在视图中添加圆形贝塞尔曲线。 The issue I am having is to animate for the curve to shrink down over a certain period of time. 我要解决的问题是为曲线设置动画以使其在一定时间内缩小。

If I were using UIViews instead I would probably use constraints but I can't seem to find any resources to help me use them in this case. 如果我使用的是UIViews,则可能会使用约束,但是在这种情况下,我似乎找不到任何资源来帮助我使用它们。

Having looked through many of the answers to different questions on Stackoverflow it seems like I might need to use CABasicAnimation but the questions are mainly to animate the curve of a line instead of the length of the line. 浏览了关于Stackoverflow的不同问题的许多答案之后,似乎我可能需要使用CABasicAnimation,但是这些问题主要是为直线的曲线而不是直线的长度设置动画。

This issue is difficult as the method used for drawing a line (start point, a series of paths, end point) seems very different from that of drawing a circle. 这个问题很难解决,因为绘制直线(起点,一系列路径,终点)所用的方法似乎与绘制圆圈的方法大不相同。 This means I don't know if the process I am looking for will be the same or different. 这意味着我不知道我要寻找的过程是相同还是不同。 An answer here seems to have a solution for animating a line but my code for creating the circle doesn't use start or end points meaning it doesn't help very much. 此处的答案似乎是为线设置动画的一种解决方案,但是我创建圆的代码没有使用起点或终点,这意味着它没有太大帮助。

I thought about using separate arcs to create the circle and then changing their start points and control points but these arcs don't seem to be perfectly circular, and are more used for custom wiggly lines. 我考虑过使用单独的弧线创建圆,然后更改其起点和控制点,但是这些弧线似乎并不是完美的圆形,而是更多地用于自定义摆动线。 There is an example of this here but it isn't for iOS. 有这样的例子在这里 ,但它并不适用于iOS。

path is an animatable property on CAShapeLayer. path是CAShapeLayer上的动画属性。 If you create a second, smaller version of your circle path, you can animate between those two paths just as you would any other animatable property. 如果创建第二个较小的圆形路径版本,则可以在这两个路径之间进行动画处理,就像创建任何其他可设置动画的属性一样。

UIBezierPath *smallerPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(150, 150) radius:25 /* note different radius */ startAngle:0 endAngle:2 * M_PI clockwise:YES];

CABasicAnimation *shrinkAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
shrinkAnimation.fromValue = (id)aPath.CGPath;
shrinkAnimation.toValue = (id)smallerPath.CGPath;
shrinkAnimation.duration = 1;
[shapeLayer addAnimation:shrinkAnimation forKey:@"shrink"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM