Hey all, I'm not terribly familiar with CG drawing functions and need a little help with ending a swipe-to-draw state.
Here's how my app goes down:
I have a view controller, let's call it DrawingViewController, with the following function, which is called by a UIMenuController item:
Code:
- (void) drawHighlight{
//Create drawing view
HighlightView *hl = [[HighlightView alloc] initWithFrame:self.view.bounds];
//Add to VC's scrollview
[self.scrollView addSubview:hl];
//Store in a nice array
[self.highlightViews addObject:hl];
//Ensure it's on top
[self.scrollView bringSubviewToFront:hl];
//Finish
[hl release];
}
This function instantiates the following class, HighlightView, a sublcass of UIView:
Code:
#import "HighlightView.h"
@implementation HighlightView
@synthesize context, canvas, swiped;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
NSLog(@"%s", __PRETTY_FUNCTION__);
currentStart = CGPointZero;
self.backgroundColor = [UIColor clearColor];
//Make a canvas to draw on
canvas = [[UIImageView alloc] initWithImage:nil];
canvas.frame = self.frame;
[self addSubview:canvas];
}
return self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
swiped = NO;
UITouch *touch = [touches anyObject];
if ([touch tapCount] == 2) {
//canvas.image = nil;
return;
}
currentEnd = [touch locationInView:self];
currentEnd.y -= 20;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
swiped = YES;
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self];
currentPoint.y -= 20;
UIGraphicsBeginImageContext(self.frame.size);
[canvas.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), currentEnd.x, currentEnd.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
canvas.image = UIGraphicsGetImageFromCurrentImageContext();
//UIGraphicsEndImageContext();
currentEnd = currentPoint;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
//How do I stop the drawing?
if ([touch tapCount] == 2)
{
// canvas.image = nil;
return;
}
}
I'm given to understand this is all a pretty standard way of drawing, and it works quite well. When drawHighlight is called, I can draw in a nice red marker all over my ViewController's view like a hyperactive toddler with a shiny new crayon. The only issue is the drawing never stops, and I have no idea how to end drawing on the UIView and close the object so that when I hit the drawHighlight function again, I have a new HighlightView added to the stack. Any ideas?
Basically I want each scribble to be separate UIViews for later manipulation.