【问题标题】:Setting the lineWidth of a UIBezierPath not working设置 UIBezierPath 的 lineWidth 不起作用
【发布时间】:2018-05-31 07:34:37
【问题描述】:

我正在尝试画一个圆圈,但无论我设置什么值,圆圈边框总是一样薄。 感觉就像总是默认最细宽度的线。

- (void)drawCircleAtPoint:(CGPoint)center radius: (CGFloat)radius{

    UIBezierPath *path = [UIBezierPath bezierPath];

    path.lineWidth = 4; // Here
    path.lineCapStyle = kCGLineCapRound;
    path.lineJoinStyle = kCGLineJoinRound;

    CGFloat r = radius;


    [path addArcWithCenter:center radius:r startAngle:0.0 endAngle:M_PI*2 clockwise:true];

    [path stroke];

    CAShapeLayer* layer = [CAShapeLayer new];
    layer.path = path.CGPath;
    layer.strokeColor = UIColor.redColor.CGColor;
    layer.fillColor = UIColor.yellowColor.CGColor;

    [layer addAnimation:[self getAnimation] forKey:nil];
    [self.layer addSublayer:layer];

}


- (CABasicAnimation*)getAnimation {

    CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    animation.duration = 1.2;
    animation.fromValue = @0;
    animation.toValue = @1;
    return animation;

}

这真的把我逼疯了。

【问题讨论】:

  • 您在层中而不是在贝塞尔路径中设置线宽总是让我感到非常奇怪!
  • 值得注意的是,这同样适用于端盖形状等

标签: ios objective-c core-graphics core-animation uibezierpath


【解决方案1】:

您需要更改 CAShapeLayer 的宽度,而不是 UIBezierPath。像这样:

- (void)drawCircleAtPoint:(CGPoint)center radius: (CGFloat)radius{
    CGFloat r = radius;
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path addArcWithCenter:center radius:r startAngle:0.0 endAngle:M_PI*2 clockwise:true];
    [path setLineWidth:4]; // No need
    [path setLineCapStyle:kCGLineCapRound];
    [path setLineJoinStyle:kCGLineJoinRound];
    [path stroke];
    
    CAShapeLayer *layer = [CAShapeLayer new];
    layer.lineWidth = 4; // Add it here 
    layer.path = path.CGPath;
    layer.strokeColor = UIColor.redColor.CGColor;
    layer.fillColor = UIColor.yellowColor.CGColor;
    
    [layer addAnimation:[self getAnimation] forKey:nil];
    [self.view.layer addSublayer:layer];
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-08
  • 1970-01-01
  • 2019-12-07
  • 2017-01-07
  • 2016-08-15
  • 2011-04-13
相关资源
最近更新 更多