본문 바로가기
프로그래밍/iOS

간단한 손가락으로 그리기 소스

by 백룡화검 2012. 1. 12.



1. <webViewer.h> --> 아 제 프로젝트라 ;; 파일명 신경쓰지 말아주세요 ㅎ

#import <UIKit/UIKit.h>



@interface webViewer : UIViewController {

    UIImageView *drawImage;

    BOOL mouseMoved;

    BOOL mouseSwiped;

    CGPoint lastPoint;

    CGPoint currentPoint;

}

@property (nonatomic, retain) UIWebView *web;

@end


2. <webViewer.m>

#import "webViewer.h"



@implementation webViewer

@synthesize web;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}


- (void)dealloc

{

    [super dealloc];

}


- (void)didReceiveMemoryWarning

{

    // Releases the view if it doesn't have a superview.

    [super didReceiveMemoryWarning];

    

    // Release any cached data, images, etc that aren't in use.

}


#pragma mark - View lifecycle


- (void)viewDidLoad

{

    [super viewDidLoad];

    // Do any additional setup after loading the view from its nib.

    drawImage = [[UIImageView alloc] initWithImage:nil];

    drawImage.frame = self.view.frame;

    [self.view addSubview:drawImage];

    self.view.backgroundColor = [UIColor lightGrayColor];

    mouseMoved = 0;

}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    

    mouseSwiped = NO;

    UITouch *touch = [touches anyObject];

    

    if ([touch tapCount] == 2) {

        drawImage.image = nil;

        return;

    }

    

    lastPoint = [touch locationInView:self.view];

    lastPoint.y -= 20;

    

}



- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    mouseSwiped = YES;

    

    UITouch *touch = [touches anyObject];   

    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, 1.0);

    CGContextBeginPath(UIGraphicsGetCurrentContext());

    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);

    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);

    CGContextStrokePath(UIGraphicsGetCurrentContext());

    drawImage.image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    

    lastPoint = currentPoint;

    

    mouseMoved++;

    

    if (mouseMoved == 10) {

        mouseMoved = 0;

    }

    

}


- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    

    UITouch *touch = [touches anyObject];

    

    if ([touch tapCount] == 2) {

        drawImage.image = nil;

        return;

    }

    

    

    if(!mouseSwiped) {

        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, 1.0);

        CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);

        CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);

        CGContextStrokePath(UIGraphicsGetCurrentContext());

        CGContextFlush(UIGraphicsGetCurrentContext());

        drawImage.image = UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();

    }

}



- (void)viewDidUnload

{

    [super viewDidUnload];

    // Release any retained subviews of the main view.

    // e.g. self.myOutlet = nil;

}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

    // Return YES for supported orientations

return YES;

}


@end





도움이 되실지는 모르겠짐나 ... 여기에는 소스가 없는것 같아서 올립니다~ ㅎ

즐프 하세요~

출처 (http://www.ifans.com/forums/showthread.php?t=132024)