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

2018-05-11  本文已影响14人  DylanPP

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

上一篇 下一篇

猜你喜欢

热点阅读