POPPropertyAnimation.mm 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 "POPPropertyAnimationInternal.h"
  9. @implementation POPPropertyAnimation
  10. #pragma mark - Lifecycle
  11. #undef __state
  12. #define __state ((POPPropertyAnimationState *)_state)
  13. - (void)_initState
  14. {
  15. _state = new POPPropertyAnimationState(self);
  16. }
  17. #pragma mark - Properties
  18. DEFINE_RW_FLAG(POPPropertyAnimationState, additive, isAdditive, setAdditive:);
  19. DEFINE_RW_PROPERTY(POPPropertyAnimationState, roundingFactor, setRoundingFactor:, CGFloat);
  20. DEFINE_RW_PROPERTY(POPPropertyAnimationState, clampMode, setClampMode:, NSUInteger);
  21. DEFINE_RW_PROPERTY_OBJ(POPPropertyAnimationState, property, setProperty:, POPAnimatableProperty*, ((POPPropertyAnimationState*)_state)->updatedDynamicsThreshold(););
  22. DEFINE_RW_PROPERTY_OBJ_COPY(POPPropertyAnimationState, progressMarkers, setProgressMarkers:, NSArray*, ((POPPropertyAnimationState*)_state)->updatedProgressMarkers(););
  23. - (id)fromValue
  24. {
  25. return POPBox(__state->fromVec, __state->valueType);
  26. }
  27. - (void)setFromValue:(id)aValue
  28. {
  29. POPPropertyAnimationState *s = __state;
  30. VectorRef vec = POPUnbox(aValue, s->valueType, s->valueCount, YES);
  31. if (!vec_equal(vec, s->fromVec)) {
  32. s->fromVec = vec;
  33. if (s->tracing) {
  34. [s->tracer updateFromValue:aValue];
  35. }
  36. }
  37. }
  38. - (id)toValue
  39. {
  40. return POPBox(__state->toVec, __state->valueType);
  41. }
  42. - (void)setToValue:(id)aValue
  43. {
  44. POPPropertyAnimationState *s = __state;
  45. VectorRef vec = POPUnbox(aValue, s->valueType, s->valueCount, YES);
  46. if (!vec_equal(vec, s->toVec)) {
  47. s->toVec = vec;
  48. // invalidate to dependent state
  49. s->didReachToValue = false;
  50. s->distanceVec = NULL;
  51. if (s->tracing) {
  52. [s->tracer updateToValue:aValue];
  53. }
  54. // automatically unpause active animations
  55. if (s->active && s->paused) {
  56. s->setPaused(false);
  57. }
  58. }
  59. }
  60. - (id)currentValue
  61. {
  62. return POPBox(__state->currentValue(), __state->valueType);
  63. }
  64. #pragma mark - Utility
  65. - (void)_appendDescription:(NSMutableString *)s debug:(BOOL)debug
  66. {
  67. [s appendFormat:@"; from = %@; to = %@", describe(__state->fromVec), describe(__state->toVec)];
  68. if (_state->active)
  69. [s appendFormat:@"; currentValue = %@", describe(__state->currentValue())];
  70. if (__state->velocityVec && 0 != __state->velocityVec->norm())
  71. [s appendFormat:@"; velocity = %@", describe(__state->velocityVec)];
  72. if (!self.removedOnCompletion)
  73. [s appendFormat:@"; removedOnCompletion = %@", POPStringFromBOOL(self.removedOnCompletion)];
  74. if (__state->progressMarkers)
  75. [s appendFormat:@"; progressMarkers = [%@]", [__state->progressMarkers componentsJoinedByString:@", "]];
  76. if (_state->active)
  77. [s appendFormat:@"; progress = %f", __state->progress];
  78. }
  79. @end
  80. @implementation POPPropertyAnimation (NSCopying)
  81. - (instancetype)copyWithZone:(NSZone *)zone {
  82. POPPropertyAnimation *copy = [super copyWithZone:zone];
  83. if (copy) {
  84. copy.property = [self.property copyWithZone:zone];
  85. copy.fromValue = self.fromValue;
  86. copy.toValue = self.toValue;
  87. copy.roundingFactor = self.roundingFactor;
  88. copy.clampMode = self.clampMode;
  89. copy.additive = self.additive;
  90. }
  91. return copy;
  92. }
  93. @end
  94. @implementation POPPropertyAnimation (CustomProperty)
  95. + (instancetype)animationWithCustomPropertyNamed:(NSString *)name
  96. readBlock:(POPAnimatablePropertyReadBlock)readBlock
  97. writeBlock:(POPAnimatablePropertyWriteBlock)writeBlock
  98. {
  99. POPPropertyAnimation *animation = [[self alloc] init];
  100. animation.property = [POPAnimatableProperty propertyWithName:name initializer:^(POPMutableAnimatableProperty *prop) {
  101. prop.readBlock = readBlock;
  102. prop.writeBlock = writeBlock;
  103. }];
  104. return animation;
  105. }
  106. + (instancetype)animationWithCustomPropertyReadBlock:(POPAnimatablePropertyReadBlock)readBlock
  107. writeBlock:(POPAnimatablePropertyWriteBlock)writeBlock
  108. {
  109. return [self animationWithCustomPropertyNamed:[NSUUID UUID].UUIDString
  110. readBlock:readBlock
  111. writeBlock:writeBlock];
  112. }
  113. @end