我想处理以下情况:
如果我使用CGBezierPath和CAShapeLayer绘制矩形,如下所示:
CAShapeLayer *layerX = [CAShapeLayer layer];
layerX.path = path.CGPath;
layerX.lineWidth = 3;
layerX.strokeColor = [[UIColor whiteColor] CGColor];
layerX.fillColor = [[UIColor clearColor] CGColor];
[self.view.layer addSublayer: layerX];
而且我想添加动画,例如附加的图像,该空白区域会在矩形路径周围不断移动,从而使矩形的视觉效果不断出现。 [旧蛇游戏中的蛇运动]
我尝试使用CABasicAnimation制作动画,但实际上我什么都做不了,谢谢。
最佳答案
尝试使用此代码可能会有所帮助。
UIBezierPath *drawablePath = [UIBezierPath bezierPathWithRect:CGRectMake(100.0, 100.0, 150.0, 100.0)];
CAShapeLayer *squareLayer = [[CAShapeLayer alloc] init];
squareLayer.path = drawablePath.CGPath;
squareLayer.strokeColor = [UIColor redColor].CGColor;
squareLayer.lineWidth = 5.f;
squareLayer.fillColor = [UIColor clearColor].CGColor;
squareLayer.strokeEnd = 1.f;
//squareLayer.strokeEnd = 0.9; // this line use draw half line but some animation issue.
[self.view.layer addSublayer:squareLayer];
CABasicAnimation *drawAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
drawAnimation.fromValue = (id)[NSNumber numberWithFloat:0.10];
drawAnimation.toValue = (id)[NSNumber numberWithFloat:1.f];
drawAnimation.duration = 5.f;
drawAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
[squareLayer addAnimation:drawAnimation forKey:@"drawRectStroke"];
此代码用于绘制带有动画的矩形。
关于ios - 如何为CGPath的绘图部分制作动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46212723/