标签:
1.绘制线条
override func drawRect(rect: CGRect) {
// Drawing code
var context = UIGraphicsGetCurrentContext()
CGContextMoveToPoint(context, 100.0, 100.0)
CGContextAddLineToPoint(context, 100, 200)
CGContextSetLineWidth(context, 5.0) //set line width
CGContextSetRGBStrokeColor(context, 1, 0, 0, 1.0) //set line color
CGContextStrokePath(context)
}
override func drawRect(rect: CGRect) {
// Drawing code
var context = UIGraphicsGetCurrentContext()
var path = CGPathCreateMutable()
CGPathMoveToPoint(path, nil, 100, 100)
CGPathAddLineToPoint(path, nil, 100, 200)
CGContextAddPath(context, path)
CGContextStrokePath(context)
}
2.绘制矩形
override func drawRect(rect: CGRect) {
// Drawing code
var context = UIGraphicsGetCurrentContext()
CGContextAddRect(context, CGRect(x: 100, y: 100, width: 200, height: 200))
CGContextSetRGBFillColor(context, 1, 0, 0, 1.0) //set fill color
CGContextFillPath(context)
//添加边框
CGContextSetRGBStrokeColor(context, 0, 0, 1, 1)
CGContextSetLineWidth(context, 5)
CGContextStrokeRect(context, CGRect(x: 100, y: 100, width: 200, height: 200))
}
3.绘制圆弧
override func drawRect(rect: CGRect) {
// Drawing code
var context = UIGraphicsGetCurrentContext()
//顺时针 180度圆弧
CGContextAddArc(context, 150, 300, 100, 0, 3.14, 0);
CGContextSetRGBStrokeColor(context, 0, 0, 1, 1)
CGContextSetLineWidth(context, 5)
CGContextStrokePath(context)
}
4.填充圆
override func drawRect(rect: CGRect) {
// Drawing code
var context = UIGraphicsGetCurrentContext()
//填充圆
CGContextAddArc(context, 150, 300, 100, 0, 3.14*2, 0)
CGContextFillPath(context)
}
override func drawRect(rect: CGRect) {
// Drawing code
var context = UIGraphicsGetCurrentContext()
//填充圆
CGContextAddEllipseInRect(context, CGRect(x: 200, y: 200, width: 200, height: 200))
// CGContextFillPath(context)
CGContextSetRGBStrokeColor(context, 0, 0, 1, 1)
CGContextSetLineWidth(context, 5)
CGContextStrokePath(context)
}
5.绘制图片
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
uiImage = UIImage(named: "walle.png")!.CGImage
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func drawRect(rect: CGRect) {
// Drawing code
var context = UIGraphicsGetCurrentContext()
CGContextSaveGState(context)
CGContextTranslateCTM(context,0, 220)
CGContextScaleCTM(context, 1, -1)
CGContextDrawImage(context, CGRect(x: 0, y: 0, width: 300, height: 200), uiImage)
CGContextRestoreGState(context)
}
var uiImage:CGImageRef?
标签:
原文地址:http://www.cnblogs.com/PJXWang/p/4915478.html