ios - 设置UIBezierPath的lineWidth不起作用

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

我正在尝试绘制一个圆,但是无论我设置了什么值,圆边界始终总是相同的细线。
感觉就像总是默认最细宽度的线。

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

}

这真让我发疯。

最佳答案

您需要更改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];

}

关于ios - 设置UIBezierPath的lineWidth不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50619031/

相关文章:

ios - Google Play 游戏服务 iOS : [UITableViewWrapperView style]: unrecognized selector sent to instance

iphone - 在 Controller 之间传递数据的 "delegates"的替代方案是什么?

ios - 无需 iCloud 登录的 CloudKit 公共(public)数据库读取

objective-c - 实现NSCoding的对象子类化时,是否需要调用[super encodeWithCoder]?

objective-c - CALayer : Single pixel line looks like 2 pixels

cocoa - 使用 CGBitmapContextCreate 始终为黑色的 32 位灰度图像

ios - Firebase 动态链接在 iOS gmail 上不起作用

ios - UITableView中的多线程

ios - 如何隐藏第一个单元格顶部的分隔符?

C代码中的CGRect