酱油05-绘图
2016-08-21 本文已影响8人
没有北方的南方
使用UIGraphicsGetCurrentContext绘图
import UIKit
class Draw: UIView {
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func drawRect(rect: CGRect) {
// Drawing code
drawline()
//设置关联上下文
let begin = UIGraphicsGetCurrentContext()
//绘制矩形
CGContextAddRect(begin, CGRect(x: 100, y: 100, width: 200, height: 100))
CGContextAddRect(begin, CGRect(x: 100, y: 100, width: 100, height: 200))
//绘制椭圆
CGContextAddEllipseInRect(begin, CGRect(x: 0, y: 0, width: 100, height: 300))
//绘制圆弧(中心点,半径,起始角度,终点角度,方向)
CGContextAddArc(begin, 200, 300, 150, 1, CGFloat(M_PI_4), 0)
//贝兹曲线起始位置
CGContextMoveToPoint(begin, 0, 300)
//曲线三点绘制(一个控制点,一个终点)
CGContextAddQuadCurveToPoint(begin, 50, 0, 400, 300)
//曲线四点绘制(两个控制点,最后一个为终点)
CGContextAddCurveToPoint(begin, 50, 100, 250, 200, 300, 300)
CGContextSetStrokeColorWithColor(begin, UIColor.redColor().CGColor)
//设置填充色
CGContextSetFillColor(begin, [1,0,0,1])
//设置只填充
CGContextFillPath(begin)
//设置绘制开始点
CGContextMoveToPoint(begin, 100, 0)
//尝试绘制一个五角星
CGContextAddLineToPoint(begin, 0, 200)
CGContextAddLineToPoint(begin, 200, 100)
CGContextAddLineToPoint(begin, 0, 100)
CGContextAddLineToPoint(begin, 200, 200)
CGContextAddLineToPoint(begin, 100, 0)
CGContextSetLineWidth(begin, 5)
//设置绘制转折点为圆角
CGContextSetLineCap(begin, .Round)
CGContextSetStrokeColorWithColor(begin, UIColor.cyanColor().CGColor)
CGContextSetLineDash(begin, 2, [2,5], 2)
//EO为奇偶校验,(单为里,双为外,只填充里),Fill为整个填充,Stroke为填充边框
CGContextDrawPath(begin, .EOFillStroke)
CGContextStrokePath(begin)
}
func drawline(){
let begin = UIGraphicsGetCurrentContext()
var x : CGFloat = 50
var y : CGFloat = 50
//循环绘制表格
while x < self.frame.size.width{
CGContextMoveToPoint(begin, x, 0)
CGContextAddLineToPoint(begin, x, self.frame.size.height)
x += 50
}
while y < self.frame.size.height{
CGContextMoveToPoint(begin, 0, y)
CGContextAddLineToPoint(begin, self.frame.size.width, y)
y += 50
}
CGContextSetStrokeColorWithColor(begin, UIColor.cyanColor().CGColor)
//进行当前绘制保存
CGContextSaveGState(begin)
//设置绘制线为虚线
CGContextSetLineDash(begin, 2, [2,5], 2)
//设置绘制线的宽度
CGContextSetLineWidth(begin, 3)
//执行绘制
CGContextStrokePath(begin)
//重新回到上次的保存点
CGContextRestoreGState(begin)
}
}