2012-07-10 73 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

您應該剪輯的背景下這樣做,應該要修剪的圖紙前,然後還原後