//绘图 继承与UIView
- (void)drawRect:(CGRect)rect
{
//绘制图片
[self drawImage];
//绘制文本
[self drawText];
//绘制线
[self drawLine];
//矩形
[self drawRecttang];
[self drawArc];
}
//弧线
-(void)drawArc{
//得到上下文
CGContextRef ref = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(ref, [UIColor redColor].CGColor);
CGContextMoveToPoint(ref, 150, 150);
//参数 圆心 半径 顺时针逆时针
//饼型图自定义
//所占的比例可以自定义
CGContextAddArc(ref, 150, 150, 100, 0, 100*M_PI /180, 0);
CGContextFillPath(ref);
CGContextSetFillColorWithColor(ref, [UIColor blueColor].CGColor);
CGContextMoveToPoint(ref, 150, 150);
CGContextAddArc(ref, 150, 150, 100, 100*M_PI /180, 260*M_PI /180, 0);
CGContextFillPath(ref);
CGContextSetFillColorWithColor(ref, [UIColor yellowColor].CGColor);
CGContextMoveToPoint(ref, 150, 150);
CGContextAddArc(ref, 150, 150,100 , 260*M_PI /180, 360*M_PI /180, 0);
CGContextFillPath(ref);
//抛物线
CGContextMoveToPoint(ref, 50, 200);
CGContextAddQuadCurveToPoint(ref, 100, 400, 150, 200);
CGContextStrokePath(ref);
//Core Graphics/OpenGL ES 绘图系统
}
//矩形
-(void)drawRecttang{
//得到上下文
CGContextRef ref = UIGraphicsGetCurrentContext();
//<#CGFloat blur#> 最后一个参数代表的是阴影的羽化程度
//CGContextSetShadow(ref, CGSizeMake(30, 30), 20);
CGContextSetShadowWithColor(ref, CGSizeMake(30, 30), 20, [UIColor yellowColor].CGColor);
//填充颜色
CGContextSetFillColorWithColor(ref, [UIColor magentaColor].CGColor);
//矩形
CGContextAddRect(ref, CGRectMake(50, 50, 200, 200));
CGContextAddEllipseInRect(ref, CGRectMake(50, 50, 200, 200));
//只是划线 不能与填充前后执行 执行完前面的之后 命令就停止绘图
//CGContextStrokePath(ref);
//填充
//CGContextFillPath(ref);
//同时划线填充
CGContextDrawPath(ref, kCGPathFillStroke);
}
//线
-(void)drawLine{
//得到上下文
CGContextRef ref = UIGraphicsGetCurrentContext();
//线的颜色
CGContextSetStrokeColorWithColor(ref, [UIColor magentaColor].CGColor);
//线宽
CGContextSetLineWidth(ref, 10);
//相交时角的样式
CGContextSetLineJoin(ref, kCGLineJoinRound);
//画虚线 10 是起始位置
CGFloat lengths[] = {20,20};//实线 虚线 20 10
CGContextSetLineDash(ref, 10 , lengths, 2);
//虚线的地方是圆角
CGContextSetLineCap(ref, kCGLineCapRound);
//移动起点 画笔的起点
CGContextMoveToPoint(ref, 50, 50);
//添加一条线
CGContextAddLineToPoint(ref, 250, 50);
CGContextAddLineToPoint(ref, 150, 250);
//CGContextAddLineToPoint(ref, 50, 50);
//封闭图形
CGContextClosePath(ref);
//开始划线
CGContextStrokePath(ref);
}
//文本
-(void)drawText{
NSString * str = @"西门吹雪大战叶孤城,陆小凤,花满楼,司空摘星,木道人。";
[str drawInRect:self.bounds withAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:30.0],NSForegroundColorAttributeName:[UIColor blueColor]}];
}
//图片
-(void)drawImage{
UIImage * image1 = [UIImage imageNamed:@"5"];
[image1 drawInRect:self.bounds];
UIImage * image2 = [UIImage imageNamed:@"32_16"];
//叠加效果
/*
kCGBlendModeNormal,
kCGBlendModeMultiply,
kCGBlendModeScreen,
kCGBlendModeOverlay,
kCGBlendModeDarken,
kCGBlendModeLighten,
kCGBlendModeColorDodge,
kCGBlendModeColorBurn,
kCGBlendModeSoftLight,
kCGBlendModeHardLight,
kCGBlendModeDifference,
kCGBlendModeExclusion,
kCGBlendModeHue,
kCGBlendModeSaturation,
kCGBlendModeColor,
kCGBlendModeLuminosity,
*/
[image2 drawAtPoint:CGPointMake(0, 0) blendMode:kCGBlendModeExclusion alpha:0.5];
}