Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

Help with ending a drawing state on a UIView

mokargasmokargas Posts: 68Registered Users
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:


- (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:




#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.
Post edited by mokargas on

Replies

Sign In or Register to comment.