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

如何去除圆角矩形贝塞尔路径

  •  0
  • NightFury  · 技术社区  · 11 年前

    我是第一次使用Core Graphics进行编程,所以不太清楚如何解决这个问题。

    我正在为UITableviewCells绘制圆形矩形边框路径以及渐变和笔划作为背景视图。一切都很顺利,除了图中多余的黑色角落。

    Figure 我不知道他们为什么要表演,也不知道他们到底是什么。有人能帮我吗?谢谢

    创建单元格的代码

    #import "CustomCellBackground.h"
    .
    .
    .
    
    -(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
    
        UITableViewCell *cell = [tableView 
                                 dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
            cell.backgroundView = [[CustomCellBackground alloc] init];
            cell.selectedBackgroundView = [[CustomCellBackground alloc]init];
        }
    
        // Configure the cell.
    
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.textLabel.backgroundColor = [UIColor clearColor];
        return cell;
    
    }
    

    在CustomCellBackground.m中

    - (void)drawRect:(CGRect)rect
    {
        // Drawing code
        CGContextRef context = UIGraphicsGetCurrentContext();
    
        CGPathRef path = [[UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:10.0] CGPath];
        CGContextAddPath(context, path);
        CGContextClip(context);
        //CGContextSetLineJoin(context, kCGLineJoinRound);
    
        drawLinearGradientWithFourColors(context, self.bounds);
    
        CGContextSaveGState(context);
    
        CGContextSetStrokeColorWithColor(context,[UIColor whiteColor].CGColor);
        CGContextSetLineWidth(context, 1.0);
        CGContextAddPath(context, path);
        CGContextStrokePath(context);
        CGContextRestoreGState(context);
    
       }
    
    void drawLinearGradientWithFourColors(CGContextRef context, CGRect rect)
    {
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    
        CGFloat locations[] = {0.0, 0.2, 0.5, 1.0};
    
        CGFloat colors[16] = {
            85/255.0, 85/255.0, 85/255.0, 1.0,
            45/255.0, 45/255.0, 45/255.0, 1.0,
            22/255.0, 22/255.0, 22/255.0, 1.0,
            0, 0, 0, 1.0
        };
    
        CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, colors, locations, 4);
    
        CGPoint startPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMinY(rect));
        CGPoint endPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMaxY(rect));
    
        CGContextSaveGState(context);
        CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
        CGContextRestoreGState(context);
        CGGradientRelease(gradient);
        CGColorSpaceRelease(colorSpace);
    }
    
    2 回复  |  直到 11 年前
        1
  •  1
  •   user2254860    11 年前

    当您初始化视图时,为视图和视图的层设置Opaque参数。

    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            [self setOpaque:NO];
            [self.layer setOpaque:NO];
        }
        return self;
    }
    

        2
  •  0
  •   Community CDub    7 年前

    我在谷歌上搜索并粘贴了下面的代码。有关更多信息,请参阅 link :

    使用以下代码:

    typedef enum  {
        CustomCellBackgroundViewPositionTop, 
        CustomCellBackgroundViewPositionMiddle, 
        CustomCellBackgroundViewPositionBottom,
        CustomCellBackgroundViewPositionSingle
    } CustomCellBackgroundViewPosition;
    
    @interface CustomCellBackgroundView : UIView {
        UIColor *borderColor;
        UIColor *fillColor;
        CustomCellBackgroundViewPosition position;
    }
    
        @property(nonatomic, retain) UIColor *borderColor, *fillColor;
        @property(nonatomic) CustomCellBackgroundViewPosition position;
    @end
    
    
    - (void)drawRect:(CGRect)rect {
        // Drawing code
        CGContextRef c = UIGraphicsGetCurrentContext();
        CGContextSetFillColorWithColor(c, [fillColor CGColor]);
        CGContextSetStrokeColorWithColor(c, [borderColor CGColor]);
    
        if (position == CustomCellBackgroundViewPositionTop) {
            CGContextFillRect(c, CGRectMake(0.0f, rect.size.height - 10.0f, rect.size.width, 10.0f));
            CGContextBeginPath(c);
            CGContextMoveToPoint(c, 0.0f, rect.size.height - 10.0f);
            CGContextAddLineToPoint(c, 0.0f, rect.size.height);
            CGContextAddLineToPoint(c, rect.size.width, rect.size.height);
            CGContextAddLineToPoint(c, rect.size.width, rect.size.height - 10.0f);
            CGContextStrokePath(c);
            CGContextClipToRect(c, CGRectMake(0.0f, 0.0f, rect.size.width, rect.size.height - 10.0f));
        } else if (position == CustomCellBackgroundViewPositionBottom) {
            CGContextFillRect(c, CGRectMake(0.0f, 0.0f, rect.size.width, 10.0f));
            CGContextBeginPath(c);
            CGContextMoveToPoint(c, 0.0f, 10.0f);
            CGContextAddLineToPoint(c, 0.0f, 0.0f);
            CGContextStrokePath(c);
            CGContextBeginPath(c);
            CGContextMoveToPoint(c, rect.size.width, 0.0f);
            CGContextAddLineToPoint(c, rect.size.width, 10.0f);
            CGContextStrokePath(c);
            CGContextClipToRect(c, CGRectMake(0.0f, 10.0f, rect.size.width, rect.size.height));
        } else if (position == CustomCellBackgroundViewPositionMiddle) {
            CGContextFillRect(c, rect);
            CGContextBeginPath(c);
            CGContextMoveToPoint(c, 0.0f, 0.0f);
            CGContextAddLineToPoint(c, 0.0f, rect.size.height);
            CGContextAddLineToPoint(c, rect.size.width, rect.size.height);
            CGContextAddLineToPoint(c, rect.size.width, 0.0f);
            CGContextStrokePath(c);
            return; // no need to bother drawing rounded corners, so we return
        }
    
        // At this point the clip rect is set to only draw the appropriate
        // corners, so we fill and stroke a rounded rect taking the entire rect
    
        CGContextBeginPath(c);
        addRoundedRectToPath(c, rect, 10.0f, 10.0f);
        CGContextFillPath(c);  
    
        CGContextSetLineWidth(c, 1);  
        CGContextBeginPath(c);
        addRoundedRectToPath(c, rect, 10.0f, 10.0f);  
        CGContextStrokePath(c); 
    }
    
    static void addRoundedRectToPath(CGContextRef context, CGRect rect,
                                    float ovalWidth,float ovalHeight)
    
    {
        float fw, fh;
    
        if (ovalWidth == 0 || ovalHeight == 0) {// 1
            CGContextAddRect(context, rect);
            return;
        }
    
        CGContextSaveGState(context);// 2
    
        CGContextTranslateCTM (context, CGRectGetMinX(rect),// 3
                               CGRectGetMinY(rect));
        CGContextScaleCTM (context, ovalWidth, ovalHeight);// 4
        fw = CGRectGetWidth (rect) / ovalWidth;// 5
        fh = CGRectGetHeight (rect) / ovalHeight;// 6
    
        CGContextMoveToPoint(context, fw, fh/2); // 7
        CGContextAddArcToPoint(context, fw, fh, fw/2, fh, 1);// 8
        CGContextAddArcToPoint(context, 0, fh, 0, fh/2, 1);// 9
        CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 1);// 10
        CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1); // 11
        CGContextClosePath(context);// 12
    
        CGContextRestoreGState(context);// 13
    }