这段代码在视图中填充了一个红色矩形:
let context:CGContextRef = NSGraphicsContext.currentContext()!.CGContext
var pathPoint:CGPoint = CGPoint()
pathPoint.x = CGFloat(225.0)
pathPoint.y = CGFloat(100.0)
var myMutablePath:CGMutablePath = CGPathCreateMutable()
CGPathMoveToPoint(myMutablePath, nil, pathPoint.x, pathPoint.y)
CGPathAddLineToPoint(myMutablePath, nil, pathPoint.x + CGFloat(50.0), pathPoint.y)
CGPathAddLineToPoint(myMutablePath, nil, pathPoint.x + CGFloat(50.0), pathPoint.y + CGFloat(50.0))
CGPathAddLineToPoint(myMutablePath, nil, pathPoint.x, pathPoint.y + CGFloat(50.0))
CGPathAddLineToPoint(myMutablePath, nil, pathPoint.x, pathPoint.y)
CGContextAddPath(context, myMutablePath);
CGContextSetRGBFillColor(context, 1.0, 0.0, 0.0, 1.0)
CGContextFillPath(context)