/ Published in: iPhone
Expand |
Embed | Plain Text
DrawView.h #import <UIKit/UIKit.h> @interface DrawView : UIView { NSMutableArray *points; } @end ---------------------- .m file: #import "DrawView.h" #define POINT(X) [[self.points objectAtIndex:X] CGPointValue] UIColor *current; @implementation DrawView @synthesize points; - (id)initWithFrame:(CGRect)frame { if ((self = [super initWithFrame:frame])) { // Initialization code current = [UIColor whiteColor]; } return self; } - (void)drawRect:(CGRect)rect { // Drawing code if (!self.points) return; if (self.points.count < 2) return; CGContextRef context = UIGraphicsGetCurrentContext(); [current set]; CGContextSetLineWidth(context, 4.0f); for (int i = 0; i < (self.points.count - 1); i++) { CGPoint pt1 = POINT(i); CGPoint pt2 = POINT(i+1); CGContextMoveToPoint(context, pt1.x, pt1.y); CGContextAddLineToPoint(context, pt2.x, pt2.y); CGContextStrokePath(context); } } - (void)dealloc { [super dealloc]; } #pragma mark - #pragma mark UI Touches #pragma mark - { CGPoint pt = [[touches anyObject] locationInView:self]; } { CGPoint pt = [[touches anyObject] locationInView:self]; [self setNeedsDisplay]; } @end ========================== DrawViewController.m .... - (void)viewDidLoad { [super viewDidLoad]; DrawView *tv = [[DrawView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 240.0f, 240.0f)]; tv.center = CGPointMake(160.0f, 140.0f); [self.view addSubview:tv]; [tv release]; }
You need to login to post a comment.
