LOTPointInterpolator.m 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. //
  2. // LOTPointInterpolator.m
  3. // Lottie
  4. //
  5. // Created by brandon_withrow on 7/12/17.
  6. // Copyright © 2017 Airbnb. All rights reserved.
  7. //
  8. #import "LOTPointInterpolator.h"
  9. #import "CGGeometry+LOTAdditions.h"
  10. @implementation LOTPointInterpolator
  11. - (CGPoint)pointValueForFrame:(NSNumber *)frame {
  12. CGFloat progress = [self progressForFrame:frame];
  13. CGPoint returnPoint;
  14. if (progress == 0) {
  15. returnPoint = self.leadingKeyframe.pointValue;
  16. } else if (progress == 1) {
  17. returnPoint = self.trailingKeyframe.pointValue;
  18. } else if (!CGPointEqualToPoint(self.leadingKeyframe.spatialOutTangent, CGPointZero) ||
  19. !CGPointEqualToPoint(self.trailingKeyframe.spatialInTangent, CGPointZero)) {
  20. // Spatial Bezier path
  21. CGPoint outTan = LOT_PointAddedToPoint(self.leadingKeyframe.pointValue, self.leadingKeyframe.spatialOutTangent);
  22. CGPoint inTan = LOT_PointAddedToPoint(self.trailingKeyframe.pointValue, self.trailingKeyframe.spatialInTangent);
  23. returnPoint = LOT_PointInCubicCurve(self.leadingKeyframe.pointValue, outTan, inTan, self.trailingKeyframe.pointValue, progress);
  24. } else {
  25. returnPoint = LOT_PointInLine(self.leadingKeyframe.pointValue, self.trailingKeyframe.pointValue, progress);
  26. }
  27. if (self.hasDelegateOverride) {
  28. return [self.delegate pointForFrame:frame.floatValue
  29. startKeyframe:self.leadingKeyframe.keyframeTime.floatValue
  30. endKeyframe:self.trailingKeyframe.keyframeTime.floatValue
  31. interpolatedProgress:progress
  32. startPoint:self.leadingKeyframe.pointValue
  33. endPoint:self.trailingKeyframe.pointValue
  34. currentPoint:returnPoint];
  35. }
  36. return returnPoint;
  37. }
  38. - (BOOL)hasDelegateOverride {
  39. return self.delegate != nil;
  40. }
  41. - (void)setValueDelegate:(id<LOTValueDelegate>)delegate {
  42. NSAssert(([delegate conformsToProtocol:@protocol(LOTPointValueDelegate)]), @"Point Interpolator set with incorrect callback type. Expected LOTPointValueDelegate");
  43. self.delegate = (id<LOTPointValueDelegate>)delegate;
  44. }
  45. @end