1. 程式人生 > >IOS Swift3 基本圖形繪製

IOS Swift3 基本圖形繪製



import UIKit


class ViewController: UIViewController {


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        //狀態列大小
        let statusbar_width =  UIApplication.shared.statusBarFrame.width
        let statusbar_height = UIApplication.shared.statusBarFrame.height
        
        //螢幕大小,包括狀態列
        //let screen_width = UIScreen.main.bounds.width
        let Screen_height = UIScreen.main.bounds.height
        
        let viewRect = CGRect(x: 0, y: statusbar_height, width: statusbar_width, height: Screen_height - statusbar_height )
        let view1 = MyView(frame: viewRect)
        self.view.addSubview(view1)
    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}




class MyView: UIView {
    override init(frame: CGRect) {
        super.init(frame: frame)
        //把背景色設為透明
        self.backgroundColor = UIColor.green
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func draw(_ rect: CGRect) {
        //獲取畫筆上下文
        let context:CGContext =  UIGraphicsGetCurrentContext()!
        context.setAllowsAntialiasing(true) //抗鋸齒設定
        
        //繪製點 或 實心圓
        context.fillEllipse( in: CGRect(x: 10, y: 10, width: 2, height: 2))
        context.fillEllipse( in: CGRect(x: 15, y: 10, width: 1, height: 1))
        context.fillEllipse( in: CGRect(x: 20, y: 10, width: 0.5, height: 0.5))
        
        //繪製直線
        context.setStrokeColor(red: 0.5, green: 0.5, blue: 0.5, alpha: 1 );//設定畫筆顏色方法一
        context.setLineWidth(1);//線條寬度
        context.move(to: CGPoint(x: 10, y: 40))//開始點位置
        context.addLine(to: CGPoint(x: 100, y: 40))//結束點位置
        context.strokePath();
        
        //繪製虛線
        context.setLineWidth(1);
        let dashArray: [CGFloat] = [2, 6,4,2 ]
        context.setLineDash(phase: 1, lengths: dashArray )
        context.move(to: CGPoint(x: 120, y: 40))//開始點位置
        context.addLine(to: CGPoint(x: 180, y: 40))//結束點位置
        context.strokePath();
        
        context.setLineDash(phase: 1, lengths: [1,1] )//還原點型別
        context.move(to: CGPoint(x: 200, y: 40))//開始點位置
        context.addLine(to: CGPoint(x: 260, y: 40))//結束點位置
        context.strokePath();
        
        //繪製曲線
        context.setLineDash(phase: 0, lengths: [1,0] )//還原點型別
        
        context.move(to: CGPoint(x: 10, y: 80))//開始點位置
        context.addCurve(to: CGPoint(x: 150, y: 80), control1: CGPoint(x: 50, y: 90), control2: CGPoint(x: 90, y: 70))
        context.strokePath();
        
        context.move(to: CGPoint(x: 160, y: 80))//開始點位置
        context.addQuadCurve(to: CGPoint(x: 250, y: 80), control: CGPoint(x: 200, y: 65))
        context.strokePath();
        
        //畫圓
        context.setLineWidth(0.5);//線條寬度
        context.setStrokeColor(red: 0.5, green: 0.5, blue: 0.5, alpha: 1);
        context.addEllipse(in: CGRect(x: 10, y: 120, width: 20, height: 20)); //畫圓 x,y左上角座標
        context.strokePath() //關閉路徑
        
        //畫圓弧,單位弧度,clockwise: 順時針計算,弧度 = 角度 * π / 180 x,y中心點座標
        context.setStrokeColor( UIColor.red.cgColor );//設定畫筆顏色方法二
        context.addArc(center: CGPoint(x: 50, y: 130), radius: 10, startAngle: 0, endAngle: CGFloat(90 * Double.pi/180), clockwise: true )
        context.strokePath() //關閉路徑
        context.addArc(center: CGPoint(x: 70, y: 130), radius: 10, startAngle: 0, endAngle: CGFloat(90 * Double.pi/180), clockwise: false )
        context.strokePath() //關閉路徑
        
        //繪製矩形
        context.setStrokeColor( UIColor.gray.cgColor );
        context.addRect(CGRect(x: 100, y: 120, width: 30, height: 20 ))
        context.strokePath();
        
        //字串 x,y左上角座標
        let strTxt:String = "你好!"
        strTxt.draw(at: CGPoint(x: 10, y: 160), withAttributes: nil )
        
        let textAttributes: [String: AnyObject] = [
            NSForegroundColorAttributeName : UIColor.blue.cgColor,
            NSFontAttributeName : UIFont.systemFont(ofSize: 8 )
        ]
        strTxt.draw(at: CGPoint(x: 80, y: 160), withAttributes: textAttributes )
        
        //繪製圖片
        let path = Bundle.main.path(forResource: "fj", ofType: "png")
        let srcImage = UIImage(contentsOfFile: path!)
        srcImage?.draw(at: CGPoint(x: 10, y: 200))
        srcImage?.draw(in: CGRect(x: 100, y: 200, width: 50, height: 50 ))
    }

}