POPBasicAnimationInternal.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. Copyright (c) 2014-present, Facebook, Inc.
  3. All rights reserved.
  4. This source code is licensed under the BSD-style license found in the
  5. LICENSE file in the root directory of this source tree. An additional grant
  6. of patent rights can be found in the PATENTS file in the same directory.
  7. */
  8. #import "POPBasicAnimation.h"
  9. #import "POPPropertyAnimationInternal.h"
  10. // default animation duration
  11. static CGFloat const kPOPAnimationDurationDefault = 0.4;
  12. // progress threshold for computing done
  13. static CGFloat const kPOPProgressThreshold = 1e-6;
  14. static void interpolate(POPValueType valueType, NSUInteger count, const CGFloat *fromVec, const CGFloat *toVec, CGFloat *outVec, CGFloat p)
  15. {
  16. switch (valueType) {
  17. case kPOPValueInteger:
  18. case kPOPValueFloat:
  19. case kPOPValuePoint:
  20. case kPOPValueSize:
  21. case kPOPValueRect:
  22. case kPOPValueEdgeInsets:
  23. case kPOPValueColor:
  24. POPInterpolateVector(count, outVec, fromVec, toVec, p);
  25. break;
  26. default:
  27. NSCAssert(false, @"unhandled type %d", valueType);
  28. break;
  29. }
  30. }
  31. struct _POPBasicAnimationState : _POPPropertyAnimationState
  32. {
  33. CAMediaTimingFunction *timingFunction;
  34. double timingControlPoints[4];
  35. CFTimeInterval duration;
  36. CFTimeInterval timeProgress;
  37. _POPBasicAnimationState(id __unsafe_unretained anim) : _POPPropertyAnimationState(anim),
  38. timingFunction(nil),
  39. timingControlPoints{0.},
  40. duration(kPOPAnimationDurationDefault),
  41. timeProgress(0.)
  42. {
  43. type = kPOPAnimationBasic;
  44. }
  45. bool isDone() {
  46. if (_POPPropertyAnimationState::isDone()) {
  47. return true;
  48. }
  49. return timeProgress + kPOPProgressThreshold >= 1.;
  50. }
  51. void updatedTimingFunction()
  52. {
  53. float vec[4] = {0.};
  54. [timingFunction getControlPointAtIndex:1 values:&vec[0]];
  55. [timingFunction getControlPointAtIndex:2 values:&vec[2]];
  56. for (NSUInteger idx = 0; idx < POP_ARRAY_COUNT(vec); idx++) {
  57. timingControlPoints[idx] = vec[idx];
  58. }
  59. }
  60. bool advance(CFTimeInterval time, CFTimeInterval dt, id obj) {
  61. // default timing function
  62. if (!timingFunction) {
  63. ((POPBasicAnimation *)self).timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault];
  64. }
  65. // solve for normalized time, aka progresss [0, 1]
  66. CGFloat p = 1.0f;
  67. if (duration > 0.0f) {
  68. // cap local time to duration
  69. CFTimeInterval t = MIN(time - startTime, duration) / duration;
  70. p = POPTimingFunctionSolve(timingControlPoints, t, SOLVE_EPS(duration));
  71. timeProgress = t;
  72. } else {
  73. timeProgress = 1.;
  74. }
  75. // interpolate and advance
  76. interpolate(valueType, valueCount, fromVec->data(), toVec->data(), currentVec->data(), p);
  77. progress = p;
  78. clampCurrentValue();
  79. return true;
  80. }
  81. };
  82. typedef struct _POPBasicAnimationState POPBasicAnimationState;