问题
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