快速编辑刚刚看到你的图层代码。您混淆了几个不同的概念。像drawRect,CAShapeLayer,旧的动画代码等等......
通过执行下面的方法,您应该能够实现此目的。
你不能这样做:(你不能对drawRect的内容进行动画处理(即你不能让它在动画过程中多次绘制)。
您也许可以使用计时器或其他东西来创建自己的动画类型代码。 (即创建一个每秒触发 30 次的计时器并运行一个函数来计算您在动画中的位置并更新您想要更改的值并调用[self setNeedsDisplay];
触发绘制矩形方法。
除此之外,你无能为力。
另一个编辑
只是在这里添加另一个选项。使用 CAShapeLayer 执行此操作可能会导致性能非常差。您可能最好将 UIImageView 与一系列 UIImage 一起使用。
UIImageView 上有内置属性、animationImages、animationDuration 等。
可能的解决方案
The path
的财产CAShapeLayer
是可动画的,所以你可以使用它。
就像是...
// set up properties for path
@property (nonatomic, assign) CGPath startPath;
@property (nonatomic, assign) CGPath endPath;
@property (nonatomic, strong) CAShapeLayer *pathLayer;
// create the startPath
UIBezierPath *path = [UIBezierPath //create your path using the paint code code
self.startPath = path.CGPath;
// create the end path
path = [UIBezierPath //create your path using the paint code code
self.endPath = path.CGPath;
// create the shapee layer
self.pathLayer = [CAShapeLayer layer];
self.pathLayer.path = self.startPath;
//also set line width, colour, shadows, etc...
[self.view.layer addSubLayer:self.pathLayer];
那么你应该能够像这样为路径设置动画......
- (void)animatePath
{
[UIView animateWithDuration:2.0
animations^() {
self.pathlayer.path = self.endPath;
}];
}
文档中有很多关于 CAShapeLayer 和动画路径属性的注释。
不过这应该可行。
另外,摆脱旧的动画代码。自 iOS4.3 以来它已经消失了,您应该使用更新的块动画。