gpt4 book ai didi

ios - 贝塞尔曲线闭合路径未填充

转载 作者:行者123 更新时间:2023-11-30 13:12:54 26 4
gpt4 key购买 nike

我使用的是 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()
}
}

布局

  • 单元格

    • View (绘制贝塞尔曲线的路径)

      • 标签(带有白色文本)

结果 enter image description here

最佳答案

您正在为您的道路引入不连续性。当您说 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()

关于ios - 贝塞尔曲线闭合路径未填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38541919/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com