假设我有一条随机的bezier路径,如下所示:
let bezierPath = UIBezierPath()
bezierPath.move(to: CGPoint(x: 3, y: 0.84))
bezierPath.addCurve(to: CGPoint(x: 11, y: 8.84), controlPoint1: CGPoint(x: 3, y: 1.84), controlPoint2: CGPoint(x: 9, y: 4.59))
// [...]
bezierPath.addCurve(to: CGPoint(x: 3, y: 0.84), controlPoint1: CGPoint(x: 7, y: 4.84), controlPoint2: CGPoint(x: 3, y: -0.16))
bezierPath.close()
我想要创建一个函数来计算给定百分比的CGPoint,其中0%是bezierPath的第一点,100%是最后一点:
extension UIBezierPath {
func getPointFor(percentage: Float) -> CGPoint {
//Computation logic
return result
}
}
我已经找到了这个帖子,但是这个解决方案不允许我得到所有的点(例如,在路径的15.5%的位置)。
有办法这样做吗?
发布于 2016-11-08 00:17:52
我找到了一个用目标C写的解决方案。您可以找到源代码这里。
我通过使用桥接头来设法使用它:
#ifndef bridging_header_h
#define bridging_header_h
#import "UIBezierPath+Length.h"
#endif /* bridge_header_h */
您可以像这样使用这两个函数:
print("length=\(bezierPath.length())")
for i in 0...100 {
let percent:CGFloat = CGFloat(i) / 100.0
print("point at [\(percent)]=\(bezierPath.point(atPercentOfLength: percent))")
}
产出:
length=143.316117804497
point at [0.0]=(3.0, 0.839999973773956)
point at [0.01]=(3.26246070861816, 1.29733419418335)
point at [0.02]=(3.97137236595154, 1.91627132892609)
point at [0.03]=(5.00902938842773, 2.69386911392212)
[...]
point at [0.99]=(3.27210903167725, 0.765813827514648)
point at [1.0]=(3.0, 0.839999973773956)
https://stackoverflow.com/questions/40471362
复制相似问题