美文网首页
Quartz 2D 简单使用1--绘制简单图形

Quartz 2D 简单使用1--绘制简单图形

作者: DylanPP | 来源:发表于2018-05-11 11:21 被阅读14次

Quartz 2D使用

直线

//funciotn 1
- (void)drawRect:(CGRect)rect {
      CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 5.0);
    CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
   //拼接路径
    CGContextMoveToPoint(context, 0, 0);
    CGContextAddLineToPoint(context, 100, 100);
    //CGContextMoveToPoint+CGContextAddLineToPoint画线后面不能跟CGContextAddLines,会画成两组无关联的折线。
    //CGContextAddLines指定多个位置点,第一个点默认为起始点,CGContextMoveToPoint指定起始点也无效
    //CGPoint pos[3] ={CGPointMake(0, 0), CGPointMake(50, 100), CGPointMake(100, 200)};
    //CGContextAddLines(context, pos, 3);
    //绘制路径
    //CGContextStrokePath(context);
    CGContextDrawPath(context, kCGPathStroke);
    
}
//funciotn 2
- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    //Bezier描述路径
    UIBezierPath *path = [UIBezierPath bezierPath];
    //设置起点
    [path moveToPoint:CGPointMake(100, 100)];
    //添加一根线Line到某个点
    [path addLineToPoint:CGPointMake(150, 250)];
    //画第二根线
    [path moveToPoint:CGPointMake(100, 100)];
    [path addLineToPoint:CGPointMake(250, 150)];
    
    //设置线宽
    CGContextSetLineWidth(context, 20);
    //设置线的连接样式
    CGContextSetLineJoin(context, kCGLineJoinBevel);
    //设置线的顶角样式
    CGContextSetLineCap(context, kCGLineCapRound);// 圆角线条
    //设置线条颜色
    [[UIColor greenColor] set];
    //把路径添加到上下文
    CGContextAddPath(context, path.CGPath);
    //把上下文当中绘制的内容渲染到跟View关联的layer
    CGContextStrokePath(context);
}

矩形

CGContextAddRect

圆弧

//funciotn 1
- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextAddArc(context, 300, 150, 50, 0, M_PI*2, 1);
//    [[UIColor redColor] set];
    CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
    CGContextStrokePath(context);
}
//fucntion 2 
- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextAddArc(context, 300, 150, 50, 0, M_PI*2, 1);//圆心+半径+初始角度+顺逆时针
//    [[UIColor redColor] set];
    CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
    CGContextStrokePath(context);
}
//fucntion 3
- (void)drawRect:(CGRect)rect { 
    CGContextRef context = UIGraphicsGetCurrentContext();
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(350, 150)];
    [path addQuadCurveToPoint:CGPointMake(450, 150) controlPoint:CGPointMake(400, 0)];
    path.lineWidth = 5.0;
    CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
    CGContextAddPath(context, path.CGPath);
    CGContextStrokePath(context);
}

扇形

- (void)drawRect:(CGRect)rect {
    NSArray *dataAry = @[@14,@16,@40,@30];
    CGPoint center = CGPointMake(500, 150);
    CGFloat radius = 50;
    CGFloat startA = 0;
    CGFloat endA = 0;
    CGFloat angle =0;
    for (NSNumber *num in dataAry) {
        startA = endA;
        angle = num.floatValue/100.0 *M_PI*2; //角度
        endA  = startA+angle;
        UIBezierPath *path = [UIBezierPath bezierPath];
        [path addArcWithCenter:center radius:radius startAngle:startA endAngle:endA clockwise:YES];
         [path addLineToPoint:center];
        [[self randomColor] set];
        [path fill];
//        CGContextSetFillColorWithColor(context, [self randomColor].CGColor);
//        CGContextAddPath(context, path.CGPath);
//        CGContextFillPath(context);
       
    }
}

正弦曲线

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 5.f);
    CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
    UIBezierPath *path = [UIBezierPath bezierPath];
    CGPoint startP = CGPointMake(0, 350);
    [path moveToPoint:startP];
    //    Y = Asin(ωX+φ)+ h;
    //    φ(初相位):决定波形与X轴位置关系或横向移动距离(左加右减)
    //    ω:决定周期(最小正周期T=2π/|ω|)
    //    A:决定峰值(即纵向拉伸压缩的倍数)
    //    h:表示波形在Y轴的位置关系或纵向移动距离(上加下减)
//sin
    for (CGFloat x =0.0; x<self.bounds.size.width; x++) {
        CGFloat y = sinf(x/180*M_PI)*10+350;
        [path addLineToPoint:CGPointMake(x, y)];
    }
    CGContextAddPath(context, path.CGPath);
   
//cos
    UIBezierPath *path1 = [UIBezierPath bezierPath];
    CGPoint startP1 = CGPointMake(0, 450);
    [path1 moveToPoint:startP1];
    for (CGFloat x =0.0; x<self.bounds.size.width; x++) {
        CGFloat y = cosf(x/180*M_PI)*10+450;
        [path1 addLineToPoint:CGPointMake(x, y)];
        
    }
    CGContextAddPath(context, path1.CGPath);
    CGContextStrokePath(context);
    
}

好记性不如烂笔头。

END

微博@迪达拉君
GithubZhaoBinLe

相关文章

  • Quartz 2D 简单使用1--绘制简单图形

    Quartz 2D使用 直线 矩形 略 CGContextAddRect 圆弧 扇形 正弦曲线 好记性不如烂笔头。...

  • iOS开发-Quartz 2D基础篇

    Quartz 2D是一个二维图形绘制引擎,支持iOS环境和Mac OS X环境。我们可以使用Quartz 2D A...

  • iOS--Quartz 2D(下)

    1.Quartz 2D是什么? •Quartz 2D以PDF的规范为基础的图形库,用来绘制二维文字和图形,允许相同...

  • 15 使用 Canvas 绘图

    本章内容 理解 元素 绘制简单的 2D 图形 使用 WebGL 绘制 3D 图形 这个元素负责在页面中设定一个区域...

  • Core Image学习(swift版本)

    利用Quartz 2D我们可以绘制各类图形、图像,功能确实强大。用过photoshop的朋友都知道,使用photo...

  • IOS中使用Quartz 2D绘制虚线

    IOS中使用Quartz 2D绘制虚线

  • Quartz 2D绘图

    概况 Quartz 2D是一个二维图形绘制引擎,支持iOS环境和Mac OS X环境。我们可以使用Quartz 2...

  • Quartz2D

    一、简介 Quartz 2D是一个二维图形绘制引擎,支持iOS环境和Mac OS X环境。我们可以使用Quartz...

  • 关于Quartz 2D绘图的简单使用

    Quartz 2D是一个二维图形绘制引擎,支持iOS环境和Mac OS X环境,Quartz 2D的API可以实现...

  • iOS学习笔记--Quartz2D

    Quartz 2D是一个二维绘图引擎,同时支持iOS和Mac系统。 Quartz 2D能完成的工作: 绘制图形 :...

网友评论

      本文标题:Quartz 2D 简单使用1--绘制简单图形

      本文链接:https://www.haomeiwen.com/subject/ioqfdftx.html