我创建了一个UIBezierPath,但我不知道如何访问它的起点。我试着这样做:获取UIBezier路径的起点
let startPoint = path.currentPoint
酒店currentPoint
给我的最后一个点,而不是起点。我需要起点的原因是因为我想在路径的起始点放置图像。
任何想法?
我创建了一个UIBezierPath,但我不知道如何访问它的起点。我试着这样做:获取UIBezier路径的起点
let startPoint = path.currentPoint
酒店currentPoint
给我的最后一个点,而不是起点。我需要起点的原因是因为我想在路径的起始点放置图像。
任何想法?
您需要下拉到CGPath
并使用CGPathApply
来遍历元素。你只需要第一个,但你必须全部看。
我假设你的路径格式良好,并以“移动”开始。这应该永远是正确的UIBezierPath
(我不知道有任何办法让它不会是真的。)
你需要从抢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
}
}
在斯威夫特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
}
}
简单的方法是
-Subclass的UIBezierPath
- 创建自定义属性为的startPoint
-override的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))
如果我们在moveToPoint函数中设置了它的值,我们为什么要搜索起始点? –
@AlexKosyakov例如,您可能已经从path.addArc(...)开始。 –
@VladimirShutyuk明白了,tnx! –
由于它的作品!我还有一个问题,并提出了另一个问题。你可以看看它,看看你能回答吗?继承人的链接:http://stackoverflow.com/questions/36705072/dynamically-change-position-based-on-scrollview – JEL
嗨,我用Xcode将此代码转换为Swift 3.0。我现在正在Xcode发生更改的这一行发生崩溃:'apply(info:unsafeBody,function:callback as!CGPathApplierFunction)''。不幸的是,在调试器中没有任何东西,但是出现'thread 1 exc_bad_instruction(code = exc_1386_invop subcode = 0x0)'。我的研究表明,这是由于武力解开(在这种情况下强制铸造)。如果我尝试安全地施放,那么我会遇到'Segmentation fault:11'。有什么建议么? – JEL
“CGPathApplierFunction”的类型已更改。它现在需要一个'UnsafeMutableRawPointer?'。你必须改变'callback'的签名来匹配,而不是强制转换它。 –