ios 简单绘制

#import "DrawView.h"

#import <CoreGraphics/CoreGraphics.h>

@implementation DrawView




- (void)drawRect:(CGRect)rect {

    

    

//    drawRect: 此方法不能手动调用,可以调用以下方法间接调用

    

//    [self setNeedsDisplay];

    

    CGContextRef context = [self createCobtext];

//    [self drawLineOne:context];

//    

//    [self drawLineTwo:context];

//    

//    [self drawCylye:context];

    

    [self drawBezierPath:context];

    

}


//初始化画布

- (CGContextRef )createCobtext {

    

    

    //    CGContextRef是一个不透明的绘画环境, 简单理解为一块画布

    CGContextRef context = UIGraphicsGetCurrentContext();

    //    设置颜色

    CGContextSetStrokeColorWithColor(context, [UIColor  orangeColor].CGColor);

    //    设置画笔的宽度

    

    CGContextSetLineWidth(context, 5);

    


    

    

    return context;

}

//第一种画线方式

- (void)drawLineOne:(CGContextRef)context {

//    设置起点

    CGContextMoveToPoint(context, 20, 20);


//    设置下一个点

    CGContextAddLineToPoint(context, 200, 200);


    

    CGContextAddLineToPoint(context, 200, 500);

//    绘制

    CGContextStrokePath(context);

    

    

    

    

}


//第二种方式


- (void)drawLineTwo:(CGContextRef )context {

    

//    通过路径来画线

    CGMutablePathRef path = CGPathCreateMutable();

    

//    设置起点

//  CGAffineTransformIdentity表示见当前的viewtransform设为初始值

    CGPathMoveToPoint(path, &CGAffineTransformIdentity, 20, 20);

    

//    添加另一个点

    

    CGPathAddLineToPoint(path, &CGAffineTransformIdentity, 100, 300);

    

//    给画布添加路径,再次路径上画线

    CGContextAddPath(context, path);

    

//    绘制

    CGContextStrokePath(context);

    

    

}


//画圆弧


- (void)drawCylye:(CGContextRef )context {

//    参数!:context

//    参数2:圆心x

//    参数3:圆心y

//    参数4:半径

//    参数5:起始角度

//    参数6:终点角度

//    参数7:0表示顺时针,1表示逆时针

    CGContextAddArc(context, 100, 100, 50, 0, M_PI*2, 0);

//    绘制

    CGContextStrokePath(context);

    

}


//贝塞尔曲线


- (void)drawBezierPath:(CGContextRef)context {

    

    CGContextMoveToPoint(context, 50, 50);

    

//    设置下一点

//    参数1context

//    参数2:端点1x

//    参数3:端点1y

//    参数4:端点2x

//    参数5:端点3y

//    参数6:半径

    

    

    CGContextAddArcToPoint(context, 100, 300, 300, 100, 100);

    

    

    CGContextStrokePath(context);

    

}





@end


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值