Swift 中的 UIBezierPath:路径非常奇怪地关闭
UIBezierPath in Swift: Path gets closed very weirdly
我正在使用 的扩展来插入 U 形绘图。不幸的是,路径以一种奇怪的方式关闭:
它应该是什么样子:
实际情况:
我的代码:
override func drawRect(rect: CGRect) {
let points: [CGPoint] = [CGPoint(x: 78, y: 99), CGPoint(x: 78, y: 128), CGPoint(x: 78, y: 173), CGPoint(x: 84, y: 199), CGPoint(x: 95, y: 223), CGPoint(x: 112, y: 240), CGPoint(x: 136, y: 243), CGPoint(x: 170, y: 236), CGPoint(x: 203, y: 217), CGPoint(x: 227, y: 190), CGPoint(x: 240, y: 160), CGPoint(x: 241, y: 134), CGPoint(x: 241, y: 108), CGPoint(x: 240, y: 93), CGPoint(x: 239, y: 84), CGPoint(x: 237, y: 75), CGPoint(x: 78, y: 99), CGPoint(x: 78, y: 128), CGPoint(x: 78, y: 173), CGPoint(x: 84, y: 199), CGPoint(x: 95, y: 223), CGPoint(x: 112, y: 240), CGPoint(x: 136, y: 243), CGPoint(x: 170, y: 236), CGPoint(x: 203, y: 217), CGPoint(x: 227, y: 190), CGPoint(x: 240, y: 160), CGPoint(x: 241, y: 134), CGPoint(x: 241, y: 108), CGPoint(x: 240, y: 93), CGPoint(x: 239, y: 84), CGPoint(x: 237, y: 75), CGPoint(x: 78, y: 99), CGPoint(x: 78, y: 128), CGPoint(x: 78, y: 173), CGPoint(x: 84, y: 199), CGPoint(x: 95, y: 223), CGPoint(x: 112, y: 240), CGPoint(x: 136, y: 243), CGPoint(x: 170, y: 236), CGPoint(x: 203, y: 217), CGPoint(x: 227, y: 190), CGPoint(x: 240, y: 160), CGPoint(x: 241, y: 134), CGPoint(x: 241, y: 108), CGPoint(x: 240, y: 93), CGPoint(x: 239, y: 84), CGPoint(x: 237, y: 75)]
let path = UIBezierPath(hermiteInterpolatedPoints: points, closed: false)
if let path = path {
UIColor.blueColor().set()
path.lineWidth = 5.0
path.stroke()
}
}
当数组缩短为:
let points: [CGPoint] = [CGPoint(x: 78, y: 99), CGPoint(x: 78, y: 128), CGPoint(x: 78, y: 173), CGPoint(x: 84, y: 199), CGPoint(x: 95, y: 223), CGPoint(x: 112, y: 240), CGPoint(x: 136, y: 243), CGPoint(x: 170, y: 236), CGPoint(x: 203, y: 217), CGPoint(x: 227, y: 190), CGPoint(x: 240, y: 160), CGPoint(x: 241, y: 134), CGPoint(x: 241, y: 108)]
看起来像这样:
你的点数组是相同的 16 个点重复三次。
let points: [CGPoint] = [
CGPoint(x: 78, y: 99), CGPoint(x: 78, y: 128), CGPoint(x: 78, y: 173), CGPoint(x: 84, y: 199), CGPoint(x: 95, y: 223), CGPoint(x: 112, y: 240), CGPoint(x: 136, y: 243), CGPoint(x: 170, y: 236), CGPoint(x: 203, y: 217), CGPoint(x: 227, y: 190), CGPoint(x: 240, y: 160), CGPoint(x: 241, y: 134), CGPoint(x: 241, y: 108), CGPoint(x: 240, y: 93), CGPoint(x: 239, y: 84), CGPoint(x: 237, y: 75),
CGPoint(x: 78, y: 99), CGPoint(x: 78, y: 128), CGPoint(x: 78, y: 173), CGPoint(x: 84, y: 199), CGPoint(x: 95, y: 223), CGPoint(x: 112, y: 240), CGPoint(x: 136, y: 243), CGPoint(x: 170, y: 236), CGPoint(x: 203, y: 217), CGPoint(x: 227, y: 190), CGPoint(x: 240, y: 160), CGPoint(x: 241, y: 134), CGPoint(x: 241, y: 108), CGPoint(x: 240, y: 93), CGPoint(x: 239, y: 84), CGPoint(x: 237, y: 75),
CGPoint(x: 78, y: 99), CGPoint(x: 78, y: 128), CGPoint(x: 78, y: 173), CGPoint(x: 84, y: 199), CGPoint(x: 95, y: 223), CGPoint(x: 112, y: 240), CGPoint(x: 136, y: 243), CGPoint(x: 170, y: 236), CGPoint(x: 203, y: 217), CGPoint(x: 227, y: 190), CGPoint(x: 240, y: 160), CGPoint(x: 241, y: 134), CGPoint(x: 241, y: 108), CGPoint(x: 240, y: 93), CGPoint(x: 239, y: 84), CGPoint(x: 237, y: 75)
]
您从最后一点 CGPoint(x: 237, y: 75)
跳回起点 CGPoint(x: 78, y: 99)
(并且您这样做了两次),这将关闭路径。
对于将相同的点序列重复三次的数组,路径是正确的。
我正在使用
它应该是什么样子:
实际情况:
我的代码:
override func drawRect(rect: CGRect) {
let points: [CGPoint] = [CGPoint(x: 78, y: 99), CGPoint(x: 78, y: 128), CGPoint(x: 78, y: 173), CGPoint(x: 84, y: 199), CGPoint(x: 95, y: 223), CGPoint(x: 112, y: 240), CGPoint(x: 136, y: 243), CGPoint(x: 170, y: 236), CGPoint(x: 203, y: 217), CGPoint(x: 227, y: 190), CGPoint(x: 240, y: 160), CGPoint(x: 241, y: 134), CGPoint(x: 241, y: 108), CGPoint(x: 240, y: 93), CGPoint(x: 239, y: 84), CGPoint(x: 237, y: 75), CGPoint(x: 78, y: 99), CGPoint(x: 78, y: 128), CGPoint(x: 78, y: 173), CGPoint(x: 84, y: 199), CGPoint(x: 95, y: 223), CGPoint(x: 112, y: 240), CGPoint(x: 136, y: 243), CGPoint(x: 170, y: 236), CGPoint(x: 203, y: 217), CGPoint(x: 227, y: 190), CGPoint(x: 240, y: 160), CGPoint(x: 241, y: 134), CGPoint(x: 241, y: 108), CGPoint(x: 240, y: 93), CGPoint(x: 239, y: 84), CGPoint(x: 237, y: 75), CGPoint(x: 78, y: 99), CGPoint(x: 78, y: 128), CGPoint(x: 78, y: 173), CGPoint(x: 84, y: 199), CGPoint(x: 95, y: 223), CGPoint(x: 112, y: 240), CGPoint(x: 136, y: 243), CGPoint(x: 170, y: 236), CGPoint(x: 203, y: 217), CGPoint(x: 227, y: 190), CGPoint(x: 240, y: 160), CGPoint(x: 241, y: 134), CGPoint(x: 241, y: 108), CGPoint(x: 240, y: 93), CGPoint(x: 239, y: 84), CGPoint(x: 237, y: 75)]
let path = UIBezierPath(hermiteInterpolatedPoints: points, closed: false)
if let path = path {
UIColor.blueColor().set()
path.lineWidth = 5.0
path.stroke()
}
}
当数组缩短为:
let points: [CGPoint] = [CGPoint(x: 78, y: 99), CGPoint(x: 78, y: 128), CGPoint(x: 78, y: 173), CGPoint(x: 84, y: 199), CGPoint(x: 95, y: 223), CGPoint(x: 112, y: 240), CGPoint(x: 136, y: 243), CGPoint(x: 170, y: 236), CGPoint(x: 203, y: 217), CGPoint(x: 227, y: 190), CGPoint(x: 240, y: 160), CGPoint(x: 241, y: 134), CGPoint(x: 241, y: 108)]
看起来像这样:
你的点数组是相同的 16 个点重复三次。
let points: [CGPoint] = [
CGPoint(x: 78, y: 99), CGPoint(x: 78, y: 128), CGPoint(x: 78, y: 173), CGPoint(x: 84, y: 199), CGPoint(x: 95, y: 223), CGPoint(x: 112, y: 240), CGPoint(x: 136, y: 243), CGPoint(x: 170, y: 236), CGPoint(x: 203, y: 217), CGPoint(x: 227, y: 190), CGPoint(x: 240, y: 160), CGPoint(x: 241, y: 134), CGPoint(x: 241, y: 108), CGPoint(x: 240, y: 93), CGPoint(x: 239, y: 84), CGPoint(x: 237, y: 75),
CGPoint(x: 78, y: 99), CGPoint(x: 78, y: 128), CGPoint(x: 78, y: 173), CGPoint(x: 84, y: 199), CGPoint(x: 95, y: 223), CGPoint(x: 112, y: 240), CGPoint(x: 136, y: 243), CGPoint(x: 170, y: 236), CGPoint(x: 203, y: 217), CGPoint(x: 227, y: 190), CGPoint(x: 240, y: 160), CGPoint(x: 241, y: 134), CGPoint(x: 241, y: 108), CGPoint(x: 240, y: 93), CGPoint(x: 239, y: 84), CGPoint(x: 237, y: 75),
CGPoint(x: 78, y: 99), CGPoint(x: 78, y: 128), CGPoint(x: 78, y: 173), CGPoint(x: 84, y: 199), CGPoint(x: 95, y: 223), CGPoint(x: 112, y: 240), CGPoint(x: 136, y: 243), CGPoint(x: 170, y: 236), CGPoint(x: 203, y: 217), CGPoint(x: 227, y: 190), CGPoint(x: 240, y: 160), CGPoint(x: 241, y: 134), CGPoint(x: 241, y: 108), CGPoint(x: 240, y: 93), CGPoint(x: 239, y: 84), CGPoint(x: 237, y: 75)
]
您从最后一点 CGPoint(x: 237, y: 75)
跳回起点 CGPoint(x: 78, y: 99)
(并且您这样做了两次),这将关闭路径。
对于将相同的点序列重复三次的数组,路径是正确的。