常用的绘图API
- UIGraphicsGetCurrentContext() 初始化
-
- CGContextMoveToPoint(c: CGContext, x: CGFloat, y: CGFloat) 移动到点
- CGContextAddLineToPoint(c: CGContext!, x: CGFloat, y: CGFloat)添加一条线
- CGContextAddRect(c: CGContext, rect: CGRect)添加一个矩形
- CGContextAddArc(c: CGContext!, x: CGFloat, y: CGFloat, radius: CGFloat, startAngle: CGFloat, endAngle: CGFloat, clockwise: Int32) 添加弧形
- CGContextAddEllipseInRect(context: CGContext!, rect: CGRect) 添加椭圆
-
CGContextAddPath(context:CGContext!, path: CGPath!) 添加一个Path
- CGContextSetRGBStrokeColor(context:CGContext!, red:CGFloat, green:CGFloat, blue:CGFloat, alpha:CGFloat) 设置描边颜色
- CGContextSetRGBFillColor(context:CGContext!, red:CGFloat, green:CGFloat, blue:CGFloat, alpha:CGFloat) 设置填充颜色
- CGContextSetLineWidth(c:CGContext!, width:CGFloat) 设置线宽
- CGContextStrokePath(c:CGContext!) 对线条描边
- CGContextFillPath(c:CGContext!) 填充
- CGContextSaveGState(c:CGContext!) 保存改变前的状态
- CGContextRestoreGState(c:CGContext!)恢复改变前的状态
- CGContextTranslateCTM(c:CGContext!, tx:CGFloat, ty:CGFloat) 更改坐标系的原点
- CGContextScaleCTM(c:CGContext!, sx:CGFloat, sy:CGFloat) 反转Y轴
- CGContextDrawImage(c:CGContext!, rect:CGRect, image:CGImage!) 绘制图片
- CGRect(x: Int, y: Int, width: Int, height: Int)
绘制线条
import UIKit
class MyView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func drawRect(rect: CGRect) {
var context = UIGraphicsGetCurrentContext()
// 移动到点
CGContextMoveToPoint(context, 100, 200)
// 添加线条
CGContextAddLineToPoint(context, 100, 200)
CGContextAddLineToPoint(context, 200, 200)
// 移动
CGContextMoveToPoint(context, 100, 300)
CGContextAddLineToPoint(context, 100, 400)
CGContextAddLineToPoint(context, 200, 500)
// 设置颜色
CGContextSetRGBStrokeColor(context, 1, 0, 0, 1)
// 设置线宽
CGContextSetLineWidth(context, 5)
// 画线条
CGContextStrokePath(context)
}
}
绘制矩形
CGContextSetLineWidth(context, 5) // 设置线条宽度
CGContextSetRGBStrokeColor(context, 0, 1, 0, 1) //设置描边颜色
// 给矩形描边
CGContextStrokeRect(context, CGRect(x: 100, y: 100, width: 100, height: 100))
override func drawRect(rect: CGRect) {
var context = UIGraphicsGetCurrentContext()
// 添加矩形
CGContextAddRect(context, CGRect(x: 100, y: 100, width: 100, height: 100))
// 设置RGB填充颜色
CGContextSetRGBFillColor(context, 1, 0, 0, 1)
CGContextFillPath(context) // 填充Path
CGContextSetLineWidth(context, 5) // 设置线条宽度
CGContextSetRGBStrokeColor(context, 0, 1, 0, 1) //设置描边颜色
// 给矩形描边
CGContextStrokeRect(context, CGRect(x: 100, y: 100, width: 100, height: 100))
绘制圆形
override func drawRect(rect: CGRect) {
var context = UIGraphicsGetCurrentContext()
// 添加一个弧形
CGContextAddArc(context, 150, 200, 100, 0, 3.141592653*2, 0)
CGContextSetLineWidth(context, 10)
CGContextStrokePath(context)
// 添加一个椭圆
CGContextAddEllipseInRect(context, CGRect(x: 50, y: 400, width: 200, height: 100))
CGContextStrokePath(context)
}
绘制图片
override func drawRect(rect: CGRect) {
var context = UIGraphicsGetCurrentContext()
CGContextSaveGState(context) // 保存改变前的状态
CGContextTranslateCTM(context, 10, 400) // 更改坐标系的原点
CGContextScaleCTM(context, 1, -1) // 反转Y轴
CGContextDrawImage(context, CGRect(x: 0, y: 0, width: 200, height: 200), uiImage)
CGContextRestoreGState(context) // 恢复改变前的状态
CGContextStrokeRect(context, CGRect(x: 50, y: 100, width: 100, height: 100))
}
简易画板
// 手指按下
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()
}
var path = CGPathCreateMutable()
override func drawRect(rect: CGRect) {
var context = UIGraphicsGetCurrentContext()
// 将先前创建的Path添加到context中
CGContextAddPath(context, path)
// 将Path描画出来
CGContextStrokePath(context)
}