- (void)drawLine{
NSLog(@"%@",NSStringFromCGRect(self.bounds));
//1.取得一个跟View相关联的上下文.
CGContextRef ctx = UIGraphicsGetCurrentContext();
//2.描述路径
UIBezierPath *path = [UIBezierPath bezierPath];
//2.1.设置起点
[path moveToPoint:CGPointMake(10, 100)];
//2.1.添加一根线到某个点
[path addLineToPoint:CGPointMake(200, 20)];
//一个路径上面可以画多条线
// [path moveToPoint:CGPointMake(10, 150)];
// [path addLineToPoint:CGPointMake(200, 100)];
//把上一条线的终点当作是下一条线的起点.
[path addLineToPoint:CGPointMake(150, 200)];
//设置上下文的状态
//设置线的宽度
CGContextSetLineWidth(ctx, 10);
//设置线的连接样式
CGContextSetLineJoin(ctx, kCGLineJoinBevel);
//设置顶角的样式
CGContextSetLineCap(ctx, kCGLineCapRound);
//设置线的颜色
[[UIColor greenColor] setStroke];
//3.把路径添加到上下文
CGContextAddPath(ctx, path.CGPath);
//4.把上下文的内容显示View fill stroke
CGContextStrokePath(ctx);
}
- (void)drawQuadCurve{
//1.获取跟View相关联的上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
//2.描述路径
UIBezierPath *path = [UIBezierPath bezierPath];
//2.1设置起点
[path moveToPoint:CGPointMake(20, 200)];
//2.2添加一条曲线到某个点.
[path addQuadCurveToPoint:CGPointMake(200, 200) controlPoint:CGPointMake(100, 10)];
CGContextSetLineWidth(ctx, 10);
//3.把路径添加到上下文
CGContextAddPath(ctx, path.CGPath);
//4.把上下文的内容显示出来.
CGContextStrokePath(ctx);
}
- (void)drawrect{
//画矩形.
//1.获取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
//2.描述路径
UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(50, 50, 100, 100)];
//3.把路径添加到上下文
CGContextAddPath(ctx, path.CGPath);
[[UIColor yellowColor] set];
//4.把上下文的内容显示
CGContextFillPath(ctx);
}
//cornerRadius:圆角半径--> 可以画圆
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(50, 50, 100, 100) cornerRadius:20];
[path stroke];
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(50, 50, 100, 100)];
[[UIColor blueColor] set];
//[path fill];
[path stroke];
//ArcCenter:圆心
//radius:圆的半径
//startAngle:开始角度//起始点圆的最右侧.
//endAngle:截至角度
//clockwise:顺时针还是逆时针 YES:顺时针 NO:逆时针
CGPoint center = CGPointMake(rect.size.width * 0.5, rect.size.height * 0.5);
CGFloat radius = rect.size.width * 0.5 - 10;
CGFloat startA = 0;
CGFloat endA = -M_PI_2;
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startA endAngle:endA clockwise:NO];
[path addLineToPoint:center];
[[UIColor blueColor] set];
//封闭路径
//[path closePath];
//fill,会自动把路径给关闭
[path fill];
- (void)drawRect:(CGRect)rect {
//1.加载图片
UIImage *image = [UIImage imageNamed:@"001"];
//绘制出来的图片,是保持原来图片
// [image drawAtPoint:CGPointZero];
//把图片填充到这个rect当中.
// [image drawInRect:rect];
//添加裁剪区域 .把超区裁剪区域以外都裁剪掉
// UIRectClip(CGRectMake(0, 0, 50, 50));
// [image drawAsPatternInRect:self.bounds];
[[UIColor blueColor] set];
UIRectFill(CGRectMake(10, 10, 100, 100));
}
- (void)drawText{
NSString *str = @"小码哥小码哥小码哥小码哥";
//AtPoint:文字所画的位置
//withAttributes:描述文字的属性.
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
//设置文字大小
dict[NSFontAttributeName] = [UIFont systemFontOfSize:50];
//设置文字颜色
dict[NSForegroundColorAttributeName] = [UIColor greenColor];
//设置描边宽度
dict[NSStrokeWidthAttributeName] = @2;
//设置描边颜色
dict[NSStrokeColorAttributeName] = [UIColor blueColor];
//设置阴影
NSShadow *shadow = [[NSShadow alloc] init];
//设置阴影的便宜量
shadow.shadowOffset = CGSizeMake(10, 10);
//设置阴影颜色
shadow.shadowColor = [UIColor greenColor];
//设置阴影模糊程序
shadow.shadowBlurRadius = 1;
dict[NSShadowAttributeName] = shadow;
//不会自动换行
[str drawAtPoint:CGPointZero withAttributes:dict];
//会自动换行.
[str drawInRect:self.bounds withAttributes:dict];
}
- (void)drawRect:(CGRect)rect {
//1.获取跟View相关联的上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
//2.描述路径
UIBezierPath *path = [UIBezierPath bezierPath];
//添加横线
[path moveToPoint:CGPointMake(10, 150)];
[path addLineToPoint:CGPointMake(290, 150)];
//把当前的状态保存到图片上下文状态栈里.
CGContextSaveGState(ctx);
[[UIColor redColor] set];
CGContextSetLineWidth(ctx, 10);
//3.把路径添加到上下文
CGContextAddPath(ctx, path.CGPath);
//4.把上下文的内容渲染出来.
CGContextStrokePath(ctx);
path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(150, 10)];
[path addLineToPoint:CGPointMake(150, 290)];
//恢复上下文状态栈
CGContextRestoreGState(ctx);
//3.把路径添加到上下文
CGContextAddPath(ctx, path.CGPath);
//4.把上下文的内容渲染出来.
CGContextStrokePath(ctx);
}
- (void)drawRect:(CGRect)rect {
//1.获得跟View相关联的上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
//2.描述路径
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(-100, -50, 200, 100)];
[[UIColor redColor] set];
//上下文的矩阵操作,必须得要在添加路径之前做操作.
//平移
CGContextTranslateCTM(ctx, 100, 50);
//缩放 当为负数为朝向改方向反转,Quartz坐标系和UIView坐标系不一样,对从pdf获取的图片需要进行处理
CGContextScaleCTM(ctx, 0.5, 0.5);
//旋转
CGContextRotateCTM(ctx, M_PI_4);
//3.把路径 添加到当前上下文
CGContextAddPath(ctx, path.CGPath);
//4.把上下文的内容渲染出来.
CGContextFillPath(ctx);
}