Get the starting point of UIBezierPath

I created UIBezierPath, but I do not know how to access the starting point. I tried to do this:

let startPoint = path.currentPoint

The property currentPointgives me the last point, not the starting point. The reason I need a starting point is because I want to place the image at the starting point of the path.

Any ideas?

+4
source share
2 answers

You need to go down CGPathand use CGPathApplyto move items. You only need the first, but you must look at them all.

, "". UIBezierPath ( , .)

rob mayoff CGPath.forEach, , :

// rob mayoff CGPath.foreach
extension CGPath {
    func forEach(@noescape body: @convention(block) (CGPathElement) -> Void) {
        typealias Body = @convention(block) (CGPathElement) -> Void
        func callback(info: UnsafeMutablePointer<Void>, element: UnsafePointer<CGPathElement>) {
            let body = unsafeBitCast(info, Body.self)
            body(element.memory)
        }
        let unsafeBody = unsafeBitCast(body, UnsafeMutablePointer<Void>.self)
        CGPathApply(self, unsafeBody, callback)
    }
}

// Finds the first point in a path
extension UIBezierPath {
    func firstPoint() -> CGPoint? {
        var firstPoint: CGPoint? = nil

        self.CGPath.forEach { element in
            // Just want the first one, but we have to look at everything
            guard firstPoint == nil else { return }
            assert(element.type == .MoveToPoint, "Expected the first point to be a move")
            firstPoint = element.points.memory
        }
        return firstPoint
    }
}

Swift 3 :

// rob mayoff CGPath.foreach
extension CGPath {
    func forEach( body: @convention(block) (CGPathElement) -> Void) {
        typealias Body = @convention(block) (CGPathElement) -> Void
        func callback(info: UnsafeMutableRawPointer?, element: UnsafePointer<CGPathElement>) {
            let body = unsafeBitCast(info, to: Body.self)
            body(element.pointee)
        }
        let unsafeBody = unsafeBitCast(body, to: UnsafeMutableRawPointer.self)
        self.apply(info: unsafeBody, function: callback)
    }
}

// Finds the first point in a path
extension UIBezierPath {
    func firstPoint() -> CGPoint? {
        var firstPoint: CGPoint? = nil

        self.cgPath.forEach { element in
            // Just want the first one, but we have to look at everything
            guard firstPoint == nil else { return }
            assert(element.type == .moveToPoint, "Expected the first point to be a move")
            firstPoint = element.points.pointee
        }
        return firstPoint
    }
}
+6

-

-Subclass UIBezierPath

- startPoint

- moveToPoint startPoint

class MyBezierPath: UIBezierPath {
    var startPoint :CGPoint?

    override func moveToPoint(point: CGPoint) {
        super.moveToPoint(point)
        startPoint=point;
    }
}

    var myBezier = MyBezierPath()
    myBezier.moveToPoint(CGPoint(x: 0, y: 0))
    myBezier.addLineToPoint(CGPoint(x: 100, y: 0))
    myBezier.addLineToPoint(CGPoint(x: 50, y: 100))
+2

All Articles