2012-04-25 45 views
0

我想繪製和填充Quartz 2d for iPad的簡單路徑。石英路徑不像我所期望的那樣繪製

從類SPTCutPattern下面的方法定義了我要繪製的路徑:

- (void) initDummyCutPattern { 
NSArray *piece1Path = [[NSArray alloc] initWithObjects: 
         [NSValue valueWithCGPoint:CGPointMake(0, 0)], 
         [NSValue valueWithCGPoint:CGPointMake(3, 0)], 
         [NSValue valueWithCGPoint:CGPointMake(3, 1)], 
         [NSValue valueWithCGPoint:CGPointMake(2, 1)], 
         [NSValue valueWithCGPoint:CGPointMake(2, 2)], 
         [NSValue valueWithCGPoint:CGPointMake(0, 2)], 
         nil]; 
SPTPuzzlePiece *Piece1 = [[SPTPuzzlePiece alloc] initWithWidthInGrid:3 andHeightInGrid:2 andLeftInGrid:0 andTopInGrid:0 andBuilt:YES andPathInGrid:piece1Path]; 

NSArray *piece2Path = [[NSArray alloc] initWithObjects: 
         [NSValue valueWithCGPoint:CGPointMake(3, 0)], 
         [NSValue valueWithCGPoint:CGPointMake(5, 0)], 
         [NSValue valueWithCGPoint:CGPointMake(5, 5)], 
         [NSValue valueWithCGPoint:CGPointMake(4, 5)], 
         [NSValue valueWithCGPoint:CGPointMake(4, 4)], 
         [NSValue valueWithCGPoint:CGPointMake(2, 4)], 
         [NSValue valueWithCGPoint:CGPointMake(2, 3)], 
         [NSValue valueWithCGPoint:CGPointMake(3, 3)], 
         [NSValue valueWithCGPoint:CGPointMake(3, 2)], 
         [NSValue valueWithCGPoint:CGPointMake(2, 2)], 
         [NSValue valueWithCGPoint:CGPointMake(2, 1)], 
         [NSValue valueWithCGPoint:CGPointMake(3, 1)], 
         nil]; 
SPTPuzzlePiece *Piece2 = [[SPTPuzzlePiece alloc] initWithWidthInGrid:3 andHeightInGrid:5 andLeftInGrid:2 andTopInGrid:0 andBuilt:YES andPathInGrid:piece2Path]; 

NSArray *piece3Path = [[NSArray alloc] initWithObjects: 
         [NSValue valueWithCGPoint:CGPointMake(0, 2)], 
         [NSValue valueWithCGPoint:CGPointMake(3, 2)], 
         [NSValue valueWithCGPoint:CGPointMake(3, 3)], 
         [NSValue valueWithCGPoint:CGPointMake(2, 3)], 
         [NSValue valueWithCGPoint:CGPointMake(2, 4)], 
         [NSValue valueWithCGPoint:CGPointMake(4, 4)], 
         [NSValue valueWithCGPoint:CGPointMake(4, 5)], 
         [NSValue valueWithCGPoint:CGPointMake(0, 5)], 
         nil]; 

SPTPuzzlePiece *Piece3 = [[SPTPuzzlePiece alloc] initWithWidthInGrid:4 andHeightInGrid:3 andLeftInGrid:0 andTopInGrid:2 andBuilt:YES andPathInGrid:piece3Path]; 

self.pieces = [[NSArray alloc] initWithObjects:Piece1, Piece2, Piece3, nil]; 
} 

而且從類SPTPuzzlePieceView此方法這是應該繪製的路徑:

- (void)drawRect:(CGRect)rect 
{ 
int widthUnit = self.frame.size.width/self.puzzlePiece.widthInGrid; 
int heightUnit = self.frame.size.height/self.puzzlePiece.heightInGrid; 
CGContextRef context = UIGraphicsGetCurrentContext(); 

CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor); 

CGContextMoveToPoint(context, [[puzzlePiece.pathInGrid objectAtIndex:0] CGPointValue].x*widthUnit, 
        [[puzzlePiece.pathInGrid objectAtIndex:0] CGPointValue].y*heightUnit); 

for (NSValue *point in puzzlePiece.pathInGrid) { 
    CGContextAddLineToPoint(context, [point CGPointValue].x*widthUnit, [point CGPointValue].y*heightUnit); 
} 

CGContextFillPath(context); 
} 

我我得到這三個對象,這絕對不是我期望和想要的:

screenshot

的對象應該是更多(或更少)是這樣的:

shapes I wanted

我很抱歉,這些方法是完全斷章取義,所以我希望你能看到什麼,我想做。 drawRect方法被調用來依次繪製我在第一個方法中定義的每個片段對象。

我不明白爲什麼它不繪製我想要的形狀。請幫幫我。

謝謝!

回答

0

您的第二個和第三個形狀正在初始化爲widthInGridheightInGrid值小於它們的點。這導致在視圖範圍之外繪圖。其他

三兩件事:

  1. 考慮使用UIBezierPath用於創建和繪畫,而不是CGContext...呼叫你的路徑。犯錯誤很難。如果在任何時候用戶必須觸摸這些形狀並拖動它們,其方法將非常方便。
  2. 變量的Objective-C的命名約定是,他們先從低的情況下,他們從類名稱區分開。所以,你會有SPTPuzzlePiece *piece1而不是*Piece1。變量名內的單詞做使用大寫。
  3. drawRect,使用視圖自身bounds得到的寬度和高度,而不是它frame。如果你沒有扭曲你的觀點,這些都是一樣的,但是最好遵循。 bounds總是定義你正在繪製的座標空間。
+0

太謝謝你了!我完全明白我現在做錯了什麼。我一定會考慮其他的建議! – mwiederrecht 2012-04-25 20:12:57

相關問題