react-native-navigation的迁移库

RNNInterpolator.m 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #import "RNNInterpolator.h"
  2. #import "Color+Interpolation.h"
  3. @implementation RNNInterpolator
  4. + (UIColor *)fromColor:(UIColor *)fromColor toColor:(UIColor *)toColor precent:(CGFloat)precent {
  5. return [fromColor ?: UIColor.clearColor interpolateToValue:toColor ?: UIColor.clearColor progress:precent behavior:RNNInterpolationBehaviorUseLABColorSpace];
  6. }
  7. + (CGFloat)fromFloat:(CGFloat)from toFloat:(CGFloat)to precent:(CGFloat)precent interpolation:(RNNInterpolationOptions)interpolation {
  8. return RNNInterpolate(from, to, precent, interpolation);
  9. }
  10. + (CGRect)fromRect:(CGRect)from toRect:(CGRect)to precent:(CGFloat)p interpolation:(RNNInterpolationOptions)interpolation {
  11. return CGRectMake(RNNInterpolate(from.origin.x, to.origin.x, p, interpolation),
  12. RNNInterpolate(from.origin.y, to.origin.y, p, interpolation),
  13. RNNInterpolate(from.size.width, to.size.width, p, interpolation),
  14. RNNInterpolate(from.size.height, to.size.height, p, interpolation));
  15. }
  16. static CGFloat RNNApplyInterpolation(CGFloat p, RNNInterpolationOptions interpolation) {
  17. switch (interpolation) {
  18. case RNNInterpolationAccelerate:
  19. return RNNAccelerate(p);
  20. case RNNInterpolationAccelerateDecelerate:
  21. return RNNAccelerateDecelerate(p);
  22. case RNNInterpolationLinear:
  23. return RNNLinear(p);
  24. case RNNInterpolationDecelerate:
  25. return RNNDecelerate(p);
  26. }
  27. }
  28. static CGFloat RNNInterpolate(CGFloat from, CGFloat to, CGFloat p, RNNInterpolationOptions interpolation) {
  29. return from + RNNApplyInterpolation(p, interpolation) * (to - from);
  30. }
  31. static CGFloat RNNLinear(CGFloat p) {
  32. return p;
  33. }
  34. static CGFloat RNNAccelerate(CGFloat p) {
  35. return p * p;
  36. }
  37. static CGFloat RNNDecelerate(CGFloat p) {
  38. return -(p * (p - 2));
  39. }
  40. static CGFloat RNNAccelerateDecelerate(CGFloat p) {
  41. if (p < 0.5) {
  42. return 4 * p * p * p;
  43. } else {
  44. CGFloat f = ((2 * p) - 2);
  45. return 0.5 * f * f * f + 1;
  46. }
  47. }
  48. @end