代码之家  ›  专栏  ›  技术社区  ›  JsW

设置不工作的uibezierpath的线宽

  •  1
  • JsW  · 技术社区  · 6 年前

    我试图画一个圆,但不管我设置了什么值,圆的边界总是一样细。 感觉这条线总是默认的最窄宽度。

    - (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;
    
    }
    

    这真的把我逼疯了。

    1 回复  |  直到 6 年前
        1
  •  4
  •   Sharad Chauhan Muhammad Bilal Hussain    6 年前

    您需要更改腰果层的宽度,而不是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];
    
    }