高效地绘制一个大的可滚动区域

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。问题是这看起来效率很低。重新绘制地图大约需要半秒,这让一切都变得迟钝。

我试过以下方法:

总而言之,我的问题是是否有优化 CoreGraphics 绘图的方法,或者是否有其他更有效的绘图方法。

应该不需要计算可见矩形,这是由 UIScrollView 完成的。

Scrollview Programming Guide

此外 class documentation :管理滚动视图中显示的内容绘制的对象应该平铺内容的子视图,以便没有视图超过屏幕的大小。当用户在滚动视图中滚动时,此对象应根据需要添加和删除子视图。