//
// DrawingView.h
// SampleDay7-1
//
// Created by bit on 11. 5. 24..
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import
typedef enum {
ShapeTypeLine,
ShapeTypeRect,
ShapeTypeEllipse
} ShapeType;
@interface DrawingView : UIView {
CGPoint firstLocation;
CGPoint lastLocation;
UIColor *currentColor;
ShapeType shapeType;
CGRect currentRect;
CGRect redrawRect;
}
@property (nonatomic, assign) CGPoint firstLocation;
@property (nonatomic, assign) CGPoint lastLocation;
@property (nonatomic, retain) UIColor *currentColor;
@property (nonatomic, assign) ShapeType shapeType;
@property (nonatomic, readonly) CGRect currentRect;
@property (nonatomic, assign) CGRect redrawRect;
-(void)drawRect:(CGRect)rect ;
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
@end
/ DrawingView.m
// SampleDay7-1
//
// Created by bit on 11. 5. 24..
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "DrawingView.h"
@implementation DrawingView
@synthesize firstLocation;
@synthesize lastLocation;
@synthesize currentColor;
@synthesize shapeType;
@synthesize currentRect;
@synthesize redrawRect;
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code.
}
return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code.
}
*/
- (void)dealloc {
[super dealloc];
}
-(CGRect)currentRect {
return CGRectMake( MIN(firstLocation.x, lastLocation.x),
MIN(firstLocation.y, lastLocation.y),
fabsf(firstLocation.x - lastLocation.x ),
fabsf(firstLocation.y - lastLocation.y )
);
}
-(void)drawRect:(CGRect)rect {
[super drawRect:rect];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, currentColor.CGColor);
CGContextSetFillColorWithColor(context, currentColor.CGColor);
switch (shapeType) {
case ShapeTypeLine:
CGContextMoveToPoint(context, firstLocation.x, firstLocation.y);
CGContextAddLineToPoint(context, lastLocation.x, lastLocation.y);
CGContextStrokePath(context);
break;
case ShapeTypeRect:
CGContextAddRect(context, [self currentRect]);
CGContextDrawPath(context, kCGPathFillStroke);
break;
case ShapeTypeEllipse:
CGContextAddEllipseInRect(context, [self currentRect]);
CGContextDrawPath(context, kCGPathFillStroke);
break;
default:
break;
}
//NSLog( @"drawRect" );
//NSLog(@"firstLocation = %f", firstLocation.x );
//NSLog(@"lastLocation = %f", lastLocation.x );
}
-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *) event {
NSLog(@"touchesBegang");
UITouch *touch = [touches anyObject];
firstLocation = [touch locationInView:self];
lastLocation = [touch locationInView:self];
redrawRect = CGRectUnion(redrawRect, self.currentRect);
[self setNeedsDisplayInRect:redrawRect];
}
-(void)touchesMoved:(NSSet*)touches withEvent:(UIEvent *) event {
NSLog(@"touchesMoved");
UITouch *touch = [touches anyObject];
lastLocation = [touch locationInView:self];
redrawRect = CGRectUnion(redrawRect, self.currentRect);
[self setNeedsDisplayInRect:redrawRect];
}
-(void)touchesEnded:(NSSet*)touches withEvent:(UIEvent *) event {
NSLog(@"touchesEnded");
UITouch *touch = [touches anyObject];
lastLocation = [touch locationInView:self];
redrawRect = CGRectUnion(redrawRect, self.currentRect);
[self setNeedsDisplayInRect:redrawRect];
}
@end
댓글 없음:
댓글 쓰기
참고: 블로그의 회원만 댓글을 작성할 수 있습니다.