【问题标题】:How do I correctly position an animated CAShapeLayer如何正确定位动画 CAShapeLayer
【发布时间】:2013-12-30 21:04:28
【问题描述】:

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

view的frame是:10,10,50,100

查看图片(红色形状层应该“移动”超过 10 点。

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

【问题讨论】:

    标签: ios cashapelayer


    【解决方案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))];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-20
      • 2012-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多