Get position of path at time

前端 未结 3 1809
终归单人心
终归单人心 2021-01-13 06:17

is there a nice way to calculate the position of a path (CGPath or UIBezierPath) at a given time (from 0 to 1)?

Using CAShapeLayer for example, one can create an ani

3条回答
  •  无人及你
    2021-01-13 06:54

    If you keep records of all your points from the beginning, you can calculate the distance between them.
    When you want to know at a given time which of those points are being animated on the screen, you can do this:

    • first, get the current value of the strokeEnd (it's between 0 and 1) like this:

      CAShapeLayer *presentationLayer = (CAShapeLayer*)[_pathLayer presentationLayer];

      CGFloat strokeValue = [[presentationLayer valueForKey:@"strokeEnd"] floatValue];

    • then calculate the distance you already drew by now:

      CGFloat doneDistance = _allTheDistance * strokeValue;

    • after this, you have to iterate all your points and calculate the distance between them till you get that doneDistance

    This won't tell you exactly where on screen the path is, but the current point that is animated. Maybe it will help someone.

提交回复
热议问题