我正在尝试在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);
}
我得到这三个对象,这绝对不是我期望和想要的:
对象应该更多(或更少)像这样:
我很抱歉这些方法完全脱离了上下文,所以我希望你能看到我想要做的事情。调用drawRect方法来依次绘制我在第一个方法中定义的每个片段对象。
我无法弄清楚为什么它没有画出我想要的形状。请帮帮我。
谢谢!
答案 0 :(得分:0)
您的第二个和第三个形状正在初始化,widthInGrid
和heightInGrid
值小于其点数。这导致在视图边界外绘制。
其他三件事:
CGContext...
次调用。犯错误更难。如果用户在任何时候必须触摸这些形状并拖动它们,它的containsPoint:
方法将非常方便。SPTPuzzlePiece *piece1
而不是*Piece1
。变量名称中的单词使用大写。drawRect
中,使用视图自己的bounds
来获取宽度和高度,而不是frame
。如果你没有扭曲你的观点,这些将是相同的,但是遵循这个好习惯。 bounds
始终定义您要绘制的坐标空间。