2012-07-10 72 views
0

开始了解核心图形。我正在绘制一个填充和中风的路径,但我无法将其解决。任何人都可以告诉我我在这里做错了什么?核心图形CGContextClip - Xcode

此代码位于我的UIView子类的drawRect方法中。

//Draw a closed path with rounded corners, a fill and a stroke. 
CGContextRef context = UIGraphicsGetCurrentContext(); 
CGContextSetLineWidth(context, 4.0); 
CGContextSetStrokeColorWithColor(context,[UIColor blueColor].CGColor); 
CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor); 
//fill 
CGContextMoveToPoint(context, 10, 10); 
CGContextAddLineToPoint(context, 10, 100); 
CGContextAddArcToPoint(context, 10,140, 30,140, 20); 
CGContextAddLineToPoint(context, 200, 140); 
CGContextAddArcToPoint(context, 240,140, 240,100, 20); 
CGContextAddLineToPoint(context, 240, 10); 
CGContextClosePath(context); 
CGContextFillPath(context); 
//stroke 
CGContextMoveToPoint(context, 10, 10); 
CGContextAddLineToPoint(context, 10, 100); 
CGContextAddArcToPoint(context, 10,140, 30,140, 20); 
CGContextAddLineToPoint(context, 200, 140); 
CGContextAddArcToPoint(context, 240,140, 240,100, 20); 
CGContextAddLineToPoint(context, 240, 10); 
CGContextClosePath(context); 
CGContextStrokePath(context); 
CGContextBeginPath(context); 
//clip?? 
CGContextMoveToPoint(context, 10, 10); 
CGContextAddLineToPoint(context, 10, 100); 
CGContextAddArcToPoint(context, 10,140, 30,140, 20); 
CGContextAddLineToPoint(context, 200, 140); 
CGContextAddArcToPoint(context, 240,140, 240,100, 20); 
CGContextAddLineToPoint(context, 240, 10); 
CGContextClosePath(context); 
CGContextClip(context); 

回答

0

上下文CGContextClipCGContextStrokePath之前和之后CGContextClosePath您应该使用。这就够了。无需编写额外的代码。请尝试像下面这样..

CGContextClosePath(context); 
    CGContextClip(context); 
    CGContextStrokePath(context); 

我认为这会对您有所帮助。

1

您应该剪辑的背景下这样做,应该要修剪的图纸前,然后还原后