object-c图形绘制总结

图形绘制:直线,贝塞尔曲线,多边形,圆形,扇形

1.绘制图形最开始我们要做的是:

CGContextRef ctx = UIGraphicsGetCurrentContext();//获得当前的上下文
CGContextSaveGState(ctx);//保存空白图形到上下文栈
2.开始绘制

//拼接路径绘制直线
    CGContextMoveToPoint(ctx, 100, 100);
    CGContextAddLineToPoint(ctx, 200, 200);

//绘制三角形
    CGContextMoveToPoint(ctx, 10, 100);
    CGContextAddLineToPoint(ctx, 10, 200);
    CGContextAddLineToPoint(ctx, 300, 300);
    //关闭路径
    CGContextClosePath(ctx);
    

//圆心,坐标,半径,开始角度,结束角度,角度,方向,关闭路径可以绘制扇形
    CGContextAddArc(ctx, 180, 300, 150, 0, M_PI, 1);
//绘制四边形
    CGContextAddRect(ctx, CGRectMake(80, 200, 200, 200));

//绘制曲线
    CGContextMoveToPoint(ctx, 10, 100);//绘制起点
    CGContextAddQuadCurveToPoint(ctx, 200, 50, 150, 150);//绘制二次曲线
    CGContextAddCurveToPoint(ctx, 0, 100, 0, 300, 0, 300);

3.绘制图形的一些简单设置

    CGContextSetLineWidth(ctx, 10);//设置宽度
    CGContextSetStrokeColorWithColor(ctx, [UIColor cyanColor].CGColor);//设置颜色
    CGContextSetLineCap(ctx, kCGLineCapRound);//设置直线两头为圆滑
    const CGFloat length[] = {5,10,3,4,5,6,7,7};//实线,空白长度
    CGContextSetLineDash(ctx, 0, length, 7);//绘制虚线
    CGContextSetLineCap(ctx, kCGLineCapRound);//线头为圆
    [[UIColor blueColor] setStroke];//设置画笔颜色
    [[UIColor brownColor] setFill];//填充颜色
4.绘制

CGContextStrokePath(ctx);//渲染,完成绘制

贝赛尔曲线的绘制(实现折线图):

 //创建呗赛尔曲线
    UIBezierPath *path = [UIBezierPath bezierPath];
    //枚举器这个数组里面存得时坐标CGPoint
    [array enumerateObjectsUsingBlock:^(NSValue *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        //获得数组对象转换为CGPoint
        CGPoint point = [obj CGPointValue];
        //添加到呗赛尔曲线
        [path addLineToPoint:point];
        
        //创建转折圆点
        CGRect rect = CGRectMake(point.x-3, point.y-3, 10, 10);
//        UIBezierPath *arcPoint = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:5];//这个也可以画圆点
        UIBezierPath *arcPoint = [UIBezierPath bezierPathWithOvalInRect:rect];
        //拼接路径
        [path appendPath:arcPoint];
    }];
 
    //创建模型图层
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    //设置frmae
    shapeLayer.frame = CGRectMake(0, 0, 375, 667);
    //设置颜色
    shapeLayer.fillColor = [UIColor blueColor].CGColor;
    shapeLayer.strokeColor = [UIColor redColor].CGColor;

    shapeLayer.path = path.CGPath;//将曲线放进图层
    //添加图层
    [self.layer addSublayer:shapeLayer];



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值