CGContextRef的用法

Quartz 是主要的描画接口,支持基于路径的描画、抗锯齿渲染、渐变填充模式、图像、颜色、坐标空间变换、以及PDF 文档的创建、显示、和分析。UIKit 为Quartz 的图像和颜色操作提供了Objective-C 的封装。Core Animation 为很多UIKit 的视图属性声明的动画效果提供底层支持,也可以用于实现定制的动画。

在调用您提供的drawRect:方法之前,视图对象会自动配置其描画环境,使您的代码可以立即进行描画。作为这些配置的一部分,UIView 对象会为当前描画环境创建一个图形上下文(对应于CGContextRef 封装类型)

 

首先,要画东西,必须先创建一个上下文,创建上下文必须在func drawRect(rect: CGRect)这个函数内执行

我们先来看一下CGContext的用法

 

  CGContextRef context = UIGraphicsGetCurrentContext(); //设置上下文

 

 CGContextMoveToPoint //开始画线

 

 CGContextAddLineToPoint //画直线

 

 CGContextAddEllipseInRect //画一椭圆

 

 CGContextSetLineCap //设置线条终点形状

 

 CGContextSetLineDash //画虚线

 

 CGContextAddRect //画一方框

 

 CGContextStrokeRect //指定矩形

 

 CGContextStrokeRectWithWidth //指定矩形线宽度

 

 CGContextStrokeLineSegments //一些直线

 

 CGContextAddArc //画已曲线 前俩店为中心 中间俩店为起始弧度 最后一数据为0则顺时针画 1则逆时针

 

 CGContextAddArcToPoint(context,0,0, 2, 9, 40);//先画俩条线从point 到 弟1点 , 从弟1点到弟2点的线  切割里面的圆

 

 CGContextSetShadowWithColor //设置阴影

 

 CGContextSetRGBFillColor //这只填充颜色

 

 CGContextSetRGBStrokeColor //画笔颜色设置

 

 CGContextSetFillColorSpace //颜色空间填充

 

 CGConextSetStrokeColorSpace //颜色空间画笔设置

 

CGContextFillRect //补充当前填充颜色的rect

 

 CGContextSetAlaha //透明度

 

 CGContextTranslateCTM //改变画布位置

 

 CGContextSetLineWidth //设置线的宽度

 

 CGContextAddRects //画多个线

 

 CGContextAddQuadCurveToPoint //画曲线

 

 CGContextStrokePath //开始绘制图片

 

 CGContextDrawPath //设置绘制模式

 

 CGContextClosePath //封闭当前线路

 

 CGContextTranslateCTM(context, 0, rect.size.height);    CGContextScaleCTM(context, 1.0, -1.0);//反转画布

 

 CGContextSetInterpolationQuality //背景内置颜色质量等级

 

 CGImageCreateWithImageInRect //从原图片中取小图

 

 

看完这些后话不多说,直接上代码

 

class LineView: UIView {

    var labelTile = ["动态","关注","粉丝","文章","文集"]
    override func drawRect(rect: CGRect) {
        // Drawing code
        //设置上下文
        let context = UIGraphicsGetCurrentContext()
        //设置线的颜色为黑色,透明度为0.3
        CGContextSetRGBStrokeColor(context, 0, 0, 0, 0.3)
        //线的宽度为0.5
        CGContextSetLineWidth(context, 0.5)
        
        //传进来控件的宽度
        let lineW = rect.width
        //每个竖直线条的高度
        let lineH = rect.height
    
        for var i = 1;i<=4;i++ {
    
            //开始画线, x,y 为开始点的坐标
            CGContextMoveToPoint(context, (CGFloat)(i) * (lineW/5) , 10)
            //画直线, x,y 为线条结束点的坐标
            CGContextAddLineToPoint(context, (CGFloat)(i) * (lineW/5), lineH - 10)
            
        }
        
       //第一条横线
       CGContextMoveToPoint(context, 0, 5)
       CGContextAddLineToPoint(context, rect.width, 5)
        
       //第二条横线
       CGContextMoveToPoint(context, 0, lineH - 5)
       CGContextAddLineToPoint(context, lineW, lineH - 5)
        
       //开始画
       CGContextStrokePath(context)
        
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //默认是黑色的,设置下背景颜色
        self.backgroundColor = UIColor.whiteColor()
        
        let frameW = frame.width
        let frameH = frame.height
        
        for var i = 0;i<5;i++ {
            //初始化内部的label
            let label = UILabel(frame: CGRectMake((CGFloat)(i) * (frameW/5) , frameH / 5, frameW / 5, frameH / 3 * 2))
            
            label.text = labelTile[i]
            label.textColor = UIColor.grayColor()
            label.font = UIFont.systemFontOfSize(13)
            label.textAlignment = NSTextAlignment.Center
            self.addSubview(label)
        }
    
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
   
}

 

 最后我们来看一下效果图~

 

posted @ 2015-10-28 21:09  Aeronfay  阅读(208)  评论(1编辑  收藏  举报