我正在使用Swift v2.2并且我已经绘制了一个如下所示的矩形,我打算填充它然后在其中显示一些白色文本。但是,我看到路径已经关闭,但矩形没有被填满。如有任何意见,请提供帮助和谢谢。
代码
class InstructionArrowBorderView: UIView {
var lineWidth: CGFloat = 1 {didSet { setNeedsDisplay() } }
var instructionRectPathColor: UIColor = UIColor(red:13/255.0, green:118/255.0, blue:255/255.0, alpha: 1.0) { didSet { setNeedsDisplay() } }
override func drawRect(rect: CGRect) {
let instructionRectPath = UIBezierPath()
instructionRectPath.moveToPoint(CGPoint(x: bounds.minX, y: bounds.maxY - 50))
instructionRectPath.addLineToPoint(CGPoint(x: bounds.minX, y: bounds.minY))
instructionRectPath.moveToPoint(CGPoint(x: bounds.minX, y: bounds.minY))
instructionRectPath.addLineToPoint(CGPoint(x: bounds.maxX, y: bounds.minY))
instructionRectPath.moveToPoint(CGPoint(x: bounds.maxX, y: bounds.minY))
instructionRectPath.addLineToPoint(CGPoint(x: bounds.maxX, y: bounds.maxY - 50))
instructionRectPath.moveToPoint(CGPoint(x: bounds.maxX, y: bounds.maxY - 50))
instructionRectPath.addLineToPoint(CGPoint(x: bounds.minX, y: bounds.maxY - 50))
instructionRectPath.lineWidth = lineWidth
instructionRectPath.closePath()
instructionRectPath.fill()
instructionRectPathColor.set()
instructionRectPath.stroke()
}
}
布局
细胞
查看(绘制Bezier的路径)
答案 0 :(得分:0)
您在路径中引入了不连续性。当您说addLineToPoint
时,您也不需要说moveToPoint
。这会阻止您的路径正确填充。您还需要设置颜色填充/描边颜色,然后填充/描边路径。
您的drawRect
代码应为:
let instructionRectPath = UIBezierPath()
instructionRectPath.moveToPoint(CGPoint(x: bounds.minX, y: bounds.maxY - 50))
instructionRectPath.addLineToPoint(CGPoint(x: bounds.minX, y: bounds.minY))
instructionRectPath.addLineToPoint(CGPoint(x: bounds.maxX, y: bounds.minY))
instructionRectPath.addLineToPoint(CGPoint(x: bounds.maxX, y: bounds.maxY - 50))
instructionRectPath.addLineToPoint(CGPoint(x: bounds.minX, y: bounds.maxY - 50))
instructionRectPath.closePath()
instructionRectPath.lineWidth = lineWidth
instructionRectPathColor.set()
instructionRectPath.fill()
instructionRectPath.stroke()
可以更简洁地写成:
let instructionRectPath = UIBezierPath(rect: CGRect(x: bounds.minX, y: bounds.minY, width: bounds.maxX, height: bounds.maxY - 50))
instructionRectPath.lineWidth = lineWidth
instructionRectPathColor.set()
instructionRectPath.fill()
instructionRectPath.stroke()