#import "MyView.h" @implementation MyView - (void)drawRect:(CGRect)rect { //获取图形上下文(drawrect方法中已经创建好图形上下文) CGContextRef context = UIGraphicsGetCurrentContext(); // [self drawLineWithContext:context]; // [self drawShapeCircleWithContext:context]; // [self drawCircleWithContext:context]; // [self drawTextWithContext:context]; // [self drawImageWithContext:context]; [self drawGradientWithContext:context]; CGContextRelease(context); } //绘制渐变 - (void)drawGradientWithContext:(CGContextRef)context { /* space 颜色空间 components 颜色数组 locations 渐变范围 count locations个数 */ //创建颜色空间 CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); //颜色数组 CGFloat components[12] = { 1.f,0.f,0.f,1.f, 0.f,0.f,1.f,1.f, // 0.f,1.f,0.f,1.f }; CGFloat locations[2] = {0.f,1.f}; CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, components, locations, 2); //绘制线性渐变 // CGContextDrawLinearGradient(context, gradient, CGPointMake(0, 0), CGPointMake(self.bounds.size.width, self.bounds.size.height), kCGGradientDrawsAfterEndLocation); //绘制径向渐变 /* startPoint:起始圆心位置 endPoint:结束圆心位置 startRadius:起始圆的半径 endRadius:结束圆的半径 */ CGPoint startPoint = CGPointMake(100, 200); CGPoint endPoint = CGPointMake(150, 150); CGContextDrawRadialGradient(context, gradient, startPoint, 0, endPoint, 200, kCGGradientDrawsAfterEndLocation); //释放内存 CGColorSpaceRelease(colorSpace); //释放内存 CGGradientRelease(gradient); } //绘制图片 - (void)drawImageWithContext:(CGContextRef)context { UIImage *image = [UIImage imageNamed:@"1.png"]; //以平铺的形式绘制图片 // [image drawAsPatternInRect:self.bounds]; //在某个点绘制图片,图片大小为图片实际大小 // [image drawAtPoint:CGPointMake(100, 100)]; //在某个区域绘制图片 [image drawInRect:CGRectMake(100, 100, 200, 200)]; } //绘制文字 - (void)drawTextWithContext:(CGContextRef)context { NSString *str = @"宝山宝山宝山宝山宝山宝山宝山宝山宝山宝山宝山宝山宝山宝山宝山宝山宝山宝山宝山宝山宝山宝山宝山宝山宝山宝山宝山宝山宝山宝山宝山宝山宝山宝山宝山宝山"; //在某个点绘制文字,不会自动换行 // [str drawAtPoint:CGPointMake(50, 50) withAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:18],NSForegroundColorAttributeName:[UIColor redColor]}]; //在一个区域内绘制文字,会自动换行 [str drawInRect:CGRectMake(50, 50, 200, 300) withAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:18],NSForegroundColorAttributeName:[UIColor redColor]}]; } //绘制圆 - (void)drawCircleWithContext:(CGContextRef)context { CGContextMoveToPoint(context, 200, 200); //(x,y) 圆心 //radius:半径 //startAngle:起始角度 //endAngle:结束角度 //clockwise 1代表逆时针 0顺时针 CGContextAddArc(context, 200, 200, 120, 0, M_PI, 1); CGContextDrawPath(context, kCGPathFill); CGContextMoveToPoint(context, 200, 200); [[UIColor greenColor]setFill]; CGContextAddArc(context, 200, 200, 120, M_PI, M_PI_2, 1); CGContextDrawPath(context, kCGPathFill); CGContextMoveToPoint(context, 200, 200); [[UIColor redColor]setFill]; CGContextAddArc(context, 200, 200, 120, M_PI_2, 0, 1); CGContextDrawPath(context, kCGPathFill); } //绘制矩形 - (void)drawShapeCircleWithContext:(CGContextRef)context { //添加矩形 CGRect rect = CGRectMake(100, 100, 200, 150); CGContextAddRect(context, rect); //设置填充颜色 // CGContextSetRGBFillColor(context, 1.f, 0.f, 0.f, 1.f);等效于 [[UIColor redColor]setFill]; //设置线条颜色 // CGContextSetRGBStrokeColor(context, 0.f, 1.f, 0.f, 1.f); [[UIColor greenColor]setStroke]; //设置线条宽度 CGContextSetLineWidth(context, 10.f); CGContextDrawPath(context, kCGPathFillStroke); //添加一组矩形 CGRect rects[2] = {CGRectMake(0, 0, 50, 50),CGRectMake(50, 50, 50, 50)}; CGContextAddRects(context, rects, 2); //绘制矩形 CGContextDrawPath(context, kCGPathStroke); //填充一个矩形 // CGContextFillRect(context, CGRectMake(100, 400, 100, 100)); //绘制椭圆 CGContextAddRect(context, CGRectMake(100, 30, 200, 150)); CGContextAddEllipseInRect(context, CGRectMake(100, 300, 200, 150)); CGContextDrawPath(context, kCGPathStroke); } //绘制线 - (void)drawLineWithContext:(CGContextRef)context { //移动到点 CGContextMoveToPoint(context, 50, 50); //添加到指定点的线 CGContextAddLineToPoint(context, 200, 200); CGContextAddLineToPoint(context, 200, 50); // CGPoint points[2] = {CGPointMake(0, 0),CGPointMake(0, 100)}; // CGContextAddLines(context, points, 2); //设置绘制属性 //设置线条颜色(设置透明度会影响绘制性能) CGContextSetRGBStrokeColor(context, 1.f, 0.f, 0.f, 1.0); // 等效CGContextSetStrokeColorWithColor(context, [[UIColor redColor]CGColor]); //设置填充颜色 CGContextSetRGBFillColor(context, 0.f, 1.f, 0.f, 1.0); //设置线条宽度 CGContextSetLineWidth(context, 1); //设置端点样式 kCGLineCapRound:圆角 kCGLineCapSquare:平角 CGContextSetLineCap(context, kCGLineCapRound); //设置线条连接点样式 kCGLineJoinRound:圆角,kCGLineJoinBevel:平角 kCGLineJoinMiter:尖角 CGContextSetLineJoin(context, kCGLineJoinRound); /** 绘制虚线: phase:偏移(相位),一般偏移指定为0 lengths:数组 count:数组元素个数 */ CGFloat lengths[2] = {20.f,10.f}; CGContextSetLineDash(context, 0, lengths, 2); //关闭路径 CGContextClosePath(context); //绘制路径 /* kCGPathStroke : 只绘制路径 kCGPathFill : 只填充路径 kCGPathFillStroke : 绘制路径并填充 */ CGContextDrawPath(context, kCGPathFillStroke); } @end
时间: 2024-10-15 20:25:02