Quartz2D - 04.利用贝瑟尔路径(UIBezierPath)绘制基本图形
2015-08-05 21:14
423 查看
1.基本形状
// 自定义View #import "DrawView.h" @implementation DrawView // 实现drawRect方法绘图 -(void)drawRect:(CGRect)rect{ // 画线 // [self drawLine1]; // [self drawLine2]; // 画曲线 // [self drawQuadCurve]; // 画形状 // [self drawRect]; // 画圆 // [self drawCircle]; // 画扇形 // [self drawFan]; // 画圆环 [self drawLoop]; } // 画圆环 -(void)drawLoop { // 创建贝瑟尔路径 UIBezierPath *path = [UIBezierPath bezierPath]; // 设定圆心 CGPoint center = CGPointMake(100, 100); // 画弧 [path addArcWithCenter:center radius:50 startAngle:0 endAngle:M_PI * 2 clockwise:YES]; // 画线 [path addLineToPoint:center]; // 关闭路径 [path closePath]; // 设置线宽 path.lineWidth = 10; // 设置画笔颜色 [[UIColor greenColor] setStroke]; // 路径走线 [path stroke]; // 设置填充颜色 [[UIColor redColor] setFill]; // 填充路径 [path fill]; } // 画扇形 -(void)drawFan{ // 创建贝瑟尔路径 UIBezierPath *path = [UIBezierPath bezierPath]; CGPoint center = CGPointMake(100, 100); // 画弧 [path addArcWithCenter:center radius:50 startAngle:0 endAngle:M_PI_2 clockwise:YES]; // 画线 [path addLineToPoint:center]; // 关闭路径 [path closePath]; // 设置画笔颜色 [[UIColor redColor] set]; // 画路径 [path fill]; } // 画圆形 -(void)drawCircle{ // 创建贝瑟尔路径 UIBezierPath *path = [UIBezierPath bezierPath]; // 画线 [path addArcWithCenter:CGPointMake(100, 100) radius:50 startAngle:0 endAngle:M_PI * 2 clockwise:YES]; // 设置画笔颜色 [[UIColor redColor] setStroke]; // 画路径 [path stroke]; } // 画矩形 -(void)drawRect{ // 创建贝瑟尔路径 UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(20, 20, 100, 100)]; // 设置属性 // 设置线宽 path.lineWidth = 20; // 设置交点样式 path.lineJoinStyle = kCGLineJoinRound; // 设置画笔颜色 [[UIColor redColor] setStroke]; // 画路径 [path stroke]; } // 画曲线 -(void)drawQuadCurve{ // 创建贝瑟尔路径 UIBezierPath *path = [UIBezierPath bezierPath]; // 画曲线 // 设置起始点 [path moveToPoint:CGPointMake(10, 100)]; // 画曲线,根据控制点和终点 [path addQuadCurveToPoint:CGPointMake(110, 100) controlPoint:CGPointMake(50, 10)]; // 渲染路径 [path stroke]; } // 画线1 -(void)drawLine1{ // 获取图形上下文 CGContextRef ctx = UIGraphicsGetCurrentContext(); // 创建贝瑟尔路径 UIBezierPath *path = [UIBezierPath bezierPath]; // 移动到初始点 [path moveToPoint:CGPointZero]; // 画线 [path addLineToPoint:CGPointMake(100, 100)]; // 设置线宽 CGContextSetLineWidth(ctx, 10); // 添加路径 CGContextAddPath(ctx, path.CGPath); // 渲染视图 CGContextStrokePath(ctx); } // 画线2 -(void)drawLine2{ // 创建贝瑟尔路径 UIBezierPath *path = [UIBezierPath bezierPath]; // 移动到初始点 [path moveToPoint:CGPointZero]; // 画线 [path addLineToPoint:CGPointMake(100, 100)]; // 设置线宽 path.lineWidth = 20; // 设置圆角 path.lineCapStyle = kCGLineCapRound; // 渲染界面 [path stroke]; } @end
相关文章推荐
- Windows/MFC_三个退出程序消息:WM_CLOSE、WM_DESTROY、WM_QUIT
- UE4蓝图用户指南
- UE4蓝图简介
- Rescue
- iOS之UIView的层次结构变更
- UIPickerView的简单应用——省份/城市选择的实现
- CSS3 Media Query:移动 Web 的完美开端
- 黑马程序员——break和continue语句+函数+函数重载-第4天
- 黑马程序员—JAVA基础—GUI(图形化界面编程)
- iOS之UIView的圆角
- UIView详解
- iOS之UIView的变更动画
- 28.UIScrollView和UIPageController
- HDU 1242 Rescue
- UIScrollView
- iOS之UIImage的三中读取方法
- 【JAVA语言基础】map以value进行排序
- getRequestDispatcher()与sendRedirect()的区别
- iOS之UIImage的三中读取方法
- iOS之UIImageView帧动画