填充后无法描边路径

时间:2012-11-23 09:23:15

标签: ios core-graphics drawrect

下面的代码很好地创建了一个由CGRect(rectRect)定义的圆角矩形。

它很好,但我没有中风。任何想法为什么我看不到中风?

-(void)drawRect:(CGRect)rect {

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    CGContextSetRGBFillColor(ctx, 0, 0, 0, 0.4);
    CGContextSetRGBStrokeColor(ctx, 1, 1, 1, 1);
    CGContextSetLineWidth(ctx, 4.0);

    float fw, fh;
    rect = rectRect;
    float ovalWidth = 12;
    float ovalHeight = 12;

    if (ovalWidth == 0 || ovalHeight == 0) {
        CGContextAddRect(ctx, rect);
        return;
    }

    CGContextTranslateCTM (ctx, CGRectGetMinX(rect), CGRectGetMinY(rect));
    CGContextScaleCTM (ctx, ovalWidth, ovalHeight);
    fw = CGRectGetWidth (rect) / ovalWidth;
    fh = CGRectGetHeight (rect) / ovalHeight;
    CGContextMoveToPoint(ctx, fw, fh/2);
    CGContextAddArcToPoint(ctx, fw, fh, fw/2, fh, 1);
    CGContextAddArcToPoint(ctx, 0, fh, 0, fh/2, 1);
    CGContextAddArcToPoint(ctx, 0, 0, fw/2, 0, 1);
    CGContextAddArcToPoint(ctx, fw, 0, fw, fh/2, 1);
    CGContextClosePath(ctx);

    CGContextFillPath(ctx);
    CGContextStrokePath(ctx);

}

2 个答案:

答案 0 :(得分:50)

绘制路径时,通过抚摸或填充路径,图形上下文会将其路径重置为空。因此,在您调用CGContextFillPath后,上下文没有中风路径。

您可以使用CGContextDrawPath函数在一次调用中同时执行这两项操作,而不是尝试填充路径然后对其进行描述:

CGContextDrawPath(ctx, kCGPathFillStroke);

kCGPathFillStroke常量告诉Core Graphics填充路径,然后抚摸它。

另一方面,您可以使用UIBezierPathUIColor来大幅减少代码量:

-(void)drawRect:(CGRect)rect {
    [[UIColor colorWithWhite:0 alpha:0.4] setFill];
    [[UIColor whiteColor] setStroke];

    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rectRect cornerRadius:12];
    path.lineWidth = 4;
    [path fill];
    [path stroke];
}

答案 1 :(得分:0)

Swift 5 版本。重要部分:

library(dplyr)
library(tidyr)

DT %>% 
    mutate(UNIQUEID = row_number()) %>% 
    mutate_all(as.character) %>% 
    pivot_longer(cols = -UNIQUEID) %>% 
    mutate(name = stringr::str_to_upper(name)) %>% 
    filter(!is.na(value)) %>% 
    pivot_wider(names_from = name, values_from = value) %>% 
    type.convert(as.is=TRUE) %>% select(-UNIQUEID)