2013-04-08 95 views
0

我正在編程的第一次使用核心圖形,所以沒有太多的想法,我怎麼能解決問題。如何刪除圓形矩形貝塞爾路徑

我繪製了圓角矩形貝塞爾路徑以及作爲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); 
} 

回答

1

當你初始化你的觀點,setOpaque參數的觀點和看法層。

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

+1

三江源它幫助我:) – NightFury 2013-04-08 09:21:27

+0

歡迎您 – user2254860 2013-04-08 09:27:14

0

我GOOGLE了它和粘貼下面的代碼。欲瞭解更多信息請參閱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 
} 
+0

謝謝您的幫助。但我不想遵循這種方法:) – NightFury 2013-04-08 09:21:00