Answer the question
In order to leave comments, you need to log in
How to draw a line with Core Graphics?
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.drawImage];
if (touch.tapCount == 1) {
lastPoint = [touch locationInView:self.drawImage];
} else {
drawImage.image = nil;
}
FirstPoint=currentPoint;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.drawImage];
UIGraphicsBeginImageContext(self.drawImage.frame.size);
CGRect drawRect = CGRectMake(0.0f, 0.0f,
self.drawImage.frame.size.width,
self.drawImage.frame.size.height);
[drawImage.image drawInRect:drawRect];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 1.0f);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0f, 0.0f, 0.0f, 1.0f);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 1.0f);
CGContextStrokePath(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// lastPoint = currentPoint;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
if (UITouchPhaseEnded) {
CGPoint currentPoint = [touch locationInView:self.drawImage];
endPoint=currentPoint;
}
// [[NSNotificationCenter defaultCenter] postNotificationName:@"metro" object:nil];
}
Answer the question
In order to leave comments, you need to log in
Why did you comment out lastPoint = currentPoint; ? This is exactly why it doesn't work, it seems.
Make 2 canvases. One for temporary changes (Canvas A) and one for those that have already been applied (Canvas B).
Canvas A is transparent and lies on top of canvas B. In
your way, draw a line on canvas A, each time you change the position of your finger, completely clear this canvas and draw a line.
Once the touch ends, copy the changes from canvas A to canvas B.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question