UIBezierPath point at fraction of path

我们两清 提交于 2019-12-22 10:49:35

问题


Given an arbitrary UIBezierPath, I'm looking for a way to get a point at a fraction of the length of that path.

Example:

UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(200.0, 200.0)];
[path addLineToPoint:CGPointMake(200.0, 400.0)];

CGPoint p = [path pointAtFraction:0.5];

p should be {x: 200.0, y: 300.0} in this case.

I'm aware that this simple example could be calculated, but I'm looking for a solution that fits ANY UIBezierPath (arcs, rounded rects, etc.)

Seeing CAShapeLayer, which basically lives off UIBezierPath, and its property strokeEnd, I suppose the information is somewhere inside the path object. However, neither UIBezierPath nor CGPathRef interfaces show any way to achieve this.

I tried creating a CAShapeLayer, setting the strokeEnd and retrieving the CGPathRef from the layer, but the path stays the same.

Is there any way (public API) to achieve this ?


回答1:


Some time ago I've made a carousel menu based on bezier cubic curve. I think this code can help you:

CGFloat bezierInterpolation(CGFloat t, CGFloat a, CGFloat b, CGFloat c, CGFloat d) {
    CGFloat t2 = t * t;
    CGFloat t3 = t2 * t;
    return a + (-a * 3 + t * (3 * a - a * t)) * t
           + (3 * b + t * (-6 * b + b * 3 * t)) * t
           + (c * 3 - c * 3 * t) * t2
           + d * t3;
}

- (CGPoint)getPointForT:(CGFloat)t {
    return CGPointMake(bezierInterpolation(t, _p1.x, _p2.x, _p3.x, _p4.x), bezierInterpolation(t, _p1.y, _p2.y, _p3.y, _p4.y));
}

t - your fraction length <0,1>

_p1, _p2, _p3, _p4 - curve's points (check this site to visualize it a little bit: http://cubic-bezier.com/#.17,.67,.83,.67)




回答2:


Sorry for the huge delay. I actually solved this more than 1 year ago, wow.
I created a small category on UIBezierPath which does the job. GitHub Repo here

After re-reading my code from 1 year back I have to say it's quite shocking how I used to code back then :D The lack of documentation is disturbing. If you need more info please let me know and I'll update the repo.



来源:https://stackoverflow.com/questions/25097503/uibezierpath-point-at-fraction-of-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!