Created
May 9, 2011 21:26
-
-
Save nicholasjackson/963449 to your computer and use it in GitHub Desktop.
How to Draw with Touches
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma mark<Touch Methods> | |
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { | |
UITouch *touch = [touches anyObject]; | |
if ([touch tapCount] == 2) { | |
drawImage.image = nil; | |
return; | |
} | |
lastPoint = [touch locationInView:self.view]; | |
lastPoint.y -= 20; | |
path = CGPathCreateMutable(); //create our fill path | |
CGPathMoveToPoint(path, NULL, lastPoint.x, lastPoint.y); | |
} | |
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { | |
UITouch *touch = [touches anyObject]; | |
CGPoint currentPoint = [touch locationInView:self.view]; | |
currentPoint.y -= 20; | |
UIGraphicsBeginImageContext(self.view.frame.size); | |
[drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; | |
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); | |
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0); | |
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 0.8); | |
CGContextBeginPath(UIGraphicsGetCurrentContext()); | |
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); | |
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y); | |
CGContextStrokePath(UIGraphicsGetCurrentContext()); | |
drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); | |
UIGraphicsEndImageContext(); | |
// add to our fill path | |
CGPathAddLineToPoint(path, NULL, currentPoint.x, currentPoint.y); | |
lastPoint = currentPoint; | |
} | |
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { | |
UITouch *touch = [touches anyObject]; | |
if ([touch tapCount] == 2) { | |
drawImage.image = nil; | |
return; | |
} | |
UIGraphicsBeginImageContext(self.view.frame.size); | |
[drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; | |
CGPathCloseSubpath(path); | |
CGContextSetRGBFillColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 0.7); | |
CGContextAddPath(UIGraphicsGetCurrentContext(), path); | |
CGContextDrawPath(UIGraphicsGetCurrentContext(), kCGPathEOFill); | |
CGContextFillPath(UIGraphicsGetCurrentContext()); | |
drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); | |
UIGraphicsEndImageContext(); | |
CGPathRelease(path); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment