Ios绘制线条animation

我有一个观点,我有多个水平线(如horzontal拨号)。 我想animation水平运动。 它应该看起来像这样

编辑:

水平的动画

在这里输入图像说明

在这里输入图像说明

我怎样才能做到这一点? 据我所知,我不能使用CoreAnimation。 此外,我不能模拟不同的速度。 updatemethod如何工作? 我是否改变抽签时间或距离?

我知道我可以用scrollview来解决这个问题,但是我不想使用它。

谢谢!

如果我已经明白你想要做什么,那么我看不出有什么理由不能用Core Animation来做到这一点。

如果您正在移动的图案非常简单,那么您可以在CAShapeLayer上使用线条图案来绘制图案。 我创build了我的图层,以便从边界的高度获取线宽,并且形状图层的path从图层的边界获取其起点和终点:

CAShapeLayer *lineLayer = [CAShapeLayer layer]; lineLayer.bounds = CGRectMake(0, 0, 200, 60); lineLayer.position = self.view.center; lineLayer.strokeColor = [UIColor blackColor].CGColor; lineLayer.lineDashPattern = @[@5, @2, @2, @2, @2, @2, @2, @2, @2, @2]; lineLayer.lineWidth = CGRectGetHeight(lineLayer.bounds); UIBezierPath *linePath = [UIBezierPath bezierPath]; [linePath moveToPoint:CGPointMake(0, CGRectGetMidY(lineLayer.bounds))]; [linePath addLineToPoint:CGPointMake(CGRectGetMaxX(lineLayer.bounds), CGRectGetMidY(lineLayer.bounds))]; lineLayer.path = linePath.CGPath; [self.view.layer addSublayer:lineLayer]; 

这将产生模式的静态绘图。 代码中的划线图案是线条显示位置和不显示位置的交替段的长度。

在这里输入图像说明

随着path的绘制,我通过改变线条的“阶段”(将它们向一个方向或另一个方向移动)来进行animation。 为了使图案看起来像是平滑而连续的移动,我创build了一个线性重复的animation,将图案的全长移动:

 NSNumber *totalDashLenght = [lineLayer.lineDashPattern valueForKeyPath:@"@sum.self"]; // KVC is awesome :) CABasicAnimation *animatePhase = [CABasicAnimation animationWithKeyPath:@"lineDashPhase"]; animatePhase.byValue = totalDashLenght; // using byValue means that even if the layer has a shifted phase, it will shift on top of that. animatePhase.duration = 3.0; animatePhase.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]; animatePhase.repeatCount = INFINITY; [lineLayer addAnimation:animatePhase forKey:@"marching ants"]; 

animation线看起来像这样:

在这里输入图像说明

如果您希望不同的线条以不同的速度animation,则可以更改animation的duration 。 这个值表示animation移动一个完整阶段所需要的时间。