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)
    }

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM