Drawing incrementally in a UIView (iPhone)

后端 未结 5 1457
甜味超标
甜味超标 2020-12-02 14:09

As far as I have understood so far, every time I draw something in the drawRect: of a UIView, the whole context is erased and then redrawn.

So I have to do something

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-02 14:21

    You can save your CGPath as a member of your class. And use that in the draw method, you will only need to create the path when the dots change but not every time the view is redraw, if the dots are incremental, just keep adding the ellipses to the path. In the drawRect method you will only need to add the path

    CGContextAddPath(context,dotsPath);
    
    -(CGMutablePathRef)createPath
    {
        CGMutablePathRef dotsPath =  CGPathCreateMutable();
    
        for (Drop *drop in myPoints) {
            CGPathAddEllipseInRect ( dotsPath,NULL,
                CGRectMake(drop.point.x - drop.size/2, drop.point.y - drop.size/2, drop.size, drop.size));
        }
    
    return dotsPath;
    }
    

提交回复
热议问题