废弃的方法.png先列举一些已经不能使用的方法
列举一下常用的方法
//获取上下文对象
let context = UIGraphicsGetCurrentContext()
//线条颜色
context?.setStrokeColor(UIColor.red.cgColor)
//线条宽度
context?.setLineWidth(1.0)
//移动画笔到某一点
context?.move(to: CGPoint(x: 10, y: 10))
//画线
context?.addLine(to: CGPoint(x: gameSize , y: 10))
//画弧线
context?.addArc(center: CGPoint.init(x: 150, y: 100), radius: 30, startAngle: 0, endAngle: CGFloat(M_PI*2), clockwise: true);
//二次贝塞尔曲线
context?.addQuadCurve(to: CGPoint.init(x: 150, y: 150), control: CGPoint.init(x: 50, y: 100))
//三次贝塞尔曲线
context?.addCurve(to: CGPoint.init(x: 250, y: 150), control1: CGPoint.init(x: 50, y: 100), control2: CGPoint.init(x: 100, y: 150))
//绘制矩形
context?.setFillColor(UIColor.brown.cgColor);
//设置填充颜色
context?.fill(CGRect.init(x: 50, y: 50, width: 100, height: 50));
//绘制椭圆
context?.strokeEllipse(in: CGRect.init(x: 50, y: 50, width: 100, height: 50));
//绘制
context?.strokePath()
再演示一个棋盘的DEMO
//棋盘格数,可修改此参数定制棋盘大小
let chessPiecesNumber = 10
let gameViewSize = Double(frame.size.width - 20)
let context = UIGraphicsGetCurrentContext()
context?.setLineWidth(1.0)
for i in 0..<chessPiecesNumber {
context?.move(to: CGPoint(x: 10, y: 10 + gameViewSize / Double(chessPiecesNumber - 1) * Double(i)))
context?.addLine(to: CGPoint(x: gameViewSize + 10, y: 10 + gameViewSize / Double(chessPiecesNumber - 1) * Double(i)))
context?.strokePath()
}
for i in 0..<chessPiecesNumber {
context?.move(to: CGPoint(x: 10 + gameViewSize / Double(chessPiecesNumber - 1) * Double(i), y: 10))
context?.addLine(to: CGPoint(x: 10 + gameViewSize / Double(chessPiecesNumber - 1) * Double(i), y: gameViewSize + 10))
context?.strokePath()
}
棋盘.png注意:
1.for循环的 ...10 已经不能用了,必须用 ..<10 了
2.CGRect(x: 0, y: 0, width: 100, height: 100)已经不能改用了,必须用
CGRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: 100, height: 100))
网友评论