0

I am new to Objective-C programming.

My problem is about drawing lines with touchesMoved.

My code is like this:

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetShouldAntialias(context, YES);

    CGContextSetLineWidth(context, 7.0f);

    CGContextSetRGBStrokeColor(context, 0.7, 0.7, 0.7, 1.0);

    CGContextMoveToPoint(context, self.touchedPoint2.x, self.touchedPoint2.y);

    CGContextAddLineToPoint(context, self.touchedPoint.x, self.touchedPoint.y); 

    CGContextDrawPath(context,kCGPathFillStroke);
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    self.touchedPoint = [[touches anyObject] locationInView:self];

    [self setNeedsDisplay];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
   self.touchedPoint2 = [[touches anyObject] locationInView:self];
}
mig
  • 21
  • 3

1 Answers1

1

This is how drawRect: is supposed to work.

You should draw everything into an offscreen buffer (e.g. CGImage or CGLayer) and then use drawRect: only to draw the buffer.

Consider also looking into this question which lists other possibilites.

Community
  • 1
  • 1
Sulthan
  • 128,090
  • 22
  • 218
  • 270