import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let viewRect = CGRect(x: 0, y: 0, width: 400, height: 400)
let view1 = MyView(frame: viewRect)
self.view.addSubview(view1)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
class MyView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = UIColor.clearColor()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func drawRect(rect: CGRect) {
super.drawRect(rect)
let path = UIBezierPath()
path.lineWidth = 1//路徑寬度
path.lineCapStyle = .Round//端點樣式(枚舉)
path.lineJoinStyle = .Round//連接點樣式(枚舉)
path.moveToPoint(CGPoint(x:200,y:50))//設置起點
path.addLineToPoint(CGPoint(x:300,y:100))//從上一點連接一條線到本次指定的點
path.addLineToPoint(CGPoint(x:260,y:200))
path.addLineToPoint(CGPoint(x:100,y:200))
path.addLineToPoint(CGPoint(x:100,y:70))
path.closePath()//閉合路徑官卡,把起始點和終點連接起來
UIColor.blueColor().setFill()//設置填充顏色(不常用
UIColor.redColor().setStroke()//設置路徑顏色(不常用)
path.fill()
path.stroke()
}
}
畫圓
/*
參數(shù)解釋:
1.center: CGPoint 中心點坐標
2.radius: CGFloat 半徑
3.startAngle: CGFloat 起始點所在弧度
4.endAngle: CGFloat 結束點所在弧度
5.clockwise: Bool 是否順時針繪制
7.畫圓時腿准,沒有坐標這個概念片吊,根據(jù)弧度來定位起始點和結束點位置。M_PI即是圓周率双炕。畫半圓即(0,M_PI),代表0到180度皮假。全圓則是(0,M_PI*2)怔接,代表0到360度
*/
let mainPath1 = UIBezierPath(arcCenter: CGPoint(x: 50, y: 50), radius: 50, startAngle: CGFloat(M_PI) * 0, endAngle: CGFloat(M_PI) * 2, clockwise: true)