2013-12-20 189 views
0

這就是我用Bezier路徑繪製類似popover窗口的方式。 BOOLEAN bAbajo使繪圖窗口向上。如果沒有bAbajo,它指向向上:重新繪製NSBezierPath

- (void)drawRect:(NSRect)dirtyRect 
{ 
    NSRect contentRect = NSInsetRect([self bounds], LINE_THICKNESS, LINE_THICKNESS); 
    NSBezierPath *path = [NSBezierPath bezierPath]; 
    if (_bAbajo) 
    { 
     [path removeAllPoints]; 
     [path moveToPoint:NSMakePoint(_arrowX, NSMinY(contentRect))]; 
     [path lineToPoint:NSMakePoint(_arrowX + ARROW_WIDTH/2, NSMinY(contentRect) + ARROW_HEIGHT)]; 
     [path lineToPoint:NSMakePoint(NSMaxX(contentRect) - CORNER_RADIUS, NSMinY(contentRect) + ARROW_HEIGHT)]; 
     NSPoint bottomRightCorner = NSMakePoint(NSMaxX(contentRect), NSMinY(contentRect) + ARROW_HEIGHT); 
     [path curveToPoint:NSMakePoint(NSMaxX(contentRect), NSMinY(contentRect) + ARROW_HEIGHT + CORNER_RADIUS) 
     controlPoint1:bottomRightCorner controlPoint2:bottomRightCorner]; 
     [path lineToPoint:NSMakePoint(NSMaxX(contentRect), NSMaxY(contentRect) - CORNER_RADIUS)]; 
     NSPoint topRightCorner = NSMakePoint(NSMaxX(contentRect), NSMaxY(contentRect)); 
     [path curveToPoint:NSMakePoint(NSMaxX(contentRect) - CORNER_RADIUS, NSMaxY(contentRect) + CORNER_RADIUS) controlPoint1:topRightCorner controlPoint2:topRightCorner]; 
     [path lineToPoint:NSMakePoint(NSMinX(contentRect) + CORNER_RADIUS, NSMaxY(contentRect) + CORNER_RADIUS)]; 
     NSPoint topLeftCorner = NSMakePoint(NSMinX(contentRect), NSMaxY(contentRect)); 
     [path curveToPoint:NSMakePoint(NSMinX(contentRect), NSMaxY(contentRect) - CORNER_RADIUS) controlPoint1:topLeftCorner controlPoint2:topLeftCorner]; 
     [path lineToPoint:NSMakePoint(NSMinX(contentRect), NSMinY(contentRect) + CORNER_RADIUS + ARROW_HEIGHT)]; 
     NSPoint bottomLeftCorner = NSMakePoint(NSMinX(contentRect), NSMinY(contentRect) + ARROW_HEIGHT); 
     [path curveToPoint:NSMakePoint(NSMinX(contentRect) + CORNER_RADIUS, NSMinY(contentRect) + ARROW_HEIGHT) controlPoint1:bottomLeftCorner controlPoint2:bottomLeftCorner]; 
     [path lineToPoint:NSMakePoint(_arrowX - ARROW_WIDTH/2, NSMinY(contentRect) + ARROW_HEIGHT)]; 
    } 
    else 
    { 
     [path removeAllPoints]; 
     [path moveToPoint:NSMakePoint(_arrowX, NSMaxY(contentRect))]; 
     [path lineToPoint:NSMakePoint(_arrowX + ARROW_WIDTH/2, NSMaxY(contentRect) - ARROW_HEIGHT)]; 
     [path lineToPoint:NSMakePoint(NSMaxX(contentRect) - CORNER_RADIUS, NSMaxY(contentRect) - ARROW_HEIGHT)]; 
     NSPoint topRightCorner = NSMakePoint(NSMaxX(contentRect), NSMaxY(contentRect) - ARROW_HEIGHT); 
     [path curveToPoint:NSMakePoint(NSMaxX(contentRect), NSMaxY(contentRect) - ARROW_HEIGHT - CORNER_RADIUS) 
      controlPoint1:topRightCorner controlPoint2:topRightCorner]; 
     [path lineToPoint:NSMakePoint(NSMaxX(contentRect), NSMinY(contentRect) + CORNER_RADIUS)]; 
     NSPoint bottomRightCorner = NSMakePoint(NSMaxX(contentRect), NSMinY(contentRect)); 
     [path curveToPoint:NSMakePoint(NSMaxX(contentRect) - CORNER_RADIUS, NSMinY(contentRect)) 
      controlPoint1:bottomRightCorner controlPoint2:bottomRightCorner]; 
     [path lineToPoint:NSMakePoint(NSMinX(contentRect) + CORNER_RADIUS, NSMinY(contentRect))]; 
     [path curveToPoint:NSMakePoint(NSMinX(contentRect), NSMinY(contentRect) + CORNER_RADIUS) 
      controlPoint1:contentRect.origin controlPoint2:contentRect.origin]; 
     [path lineToPoint:NSMakePoint(NSMinX(contentRect), NSMaxY(contentRect) - ARROW_HEIGHT - CORNER_RADIUS)]; 
     NSPoint topLeftCorner = NSMakePoint(NSMinX(contentRect), NSMaxY(contentRect) - ARROW_HEIGHT); 
     [path curveToPoint:NSMakePoint(NSMinX(contentRect) + CORNER_RADIUS, NSMaxY(contentRect) - ARROW_HEIGHT) 
      controlPoint1:topLeftCorner controlPoint2:topLeftCorner]; 
     [path lineToPoint:NSMakePoint(_arrowX - ARROW_WIDTH/2, NSMaxY(contentRect) - ARROW_HEIGHT)]; 
    } 
    [path closePath]; 
    [[NSColor colorWithDeviceWhite:1 alpha:FILL_OPACITY] setFill]; 
    [path fill]; 
    //[NSGraphicsContext saveGraphicsState]; 

    NSBezierPath *clip = [NSBezierPath bezierPathWithRect:[self bounds]]; 
    [clip appendBezierPath:path]; 
    [clip addClip]; 

    [path setLineWidth:LINE_THICKNESS * 2]; 
    [[NSColor whiteColor] setStroke]; 
    [path stroke]; 

    [NSGraphicsContext restoreGraphicsState]; 
} 

在第一次的drawRect被調用時,一切正常,繪圖去它應該取決於bAbajo布爾的方式。之後,如果我再次調用帶有bAbajo的相反值的drawRect,則會執行正確的代碼(bAbajo具有期望的值,並且通過調試帶有斷點我可以看到它正在執行drawRect中的正確代碼),但窗口是沒有用新的NSBezierPath *路徑更新。任何幫助?

+0

如果在進行任何繪圖之前清除窗口,會發生什麼情況? – user1118321

+0

你是什麼意思「如果我再次調用drawRect?」你從不直接調用drawRect。你的意思是「當系統調用drawRect時,在我調用setNeedsDisplay之後?」 –

+0

是的,你是對的,我的意思是'當我打電話給setNeedsDisplay後系統調用' –

回答

0

事實上,羅布間接給了我答案,我沒有在設置bAbajo之後調用setNeedsDisplay。