iOS动画 —— 自界说View

手机软件开发 2024-9-16 07:21:49 113 0 来自 中国
创建路径

1.使用CGContextRef创建,如CGContextAddArc

这种方式是直接对图形上下文举行操作,常用的方法有:
之前的讲解文章:https://www.jianshu.com/p/f2841b6b9260
   CGContextBeginPath //开始画路径   CGContextMoveToPoint //移动到某一点   CGContexAddLineToPoint //画直线   CGContexAddCurveToPoint //画饼图   CGContexAddEllipseInRect //画椭圆   CGContexAddArc //画圆   CGContexAddRect //画方框   CGContexClosePath //封闭当前路径2.使用CGPathRef创建,如CGPathAddArc

使用方法一绘制路径后将清空图形上下文,假如我们想保存路径来复用,可以使用Quartz提供的CGPath函数聚集来创建可复用的路径对象。
   CGPathCreateMutable   CGPathMoveToPoint   CGPathAddLineToPoint   CGPathAddCurveToPoint   CGPathAddEllipseInRect   CGPathAddArc   CGPathAddRect   CGPathCloseSubpath 这些函数和上面方法一的逐一对应,可代替之使用。
3.使用UIBezierPath创建,如bezierPathWithOvalInRect

UIBezierPath存在于UIKit中,是对路径绘制的封装,和CGContextRef雷同,优点是更面向对象,我们可以像操作普通对象一样对其举行操作。
  在自界说View的时间,一样平常使用UIBezierPath来创建路径就能根本满足我们的需求,保举使用。
之前的讲解文章:https://www.jianshu.com/p/fc5a1e0372fa
- (void)drawRectCGRect)rect // 重写drawRect方法{   // 1.初始化图形相应的UIBezierPath对象   UIBezierPath* aPath = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 200, 200)]; // 2.   // 2.设置一些修饰属性    aPath.lineWidth = 8.0;   //路径的止境外形,   aPath.lineCapStyle = kCGLineCapRound;    //路径的毗连点外形   aPath.lineJoinStyle = kCGLineCapRound;    UIColor *color = [UIColor colorWithRed:0 green:0 blue:0.7 alpha:1];   [color set];    // 3.出发点   [aPath moveToPoint:CGPointMake(20, 20)];   // 4.绘制线条   [aPath addLineToPoint:CGPointMake(100, 100)];   [aPath stroke]; // 渲染,完成绘制}画图的焦点步调


  • 获取上下文
  • 绘制路径
  • 添加路径到上下文
  • 修改图外形态参数
  • 渲染上下文
演示着实现步调

1. 使用CGContextRef创建路径

//获取上下文 CGContextRef ctx = UIGraphicsGetCurrentContext(); //绘制路径: 圆形(中央坐标200、200、半径100、出发点弧度0、止境弧度2PI、画的方向0逆1正) CGContextAddArc(ctx, 200, 200, 100, 0, M_PI * 2, 0); //修改图外形态参数 CGContextSetRGBStrokeColor(ctx, 0.5, 0.5, 0.9, 1.0);//笔颜色 CGContextSetLineWidth(ctx, 10);//线条宽度 //渲染上下文 CGContextStrokePath(ctx);2. 使用CGPathRef创建路径

//获取上下文 CGContextRef ctx = UIGraphicsGetCurrentContext(); //创建可变路径 CGMutablePathRef path = CGPathCreateMutable(); //添加圆形到路径中(所在路径、不举行变更操作、中央坐标200、200、出发点弧度0、止境弧度2PI、画的方向0逆1正) CGPathAddArc(path, NULL, 200, 200, 100, 0, M_PI * 2, 1); //将路径添加到上下文 CGContextAddPath(ctx, path); //修改图外形态参数 CGContextSetRGBStrokeColor(ctx, 0.5, 0.5, 0.9, 1.0);//笔颜色 CGContextSetLineWidth(ctx, 10);//线条宽度 //渲染上下文 CGContextStrokePath(ctx);3. 使用UIBezierPath创建路径

//创建路径 UIBezierPath * path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(100, 100, 200, 200)]; //修改图外形态参数 [[UIColor colorWithRed:0.5 green:0.5 blue:0.9 alpha:1.0] setStroke];//笔颜色 [path setLineWidth:10];//线条宽度 //渲染 [path stroke];自界说view的步调

步调一:新建一个类,继承UIView类。

步调二:重载drawRect方法,在这个方法中举行画图。


  • 新建CircleView类,继承UIView类
  • 在CircleView.m中重载drawRect方法
- (void)drawRectCGRect)rect { }

  • 画一个圆
- (void)drawRectCGRect)rect {    UIBezierPath * path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(100, 100, 200, 200)];    [[UIColor colorWithRed:0.5 green:0.5 blue:0.9 alpha:1.0] setStroke];    [path setLineWidth:10];    [path stroke];}

  • 创建CircleView的实例添加到视图中
- (void)viewDidLoad {     [super viewDidLoad];     CircleView * cricleView = [[CircleView alloc]initWithFrame:self.view.bounds];     [self.view addSubview:cricleView]; }

  • 效果图

    1.png
您需要登录后才可以回帖 登录 | 立即注册

Powered by CangBaoKu v1.0 小黑屋藏宝库It社区( 冀ICP备14008649号 )

GMT+8, 2025-4-18 14:33, Processed in 0.140070 second(s), 35 queries.© 2003-2025 cbk Team.

快速回复 返回顶部 返回列表