我无法使用 swift 填充路径三角形。 我使用了下面的代码,它产生了一个笔划。
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 填充这个三角形吗?
最佳答案
要填充路径,请设置填充颜色并使用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);
关于swift - 如何使用Core Graphics快速填充三角形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32547783/