gpt4 book ai didi

ios - 获取 UIBezierPath 的起点

转载 作者:搜寻专家 更新时间:2023-10-31 08:18:06 24 4
gpt4 key购买 nike

我创建了一个 UIBezierPath,但我不知道如何访问它的起点。我试过这样做:

let startPoint = path.currentPoint

属性 currentPoint 给我最后一点,而不是起点。我需要起点的原因是因为我想在路径的起点放置一个图像。

有什么想法吗?

最佳答案

您需要下拉到 CGPath 并使用 CGPathApply 遍历元素。您只想要第一个,但您必须查看所有。

我假设您的路径格式正确,并且以“移动”开头。这对于 UIBezierPath 应该总是正确的(我不知道有什么方法可以让它不正确。)

您需要 rob mayoff 的 CGPath.forEach 的帮助,这很棘手,但有了它就很简单了:

// rob mayoff's 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's 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
}
}

关于ios - 获取 UIBezierPath 的起点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36702853/

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