2015-09-13 57 views
1

我无法使用swift填充路径三角形。 我用下面的代码来做一个笔划。如何使用Core图形快速填充三角形

 let view = UIView(frame: CGRectMake(0, 0, 300, 300)) 
     let shape = CAShapeLayer() 
     view.layer.addSublayer(shape) 
     shape.opacity = 0.5 
     shape.lineWidth = 2 
     shape.lineJoin = kCALineJoinMiter 
     shape.strokeColor = UIColor(hue: 0.786, saturation: 0.79, brightness: 0.53, alpha: 1.0).CGColor 
     shape.fillColor = UIColor(hue: 0.786, saturation: 0.15, brightness: 0.89, alpha: 1.0).CGColor 


     let context = UIGraphicsGetCurrentContext() 
     CGContextSetStrokeColorWithColor(context, shape.strokeColor); 
     CGContextSetLineWidth(context, shape.lineWidth); 
     CGContextMoveToPoint(context, 100, 100) 
     CGContextAddLineToPoint(context, 150, 150) 
     CGContextAddLineToPoint(context, 100, 200) 
     CGContextAddLineToPoint(context, 100, 100) 
     CGContextDrawPath(context, kCGPathStroke); 

任何想法,用什么来填补shape.fillColor这个三角形?

回答

1

要填充路径,请设置填充颜色并使用kCGPathContextFill。要填充并冲程 路径,请使用kCGPathFillStroke。此外,路径应该是关闭 填充它:

CGContextSetFillColorWithColor(context, ...) 
CGContextMoveToPoint(context, 100, 100) // move to first point 
CGContextAddLineToPoint(context, 150, 150) // line to second point 
CGContextAddLineToPoint(context, 100, 200) // line to third point 
CGContextClosePath(context) // line to first point and close path 
CGContextDrawPath(context, kCGPathFill); 
+0

很多很多的感谢... –