iOS海哥开发笔记 (海哥原创,Quartz-2d的简单使用 一)


-.基本使用,画线断,首先要自定义一个view,重写

- (void)drawRect:(CGRect)rect方法,然后就可以肆意的话了

  1.画线断,每一句代码都给注释了哦,

    // 1.开启图形上下文

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    //  起始点

    CGContextMoveToPoint(ctx, 20, 20);

     // 画到哪一个点

    CGContextAddLineToPoint(ctx, 20, 100);

    CGContextAddLineToPoint(ctx, 60, 180);

    // 设置线的转折处风格

    CGContextSetLineJoin(ctx, kCGLineJoinRound);

    

    CGContextMoveToPoint(ctx, 60, 60);

    CGContextAddLineToPoint(ctx, 160, 160);

    CGContextAddLineToPoint(ctx, 60, 100);

    // 线的颜色,这句代码可自动识别空心线或者实心线,

    [[UIColor redColor] set];

    

    // 设置线条的宽度

    CGContextSetLineWidth(ctx, 10);

    // 设置圆角

    CGContextSetLineJoin(ctx, kCGLineJoinRound);

    // 渲染

    CGContextStrokePath(ctx);

    // 销毁 非OC对象,要手动内存管理,不用了就销毁

       //  开发技巧,不熟悉的东西不知道释放,只要你觉得不熟,直接敲release,remove之类的,Xcode不报错,不就搞定了吗

    CGContextRelease(cox);


  2. 显示文字

- (void)drawString

{

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    CGRect hRect = CGRectMake(100, 100, 200, 200);

    // 画圆

    CGContextAddRect(ctx, Hrect);

    [[UIColor greenColor] set];

    CGContextFillPath(ctx);

    // 设置属性字符串

    NSString *str = @"我是小海,你还认识我吗???";

    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:

                          [UIColor redColor]          , NSForegroundColorAttributeName,

                          [UIFont systemFontOfSize:30], NSFontAttributeName,nil];

    [str drawInRect:Hrect withAttributes:dict];

}


 3.画贝塞尔曲线

// 画贝塞尔曲线

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    // 贝塞尔曲线的起始位置

    CGContextMoveToPoint(ctx, 100, 50);

    // 设置线的宽度

    CGContextSetLineWidth(ctx, 30);    

 // 参数依次是 上下文,起始点X,起始点Y,中心点x,中心点Y,想想,给你一个点,两边两个点,曲线是不是有了

    CGContextAddQuadCurveToPoint(ctx, 150, 80, 200, 50);

    CGContextStrokePath(ctx);


4.画圆

CGContextRef ctx = UIGraphicsGetCurrentContext();

    CGContextAddArc(ctx, 200, 200, 60, 0, M_2_PI, 1);

    [[UIColor greenColor] set];

    CGContextStrokePath(cox);


5.图形上下文栈,当然你要好好理解

// 1.获取图形上下文

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    

    /*  当用到图形上下文栈的时候,会复制一份放入栈中,当第一个上下文渲染之后,会将栈定的拷贝的那个上下文出栈,替换当前上下文,又是一个全新的不带有属性上下文,所以第二根线渲染之后喝第一根不一样

     */

    // 设置图形上下文栈,拷贝一份放入图形上下文栈中

    CGContextSaveGState(ctx);

    

    // 设置线条的宽度

    CGContextSetLineWidth(ctx, 10);

    // 设置转接圆角

    CGContextSetLineJoin(ctx, kCGLineJoinRound);

    // 设置两头样式

    CGContextSetLineCap(ctx, kCGLineCapRound);

    [[UIColor redColor] set];


    // 第一根线

    CGContextMoveToPoint(ctx, 100, 100);

    CGContextAddLineToPoint(ctx, 150, 200);

    CGContextAddLineToPoint(ctx, 200, 180);

     CGContextStrokePath(ctx);

    

    // 将栈顶上下文出栈,替换当前上下文。

    CGContextRestoreGState(ctx);

    

    // 第二根线

    CGContextMoveToPoint(ctx, 250, 150);

    CGContextAddLineToPoint(ctx, 220, 250);

    CGContextAddLineToPoint(ctx, 280, 220);

    

        // 渲染

    CGContextStrokePath(ctx);



最后祝大家使用愉快,笔者原创,转载注明出处
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值