[英]UIBezierPath not equally fat everywhere and cut off
I am trying to draw a line with a smooth quadratic curve attached to its end.我正在尝试绘制一条线,其末端附有平滑的二次曲线。 Therefore, I am using this code:因此,我正在使用此代码:
import Foundation
import UIKit
class IndicatingView: UIView {
var path: UIBezierPath!
override init(frame: CGRect) {
super.init(frame: frame)
}
func createLineWithCurve() {
path = UIBezierPath()
path.move(to: CGPoint(x: 0, y: 0))
path.addLine(to: CGPoint(x: 0, y: self.frame.height - self.frame.width))
path.addQuadCurve(to: CGPoint(x: self.frame.width, y: self.frame.height), controlPoint: CGPoint(x: 0, y: self.frame.height))
}
override func draw(_ rect: CGRect) {
self.createLineWithCurve()
path.lineWidth = 3
// Specify a border (stroke) color.
UIColor.purple.setStroke()
path.stroke()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
And the output I get looks quite good already:我得到的输出看起来已经很好了:
However, as you can see, the quad-curve is fatter than the normal line.但是,正如您所看到的,四边形曲线比正常线更粗。 Also, the quad-curve seems to be cut off at the bottom.此外,四边形曲线似乎在底部被切断。 How can I fix that?我该如何解决? I want the path to be completely smooth without looking fatter at some points or being cut-off somehow.我希望路径完全平滑,而不会在某些点看起来更胖或以某种方式被切断。
Thanks in advance for your help :)在此先感谢您的帮助 :)
The problem is that your path is getting clipped by the bounds of the view.问题是你的路径被视图的边界剪掉了。 For example, in your vertical stroke starting at 0, 0
, half of the path's vertical stroke will fall outside of the left edge of the view.例如,在从0, 0
开始的垂直笔划中,路径垂直笔划的一半将落在视图的左边缘之外。
You should use insetBy(dx:dy:)
to inset the stroked path by half the line width, which will ensure the whole stroke will fall within the bounds
of the view.您应该使用insetBy(dx:dy:)
将描边路径插入线宽的一半,这将确保整个描边落在视图的bounds
内。 And then use minX
, maxX
, etc. of that resulting inset CGRect
to figure out the various CGPoint
for your path.然后使用minX
、 maxX
等生成的插入CGRect
来找出路径的各种CGPoint
。
For example:例如:
class IndicatingView: UIView {
func lineWithCurve() -> UIBezierPath {
let lineWidth: CGFloat = 3
let rect = bounds.insetBy(dx: lineWidth / 2, dy: lineWidth / 2)
let path = UIBezierPath()
path.lineWidth = lineWidth
path.lineCapStyle = .square
path.move(to: CGPoint(x: rect.minX, y: rect.minY))
path.addLine(to: CGPoint(x: rect.minX, y: rect.maxY - rect.width))
path.addQuadCurve(to: CGPoint(x: rect.maxX, y: rect.maxY), controlPoint: CGPoint(x: rect.minX, y: rect.maxY))
return path
}
override func draw(_ rect: CGRect) {
UIColor.purple.setStroke()
lineWithCurve().stroke()
}
}
That yields:这产生:
here you see my "result" -> i made the linewidth a bit bigger that you can see, it paints perfect ;)在这里,您可以看到我的“结果”-> 我将线宽调大了一点,您可以看到,它画得很完美;)
Apple draws always exactly on point, so if you paint a line of width 3 on point 0,0, one point is -1,0, one on 0,0 and one on 1,0 Apple 总是精确地在点上绘制,因此如果您在点 0,0 上绘制一条宽度为 3 的线,则一个点是 -1,0,一个点在 0,0 上,一个点在 1,0 上
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.