JEL*_*JEL 4 core-graphics ios uibezierpath swift
我创建了一个UIBezierPath,但我不知道如何访问它的起点.我试过这样做:
let startPoint = path.currentPoint
Run Code Online (Sandbox Code Playgroud)
该属性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
}
}
Run Code Online (Sandbox Code Playgroud)
在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
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2869 次 |
| 最近记录: |