2014-11-08 165 views
2

我有一个视图,其中我有多个水平线(如horzontal拨号)。我想动画水平运动。它应该是这样的Ios画线动画

编辑:

Horizontal Animation

enter image description here

enter image description here

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

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

谢谢!

+0

我不完全明白你想要做什么。为什么你不能使用核心动画? – 2014-11-08 10:31:10

+0

也许我不了解Core Animation。据我所知,它激发了观点的运动,而不是内在。我的想法是显示拨号盘的边缘,当拨号盘旋转时,线条移动。因此,线条必须始终在不动的区域内重复。 – Jan 2014-11-08 10:38:40

+0

你能否包含一张图片显示你正在尝试做什么(最好多一帧(理解动画的意思)? – 2014-11-08 15:45:42

回答

8

如果我已经理解你正在尝试做什么,那么我没有理由不能使用Core Animation来做到这一点。

如果您正在移动的图案非常简单,那么您可以在CAShapeLayer上使用线条图案来绘制图案。我建立了我的层,使其从边界高度得到的线条宽度和形状层的路径从层的边界得到它的起点和终点:

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

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

enter image description here

随着绘制的路径,我通过改变线的破折号“相”做了动画(在一个方向或另一个方向移动它们)。使之看起来像图案是平滑且连续地移动我创建了一个线性的,重复的动画,通过其整个长度移位的图案:

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

动画线看起来像这样:

enter image description here

如果您想让不同的线条以不同的速度动画,请更改动画的duration。此值表示动画移动一个完整阶段所需的时间。

+0

哇,所以我完全没有得到Core Animation。只是想知道..因为我想用它作为一个控件,我如何将它移动到我的手指下? – Jan 2014-11-08 18:16:25

+0

您可能可以使用图层的'timeOffset'来控制动画的时间(就像在[这个答案]中一样)(http://stackoverflow.com/a/18683845/608157))你必须弄清楚拖动手指和动画阶段之间的数学转换。这取决于一个阶段的长度和动画的持续时间。 – 2014-11-08 19:59:54