如何正确定位动画CAShapeLayer

时间:2013-12-30 21:04:29

标签: ios cashapelayer

我正在制作CAShapeLayer的结束笔画,但位置不正确,我无法弄清楚我做错了什么。框架是正确的,但是当我尝试调整位置时,我无法让它“填充”框架的视图宽度。我试图调整_maskLine的位置,但到目前为止我的方法都失败了。

视图的框架为: 10,10,50,100

参见图片(红色形状图层应该“移动”超过10分。

enter image description here

// layer setup in initWithFrame

_maskLine = [CAShapeLayer layer];

_maskLine.lineCap   = kCALineCapButt;
_maskLine.fillColor = [[UIColor whiteColor] CGColor];
_maskLine.lineWidth = frame.size.width;
_maskLine.strokeEnd = 0.0;
_maskLine.frame     = self.bounds;

[self.layer addSublayer:_maskLine];

self.clipsToBounds      = YES;
self.layer.cornerRadius = 2.0f;


// animation method logic

UIBezierPath *progressline = [UIBezierPath bezierPath];

NSUInteger randomInt = arc4random() % (NSUInteger)self.maxMeterHeight;

self.meterHeight = (randomInt / self.maxMeterHeight);

[progressline moveToPoint:CGPointMake(CGRectGetMidX(self.frame), CGRectGetHeight(self.frame))];
[progressline addLineToPoint:CGPointMake(CGRectGetMidX(self.frame), (1 - self.meterHeight) * CGRectGetHeight(self.frame))];

[progressline setLineWidth:1.0];
[progressline setLineCapStyle:kCGLineCapSquare];

self.maskLine.path        = progressline.CGPath;
self.maskLine.strokeColor = [UIColor redColor].CGColor;
self.maskLine.frame       = self.bounds;

NSLog(@"mask frame: %@", NSStringFromCGRect(self.maskLine.frame));

CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];

pathAnimation.duration       = 0.35;
pathAnimation.autoreverses   = YES;
pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
pathAnimation.fromValue      = @0.0f;
pathAnimation.toValue        = @1.0f;
pathAnimation.cumulative     = YES;

[self.maskLine addAnimation:pathAnimation forKey:@"strokeEndAnimation"];

[CATransaction commit];

self.maskLine.strokeEnd = 1.0;

1 个答案:

答案 0 :(得分:1)

“修复”正在改变这些行

[progressline moveToPoint:CGPointMake(CGRectGetMidX(self.frame), CGRectGetHeight(self.frame))];
[progressline addLineToPoint:CGPointMake(CGRectGetMidX(self.frame), (1 - self.meterHeight) * CGRectGetHeight(self.frame))];

为:

[progressline moveToPoint:CGPointMake(self.frame.size.width / 2, CGRectGetHeight(self.frame))];
[progressline addLineToPoint:CGPointMake(self.frame.size.width / 2, (1 - self.meterHeight) * CGRectGetHeight(self.frame))];
相关问题