我正在尝试在 iPad 的 Quartz 2d 中绘制和填充简单的路径。
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 方法来依次绘制我在第一个方法中定义的每个片段对象。
我不知道为什么它没有画出我想要的形状。请帮帮我。
谢谢!