主要的原理包括:
继承UIView ,重载drawrect和重载触摸事件
待实现的功能还有,路径数组保存等。
用可变数据保存path路径
画曲线是通过二次贝塞尔曲线实现的
这里可以得到画图的UIImage对象
UIGraphicsBeginImageContext(self.bounds.size); [self.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *result=UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return result;
#import "testdrow.h" @implementation testdrow UIColor *pick_color; int choose; UIBezierPath *mpath ; UIBezierPath *eraser_path; NSTimer *mytime; UIImage *tempdraw; CGContextRef CONTEXT; -(void) settime:(NSTimer *)test_time{ if(choose==1) choose=0; else if(choose==0) choose=1; [self setNeedsDisplay]; } - (IBAction)test_draw:(id)sender { mytime=[NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(settime:) userInfo:nil repeats:YES]; } - (IBAction)eraser:(id)sender { //pick_color=[UIColor alloc]; // [mpath closePath]; pick_color=[UIColor blackColor]; choose=-1; } - (id)initWithFrame:(CGRect)frame { mpath=[[UIBezierPath bezierPath] init]; eraser_path=[[UIBezierPath bezierPath]init]; //pick_color=[[UIColor alloc]init]; pick_color=[UIColor new]; pick_color=[UIColor greenColor]; choose=1; [self setBackgroundColor:[UIColor blackColor]]; self.backgroundColor=[UIColor blackColor]; self = [super initWithFrame:frame]; if (self) { // Initialization code //self init } return self; } /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code } */ //int x,y; //int firstx,firsty; /* - (void) drawRect:(CGRect)rect{ CGContextRef CONTEXT=UIGraphicsGetCurrentContext(); CGFloat temp=25.4; CGContextSetStrokeColorWithColor(CONTEXT,[UIColor redColor].CGColor); CGContextSetLineWidth(CONTEXT,5.8); CGContextMoveToPoint(CONTEXT,firstx,firsty); CGContextAddLineToPoint(CONTEXT, x, y); //CGContextAddLines(CONTEXT, //, <#size_t count#>) CGContextStrokePath(CONTEXT); printf("drawRect\n"); } */ -(void) drawRect1:(CGRect)rect{ switch(choose){ case 1: case -1: [tempdraw drawInRect:rect]; break; case 0: //CONTEXT=UIGraphicsGetCurrentContext(); //[self drawRect2:rect]; break; } } - (void)drawRect:(CGRect)rect { // pick_color=[UIColor alloc]; // UIGraphicsBeginImageContext(self.frame.size); mpath.lineWidth = 15.0; mpath.lineCapStyle = kCGLineJoinRound; //线条拐角 mpath.lineJoinStyle = kCGLineCapRound; CGContextRef CONTEXT=UIGraphicsGetCurrentContext(); // UIGraphicsBeginImageContext(SELF.CGSizeMake); CGContextSetBlendMode(CONTEXT, kCGBlendModeNormal); CGContextSetShadowWithColor(CONTEXT,CGSizeMake(1, 1),18.f, [pick_color CGColor]); UIColor *color =pick_color; //[UIColor greenColor]; [color set]; //设置线条颜色 UIColor *back_color=[UIColor blackColor]; //UIBezierPath* apath =mpath; //[UIBezierPath bezierPath]; //终点处理 switch(choose){ case 1:[mpath stroke]; break; case -1: [eraser_path stroke]; break; case 0: [back_color set]; break; } //Draws line 根据坐标点连线 } static CGPoint midpoint(CGPoint p0, CGPoint p1) { return (CGPoint) { (p0.x + p1.x) / 2.0, (p0.y + p1.y) / 2.0 }; } - (UIImage *) getimage{ UIGraphicsBeginImageContext(self.bounds.size); [self.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *result=UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return result; } - (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ UITouch *touch=[touches anyObject]; CGPoint point=[touch locationInView:self]; CGPoint mpoint=midpoint(lastpoint, point); switch(choose){ case 1:[mpath addQuadCurveToPoint:mpoint controlPoint:lastpoint]; break; case -1: [eraser_path addQuadCurveToPoint:mpoint controlPoint:lastpoint]; break; } lastpoint=point; int tempx=point.x; int tempy=point.y; NSLog(@"touches MOVE%d %d \n",tempx,tempy); UIGraphicsBeginImageContext(self.frame.size); [self.layer renderInContext:UIGraphicsGetCurrentContext()]; CONTEXT=UIGraphicsGetCurrentContext(); [tempdraw drawInRect:self.frame]; //[self drawRect2:self.frame]; UIImage *result=UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); tempdraw=result; [self setNeedsDisplay]; } - (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ //[super touchesBegan:<#touches#> withEvent:<#event#>]; // touches uitouch // mpath=[[UIBezierPath bezierPath] init]; if(choose==0){ [mytime invalidate]; choose=1; } CGPoint point=[[touches anyObject] locationInView:self]; firstx=point.x; firsty=point.y; switch(choose){ case 1: [mpath moveToPoint:point]; break; case -1: [eraser_path moveToPoint:point]; } lastpoint=point; //NSLog(@"%d %d \n",x,y); [self setNeedsDisplay]; } -(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ // [mpath closePath]; } @end
ios 继承UIView实现自定义视图——实现画图,布布扣,bubuko.com
时间: 2024-10-11 07:36:29