swift下的简单的绘图实现

import UIKit

class MyView: UIView {

    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code
    }
    */
    
    var uiImage:CGImageRef? = UIImage(named: "004.jpg")?.CGImage

    //----简易画板-----
    var path = CGPathCreateMutable()
    
    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        var p = touches.anyObject()?.locationInView(self)
        CGPathMoveToPoint(path, nil, p!.x, p!.y)
        
    }
    
    override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
        var p = touches.anyObject()?.locationInView(self)
        CGPathAddLineToPoint(path, nil, p!.x, p!.y)
        
        //执行重绘的操作
        setNeedsDisplay()
    }
    

    override func drawRect(rect: CGRect) {
        var context = UIGraphicsGetCurrentContext()
        
//        CGContextSetRGBStrokeColor(context, 1, 0, 1, 1)//设置线的颜色
//        CGContextSetLineWidth(context, 5)//设置线的宽度
//        CGContextStrokePath(context)
//        
//        /*----fillpath为填充StrokePath为画线----*/
//        
//        //画线
//        CGContextMoveToPoint(context, 100, 100)
//        CGContextAddLineToPoint(context, 100, 200)
//        CGContextAddLineToPoint(context, 200, 200)
//        CGContextStrokePath(context)
//        
//        CGContextMoveToPoint(context, 100, 300)
//        CGContextAddLineToPoint(context, 100, 400)
//        CGContextAddLineToPoint(context, 200, 500)
//        CGContextStrokePath(context)
//        
//        //画方块
//        CGContextAddRect(context, CGRect(x: 200, y: 100, width: 100, height: 100))
//        CGContextSetRGBFillColor(context, 1, 0, 0, 1)//改变方块的颜色
//        /*--先把方块添加进去然后加边框,否则只显示边框--*/
//        CGContextFillPath(context)
//        CGContextStrokeRect(context, CGRect(x: 200, y: 100, width: 100, height:100))
//        
//        //画圆---弧线
//        CGContextAddArc(context, 220, 350, 100, 0, 3.14*2, 0)
//        CGContextSetRGBFillColor(context, 1, 0, 0, 1)
//        CGContextFillPath(context)
//        
//        CGContextAddArc(context, 220, 350, 100, 0, 3.14*2, 0)//最后的0为顺时针,1为逆时针
//        CGContextStrokePath(context)
//        //---椭圆---宽高相等为圆形,宽高不等为椭圆
//        CGContextAddEllipseInRect(context, CGRect(x: 50, y: 450, width: 200, height: 200))
//        CGContextStrokePath(context)
//        
//        //------画图片
//        //保存context------如果不保存与恢复图形会影响到后续的画图
//        CGContextSaveGState(context)
//        //画布的调整
//        CGContextTranslateCTM(context, 10, 400)
//        CGContextScaleCTM(context, 1, -1)
//        CGContextDrawImage(context, CGRect(x: 100, y: 100, width: 200, height: 200), uiImage)
//        //恢复context
//        CGContextRestoreGState(context)
        
        
        //------简易画板
        //画板上的简单画线
//        var path = CGPathCreateMutable()
//        CGPathMoveToPoint(path, nil, 100, 100)
//        CGPathAddLineToPoint(path, nil, 200, 200)
        CGContextAddPath(context, path)
        CGContextStrokePath(context)
    }

}

 

posted @ 2015-03-26 18:09  暗夜追星  阅读(4063)  评论(0编辑  收藏  举报