Guest User

Untitled

a guest
Jul 17th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 0.0)
  2.  
  3. CGPoint previousPoint;
  4.  
  5. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  6. UITouch *touch = [touches anyObject];
  7.  
  8. previousPoint = [touch locationInView:self];
  9.  
  10. - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
  11.  
  12.  
  13.  
  14. UITouch *touch = [touches anyObject];
  15. currentPoint = [touch locationInView:self];
  16.  
  17. UIGraphicsBeginImageContext(self.frame.size);
  18. //This may not be necessary if you are just erasing, in my case I am
  19. //adding lines repeatedly to an image so you may want to experiment with moving
  20. //this to touchesBegan or something. Which of course would also require the begin
  21. //graphics context etc.
  22.  
  23. [scratchImage.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
  24.  
  25. CGContextSaveGState(UIGraphicsGetCurrentContext());
  26. CGContextSetShouldAntialias(UIGraphicsGetCurrentContext(), YES);
  27. CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
  28. CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
  29. CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.25, 0.25, 0.25, 1.0);
  30. CGMutablePathRef path = CGPathCreateMutable();
  31. CGPathMoveToPoint(path, nil, previousPoint.x, previousPoint.y);
  32. CGPathAddLineToPoint(path, nil, currentPoint.x, currentPoint.y);
  33. CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);
  34. CGContextAddPath(UIGraphicsGetCurrentContext(), path);
  35. CGContextStrokePath(UIGraphicsGetCurrentContext());
  36. scratchImage.image = UIGraphicsGetImageFromCurrentImageContext();
  37. CGContextRestoreGState(UIGraphicsGetCurrentContext());
  38. UIGraphicsEndImageContext();
  39. previousPoint = currentPoint;
  40. }
Add Comment
Please, Sign In to add comment