ios - 如何正确定位动画 CAShapeLayer

标签 ios cashapelayer

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

View 的框架是:10, 10, 50, 100

查看图像(红色形状图层应“移动”超过 10 pts。

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;

最佳答案

“修复”改变了这些行

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

关于ios - 如何正确定位动画 CAShapeLayer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20847545/

相关文章:

ios - UIKit 上的奇怪崩溃

ios - 为什么 eventsMatchingPredicate 返回 nil?

ios - 为 CAGradientLayer 添加 mask 使 UIBezierPath 消失

ios - 如何在 Swift 4 中使用 CAShapeLayer 和 UIBezierPath 绘制曲线?

ios - CABasicAnimation CALayer 路径动画方向

ios - 沿着 UIBezierPath 放置 `points`,使得 `center` 点位于路径上

ios - 检查 UIBezierPath 是否关闭

ios - 更新核心数据模型对象时遇到错误 (iOS)

ios - UINavigationBar 在 Action Extension 中的旋转高度错误

ios - XCode 内存不足,但 Instruments 内存不足