2013-02-26 13 views
4

作爲iOS繪圖的初學者,我想繪製一個小小的工具提示容器,它由一個帶有指示箭頭的矩形組成。 我創建了2個UIBezierPaths,並用「appendPath」加入了它們。 我認爲這是正確的做法,但經過1天的努力後,我不得不尋求幫助。 下面是我對現在所在的截圖:作爲一個整體加入UIBezierPath中風

Screenshot of UIBezierPath

正如你所看到的問題很簡單,當我嘗試中風合流通路,它不被撫摸作爲一個整體,但作爲分開的部分。 也許有人可以指出我正確的方向來解決這個問題?

下面的代碼:提前

// Only override drawRect: if you perform custom drawing. 
// An empty implementation adversely affects performance during animation. 
- (void)drawRect:(CGRect)rect 
{ 
    // create indicator path 
    UIBezierPath *indicator = [[UIBezierPath alloc] init]; 
    // corner radius 
    CGFloat radius = self.cornerRadius; 
    // main path 
    UIBezierPath *path; 
    // format frame 
    rect = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height - self.indicatorSize.height); 
    // assign path 
    path = [UIBezierPath bezierPathWithRoundedRect: rect 
           byRoundingCorners:UIRectCornerAllCorners 
             cornerRadii:CGSizeMake(radius, radius)]; 
    // x point 
    CGFloat x = (rect.size.width - self.indicatorSize.width)/2; 

    [indicator moveToPoint:CGPointMake(x, rect.size.height)]; 
    [indicator addLineToPoint:CGPointMake(x + self.indicatorSize.width/2, rect.size.height + self.indicatorSize.height)]; 
    [indicator addLineToPoint:CGPointMake(x + self.indicatorSize.width, rect.size.height)]; 
    // close path 
    [indicator closePath]; 
    // append indicator to main path 
    [path appendPath:indicator]; 

    [[UIColor redColor] setStroke]; 
    [path setLineWidth:2.0]; 
    [path stroke]; 
    [self.fillColor setFill]; 
    [path fill]; 
    // release indicator 
    [indicator release]; 
} 

感謝

回答

6

您必須畫出所有的一個路徑,因爲此刻你告訴它要繪製整個矩形,然後是三角形,這就是它正在做的事情。

從Apple UIBezierPath文檔:

[appendPath:]添加用於創建到所述接收器的路徑的端部在bezierPath路徑的命令。儘管bezierPath中的操作仍可能導致該效果,但此方法並未明確嘗試連接兩個對象中的子路徑。

所以首先它會提醒你矩形,然後它會提醒你三角形沒有試圖加入他們

+0

嗨。感謝您的提示,但是我嘗試過,雖然它似乎工作,它在底部邊框繪製一個雙邊框,我不知道爲什麼你可以在這裏檢查它:http://oi51.tinypic.com/2cxevmd.jpg 。以下是我正在繪製的新代碼:http://pastebin.com/imcvhGdQ。難道我做錯了什麼? – Hidden 2013-02-26 22:16:34

+0

嘗試在0.5,0.5上開始繪圖以避免在半像素上繪圖:有關詳細信息,請參閱此部分:http://www.cocoabuilder.com/archive/cocoa/204399-drawing-1-pixel-perfect-wide-bordered-nsbezierpath .html – iain 2013-02-26 23:48:04

+0

我在這裏回答了這個圖片:http://stackoverflow.com/a/15097830/210171 – nielsbot 2013-07-03 16:13:42