M
M
Max Onflic2014-09-13 22:56:12
Objective-C
Max Onflic, 2014-09-13 22:56:12

How to draw a line with Core Graphics?

RfLNNvQglsk.jpg

- (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];
    
    
    
}

Draw with the sun. It is not clear how to implement it in such a way that it is like in paint under windows

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
M
Max Onflic, 2014-10-14
@maxonflic

decided

_
_ _, 2014-09-14
@AMar4enko

Why did you comment out lastPoint = currentPoint; ? This is exactly why it doesn't work, it seems.

M
ManWithBear, 2014-09-14
@ManWithBear

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 question

Ask a Question

731 491 924 answers to any question