Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 07-04-2011, 10:36 PM   #1 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 68
mokargas is on a distinguished road
Default Help with ending a drawing state on a UIView

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.
mokargas is offline   Reply With Quote
Old 07-05-2011, 07:47 PM   #2 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 68
mokargas is on a distinguished road
Default

Do I need to do the drawing inside my DrawingViewController, then save that into my HighlightingView?
mokargas is offline   Reply With Quote
Reply

Bookmarks

Tags
cg drawing, drawrect, uiview

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 349
9 members and 340 guests
apatsufas, chemistry, lendo, leostc, Leslie80, lzwasyc, MarkC, SamorodovAlex, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,664
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Leslie80
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:33 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0