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()
}
}
画圆
/*
参数解释:
1.center: CGPoint 中心点坐标
2.radius: CGFloat 半径
3.startAngle: CGFloat 起始点所在弧度
4.endAngle: CGFloat 结束点所在弧度
5.clockwise: Bool 是否顺时针绘制
7.画圆时,没有坐标这个概念,根据弧度来定位起始点和结束点位置。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)