3

我是 iOS 和 Objective-C 的新手,想了解我做错了什么以及做我想做的事情的最佳方法是什么。

我正在尝试绘制由多边形组成的地图。现在我正在用UIBezierPath它来绘制它。我绘制了大约 88 个多边形moveToPointaddLineToPoint然后用随机颜色填充它。然而,其中一些多边形有超过 1000 条线。整个地图有30,000多条线。我在里面做这个MapViewdrawRect

现在整个项目的结构是这样的:

ViewControllerviewDidLoad启动一个MapView(继承自UIView)并将其添加为子视图,如下所示:

    self.pview = [[MapView alloc]init];
    self.pview.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height );
    [self.view addSubview:pview];

MapView将 csv 文件加载到Poly添加到NSMutableArray. APoly具有 X/Y 坐标(浮动,我正在使用CGFloat)。

MapView的 drawRect 看起来像这样:

- (void)drawRect:(CGRect)rect{
    int oldtemp = 0;
    UIBezierPath* path;
    UIColor* fillColor;
    path = [UIBezierPath bezierPath];
    for (int i = 0; i < [polys count]; i++){
        poly *p = [polys objectAtIndex:i];
        int temp = p.getPolyid.intValue;
        if (temp == oldtemp){
            [path moveToPoint:CGPointMake([p.getx floatValue]+posx, [p.gety floatValue]+posy)]; // more about posx/posy after this code block
        }else{
            [path addLineToPoint:CGPointMake([p.getx floatValue]+posx, [p.gety floatValue]+posy)];
            fillColor = [UIColor blueColor]; // not using a random color here, performance is still bad
            }
        }
    [path closePath];
    [fillColor setFill];
    [path fill];
}

posx并且posyCGFloats。ViewController'stouchesMoved将 x/y 坐标发送到MapView's -(void)moveX:(CGFloat)x Y:(CGFloat)y{。此方法如下所示:

-(void)moveX:(CGFloat)x Y:(CGFloat)y{
    if (x > initx){
        posx += (x - initx);
    }else{
        posx -= (initx - x);
    }
    if (y > inity){
        posy += (y - inity);
    }else{
        posy -= (inity - y);
    }
    initx = x;
    inity = y;
    [self setNeedsDisplay]; //should this be here? My map doesn't redraw without it.
}

所有这些都导致了一张地图,它可能每 2 秒绘制一次,并在其后留下一条闪烁的轨迹。

我怎样才能解决这个问题?

4

0 回答 0