UIBezierPath in Swift: Path gets closed very weirdly

若如初见. 提交于 2019-12-13 01:52:19

问题


I am using the extension from Drawing class drawing straight lines instead of curved lines to interpolate a u-shape drawing. Unfortunately the path gets closed in a weird way:

What it should look like:

What it actually looks like:

My code:

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()
    } 
}

When the array is shortened to:

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)]

It looks like this:


回答1:


Your array of points is the same 16 points repeated three times.

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)
]

You jump from the last point, CGPoint(x: 237, y: 75), back to the start point, CGPoint(x: 78, y: 99), (and you're doing that twice) which is closing the path.

The path is correct for that array which repeats the same sequence of points three times.



来源:https://stackoverflow.com/questions/34844331/uibezierpath-in-swift-path-gets-closed-very-weirdly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!