高效地绘制一个大的可滚动区域
Efficiently draw a large scrollable area
我正在开发一款使用六边形网格布局的简单游戏。网格非常大(宽度和高度几千像素)。我需要能够在 scrollView 中滚动和缩放它,并且有很多单独的六边形。我已经在 CoreGraphics 中编写了绘图代码。六边形是在其视图的 drawRect:
方法中绘制的。为每个六边形调用此绘图代码:
- (void)drawInContext:(CGContextRef)context colour:(UIColor *)colour size:(CGSize)size {
CGFloat width = size.width;
CGFloat height = size.height;
CGFloat x = self.offset.x;
CGFloat y = self.offset.y;
CGContextMoveToPoint(context, (width/2)+x, y);
CGContextAddLineToPoint(context, width+x, (height / 4)+y);
CGContextAddLineToPoint(context, width+x, (height * 3 / 4)+y);
CGContextAddLineToPoint(context, (width / 2)+x, height+y);
CGContextAddLineToPoint(context, x, (height * 3 / 4)+y);
CGContextAddLineToPoint(context, x, (height / 4)+y);
CGContextClosePath(context);
CGContextSetFillColorWithColor(context, colour.CGColor);
CGContextSetStrokeColorWithColor(context, [[UIColor whiteColor] CGColor]);
CGContextDrawPath(context, kCGPathFillStroke);
NSString *text = [NSString stringWithFormat:@"I:%ld\nR:%ld\nC:%ld", self.creationIndex, self.row, self.column];
[text drawAtPoint:CGPointMake(self.offset.x+20, self.offset.y+20) withAttributes:@{NSForegroundColorAttributeName: [UIColor blackColor], NSFontAttributeName: [UIFont systemFontOfSize:[UIFont systemFontSize]]}];
}
当需要更改时(如六边形更改颜色),我会在视图上调用 setNeedsDisplay
。问题是这看起来效率很低。重新绘制地图大约需要半秒,这让一切都变得迟钝。
我试过以下方法:
- 计算scrollView的可见矩形,只绘制那部分。当缩放到不同的矩形时,这会导致问题,因为只有目标矩形被绘制,导致黑色 space 在滚动的部分显示。
- 在六边形上设置一个标志,表示需要更新,只绘制发生变化的六边形。这导致只有更改的六边形可见,因为
drawRect:
似乎在执行绘图操作之前将视图填充为黑色,而不是将先前的图像留在那里并在顶部绘制更改的六边形。
- 使用 UIKit 构建六边形网格。这太慢了,因为有数百个单独的视图。
总而言之,我的问题是是否有优化 CoreGraphics 绘图的方法,或者是否有其他更有效的绘图方法。
应该不需要计算可见矩形,这是由 UIScrollView 完成的。
此外 class documentation :管理滚动视图中显示的内容绘制的对象应该平铺内容的子视图,以便没有视图超过屏幕的大小。当用户在滚动视图中滚动时,此对象应根据需要添加和删除子视图。
我正在开发一款使用六边形网格布局的简单游戏。网格非常大(宽度和高度几千像素)。我需要能够在 scrollView 中滚动和缩放它,并且有很多单独的六边形。我已经在 CoreGraphics 中编写了绘图代码。六边形是在其视图的 drawRect:
方法中绘制的。为每个六边形调用此绘图代码:
- (void)drawInContext:(CGContextRef)context colour:(UIColor *)colour size:(CGSize)size {
CGFloat width = size.width;
CGFloat height = size.height;
CGFloat x = self.offset.x;
CGFloat y = self.offset.y;
CGContextMoveToPoint(context, (width/2)+x, y);
CGContextAddLineToPoint(context, width+x, (height / 4)+y);
CGContextAddLineToPoint(context, width+x, (height * 3 / 4)+y);
CGContextAddLineToPoint(context, (width / 2)+x, height+y);
CGContextAddLineToPoint(context, x, (height * 3 / 4)+y);
CGContextAddLineToPoint(context, x, (height / 4)+y);
CGContextClosePath(context);
CGContextSetFillColorWithColor(context, colour.CGColor);
CGContextSetStrokeColorWithColor(context, [[UIColor whiteColor] CGColor]);
CGContextDrawPath(context, kCGPathFillStroke);
NSString *text = [NSString stringWithFormat:@"I:%ld\nR:%ld\nC:%ld", self.creationIndex, self.row, self.column];
[text drawAtPoint:CGPointMake(self.offset.x+20, self.offset.y+20) withAttributes:@{NSForegroundColorAttributeName: [UIColor blackColor], NSFontAttributeName: [UIFont systemFontOfSize:[UIFont systemFontSize]]}];
}
当需要更改时(如六边形更改颜色),我会在视图上调用 setNeedsDisplay
。问题是这看起来效率很低。重新绘制地图大约需要半秒,这让一切都变得迟钝。
我试过以下方法:
- 计算scrollView的可见矩形,只绘制那部分。当缩放到不同的矩形时,这会导致问题,因为只有目标矩形被绘制,导致黑色 space 在滚动的部分显示。
- 在六边形上设置一个标志,表示需要更新,只绘制发生变化的六边形。这导致只有更改的六边形可见,因为
drawRect:
似乎在执行绘图操作之前将视图填充为黑色,而不是将先前的图像留在那里并在顶部绘制更改的六边形。 - 使用 UIKit 构建六边形网格。这太慢了,因为有数百个单独的视图。
总而言之,我的问题是是否有优化 CoreGraphics 绘图的方法,或者是否有其他更有效的绘图方法。
应该不需要计算可见矩形,这是由 UIScrollView 完成的。
此外 class documentation :管理滚动视图中显示的内容绘制的对象应该平铺内容的子视图,以便没有视图超过屏幕的大小。当用户在滚动视图中滚动时,此对象应根据需要添加和删除子视图。