码迷,mamicode.com
首页 > 移动开发 > 详细

IOS 绘图

时间:2015-10-27 21:40:52      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:

1.绘制线条

override func drawRect(rect: CGRect) {

        // Drawing code

        var context = UIGraphicsGetCurrentContext()

        CGContextMoveToPoint(context, 100.0, 100.0)

        CGContextAddLineToPoint(context, 100, 200)

        CGContextSetLineWidth(context, 5.0)     //set line width

        CGContextSetRGBStrokeColor(context, 1, 0, 0, 1.0) //set line color

        CGContextStrokePath(context)

    }

 

 override func drawRect(rect: CGRect) {

        // Drawing code

        var context = UIGraphicsGetCurrentContext()

        var path = CGPathCreateMutable()

        CGPathMoveToPoint(path, nil, 100, 100)

        CGPathAddLineToPoint(path, nil, 100, 200)

        CGContextAddPath(context, path)

        CGContextStrokePath(context)

       

    }

 

2.绘制矩形

override func drawRect(rect: CGRect) {

        // Drawing code

        var context = UIGraphicsGetCurrentContext()

        CGContextAddRect(context, CGRect(x: 100, y: 100, width: 200, height: 200))

        CGContextSetRGBFillColor(context, 1, 0, 0, 1.0) //set fill color

        CGContextFillPath(context)

        

        //添加边框

        CGContextSetRGBStrokeColor(context, 0, 0, 1, 1)

        CGContextSetLineWidth(context, 5)

        CGContextStrokeRect(context, CGRect(x: 100, y: 100, width: 200, height: 200))

    }

 

3.绘制圆弧

  override func drawRect(rect: CGRect) {

        // Drawing code

        var context = UIGraphicsGetCurrentContext()

        //顺时针 180度圆弧

        CGContextAddArc(context, 150, 300, 100, 0, 3.14, 0);

    

        CGContextSetRGBStrokeColor(context, 0, 0, 1, 1)

        CGContextSetLineWidth(context, 5)

        CGContextStrokePath(context)

    }

 4.填充圆

override func drawRect(rect: CGRect) {

        // Drawing code

        var context = UIGraphicsGetCurrentContext()

        //填充圆

        CGContextAddArc(context, 150, 300, 100, 0, 3.14*2, 0)

        CGContextFillPath(context)

    }

override func drawRect(rect: CGRect) {

        // Drawing code

        var context = UIGraphicsGetCurrentContext()

        //填充圆

        CGContextAddEllipseInRect(context, CGRect(x: 200, y: 200, width: 200, height: 200))

//        CGContextFillPath(context)

        

        CGContextSetRGBStrokeColor(context, 0, 0, 1, 1)

        CGContextSetLineWidth(context, 5)

        CGContextStrokePath(context)

        

    }

5.绘制图片

required init?(coder aDecoder: NSCoder) {

        super.init(coder: aDecoder)

        uiImage =  UIImage(named: "walle.png")!.CGImage

        

    }

 

 

    // Only override drawRect: if you perform custom drawing.

    // An empty implementation adversely affects performance during animation.

    override func drawRect(rect: CGRect) {

        // Drawing code

        var context = UIGraphicsGetCurrentContext()

        CGContextSaveGState(context)

        CGContextTranslateCTM(context,0, 220)

        CGContextScaleCTM(context, 1, -1)

        CGContextDrawImage(context, CGRect(x: 0, y: 0, width: 300, height: 200), uiImage)

        CGContextRestoreGState(context)

    }

    var uiImage:CGImageRef?

 

  

 

IOS 绘图

标签:

原文地址:http://www.cnblogs.com/PJXWang/p/4915478.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!