2013-01-10 37 views
5

使用核心圖形,是否有可能在路徑內部劃線?相對於劃線路徑的外側一半和內側一半的線重量而言?UIView drawRect:是否有可能在路徑中敲擊?

如果說視圖的一部分位於屏幕邊緣而部分位置不在屏幕邊緣,那麼控制筆畫的可見厚度會更容易。屏幕邊緣的部分被切斷,而完全在屏幕上的視圖邊緣看起來更厚(如果筆畫可見,則兩邊都是這樣)。

enter image description here

回答

10

夾到你面前的道路行程吧。

+0

謝謝!你可以看看我的「答案」,並發表評論。這種方法不起作用。中風根本不吸引。什麼是剪輯路徑,方法名稱? – Mrwolfy

+0

你很近。 [CGContextClip()']的文檔(https://developer.apple.com/library/ios/documentation/graphicsimaging/reference/CGContext/Reference/reference.html#//apple_ref/c/func/CGContextClip)說:「確定新的剪切路徑後,函數將上下文的當前路徑重置爲空路徑。」這就解釋了爲什麼'CGContextDraw'沒有繪製任何東西 - 你需要在中風之前再次將路徑添加到上下文中。 –

+0

使用['CGPath'](https://developer.apple.com/library/ios/#documentation/graphicsimaging/Reference/CGPath/Reference/reference.html#//apple_ref/c/tdef/CGPathRef)或[ 'UIBezierPath'](http://developer.apple.com/library/ios/#documentation/uikit/reference/UIBezierPath_class/Reference/Reference.html)會使這個過程變得更簡單 - 你可以創建一次路徑,然後使用它兩次。 –

6

此畫無招:

- (void)drawRect:(CGRect)rect 
{ 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetStrokeColorWithColor(context, [UIColor darkGrayColor].CGColor); 
    CGContextSetLineWidth(context, 14); 
    CGRect rrect = CGRectMake(CGRectGetMinX(rect), CGRectGetMinY(rect), CGRectGetWidth(rect), CGRectGetHeight(rect)); 
    CGFloat radius = 30; 
    CGFloat minx = CGRectGetMinX(rrect), midx = CGRectGetMidX(rrect), maxx = CGRectGetMaxX(rrect); 
    CGFloat miny = CGRectGetMinY(rrect), midy = CGRectGetMidY(rrect), maxy = CGRectGetMaxY(rrect); 
    CGContextMoveToPoint(context, minx, midy); 
    CGContextAddArcToPoint(context, minx, miny, midx, miny, radius); 
    CGContextAddArcToPoint(context, maxx, miny, maxx, midy, radius); 
    CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius); 
    CGContextAddArcToPoint(context, minx, maxy, minx, midy, radius); 
    CGContextClosePath(context); 
    CGContextClip(context); 
    CGContextDrawPath(context, kCGPathStroke); 
} 

編輯:這是(按正確答案)什麼工作:

- (void)drawRect:(CGRect)rect 
{ 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetStrokeColorWithColor(context, [UIColor darkGrayColor].CGColor); 
    CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor); 
    CGContextSetLineWidth(context, 14); 
    CGRect pathRect = CGRectMake(10, 10, rect.size.width -20, rect.size.height -20); 
    CGPathRef path = [UIBezierPath bezierPathWithRoundedRect:pathRect cornerRadius:20].CGPath; 
    CGContextAddPath(context, path); 
    CGContextClip(context); 
    CGContextAddPath(context, path); 
    CGContextDrawPath(context, kCGPathEOFillStroke); 

} 
相關問題