POPAnimationTracer.mm 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 "POPAnimationTracer.h"
  9. #import <QuartzCore/QuartzCore.h>
  10. #import "POPAnimationEventInternal.h"
  11. #import "POPAnimationInternal.h"
  12. #import "POPSpringAnimation.h"
  13. @implementation POPAnimationTracer
  14. {
  15. __weak POPAnimation *_animation;
  16. POPAnimationState *_animationState;
  17. NSMutableArray *_events;
  18. BOOL _animationHasVelocity;
  19. }
  20. @synthesize shouldLogAndResetOnCompletion = _shouldLogAndResetOnCompletion;
  21. static POPAnimationEvent *create_event(POPAnimationTracer *self, POPAnimationEventType type, id value = nil, bool recordAnimation = false)
  22. {
  23. bool useLocalTime = 0 != self->_animationState->startTime;
  24. CFTimeInterval time = useLocalTime
  25. ? self->_animationState->lastTime - self->_animationState->startTime
  26. : self->_animationState->lastTime;
  27. POPAnimationEvent *event;
  28. if (!value) {
  29. event = [[POPAnimationEvent alloc] initWithType:type time:time];
  30. } else {
  31. event = [[POPAnimationValueEvent alloc] initWithType:type time:time value:value];
  32. if (self->_animationHasVelocity) {
  33. [(POPAnimationValueEvent *)event setVelocity:[(POPSpringAnimation *)self->_animation velocity]];
  34. }
  35. }
  36. if (recordAnimation) {
  37. event.animationDescription = [self->_animation description];
  38. }
  39. return event;
  40. }
  41. - (id)initWithAnimation:(POPAnimation *)anAnim
  42. {
  43. self = [super init];
  44. if (nil != self) {
  45. _animation = anAnim;
  46. _animationState = POPAnimationGetState(anAnim);
  47. _events = [[NSMutableArray alloc] initWithCapacity:50];
  48. _animationHasVelocity = [anAnim respondsToSelector:@selector(velocity)];
  49. }
  50. return self;
  51. }
  52. - (void)readPropertyValue:(id)aValue
  53. {
  54. POPAnimationEvent *event = create_event(self, kPOPAnimationEventPropertyRead, aValue);
  55. [_events addObject:event];
  56. }
  57. - (void)writePropertyValue:(id)aValue
  58. {
  59. POPAnimationEvent *event = create_event(self, kPOPAnimationEventPropertyWrite, aValue);
  60. [_events addObject:event];
  61. }
  62. - (void)updateToValue:(id)aValue
  63. {
  64. POPAnimationEvent *event = create_event(self, kPOPAnimationEventToValueUpdate, aValue);
  65. [_events addObject:event];
  66. }
  67. - (void)updateFromValue:(id)aValue
  68. {
  69. POPAnimationEvent *event = create_event(self, kPOPAnimationEventFromValueUpdate, aValue);
  70. [_events addObject:event];
  71. }
  72. - (void)updateVelocity:(id)aValue
  73. {
  74. POPAnimationEvent *event = create_event(self, kPOPAnimationEventVelocityUpdate, aValue);
  75. [_events addObject:event];
  76. }
  77. - (void)updateSpeed:(float)aFloat
  78. {
  79. POPAnimationEvent *event = create_event(self, kPOPAnimationEventSpeedUpdate, @(aFloat));
  80. [_events addObject:event];
  81. }
  82. - (void)updateBounciness:(float)aFloat
  83. {
  84. POPAnimationEvent *event = create_event(self, kPOPAnimationEventBouncinessUpdate, @(aFloat));
  85. [_events addObject:event];
  86. }
  87. - (void)updateFriction:(float)aFloat
  88. {
  89. POPAnimationEvent *event = create_event(self, kPOPAnimationEventFrictionUpdate, @(aFloat));
  90. [_events addObject:event];
  91. }
  92. - (void)updateMass:(float)aFloat
  93. {
  94. POPAnimationEvent *event = create_event(self, kPOPAnimationEventMassUpdate, @(aFloat));
  95. [_events addObject:event];
  96. }
  97. - (void)updateTension:(float)aFloat
  98. {
  99. POPAnimationEvent *event = create_event(self, kPOPAnimationEventTensionUpdate, @(aFloat));
  100. [_events addObject:event];
  101. }
  102. - (void)didStart
  103. {
  104. POPAnimationEvent *event = create_event(self, kPOPAnimationEventDidStart, nil, true);
  105. [_events addObject:event];
  106. }
  107. - (void)didStop:(BOOL)finished
  108. {
  109. POPAnimationEvent *event = create_event(self, kPOPAnimationEventDidStop, @(finished), true);
  110. [_events addObject:event];
  111. if (_shouldLogAndResetOnCompletion) {
  112. NSLog(@"events:%@", self.allEvents);
  113. [self reset];
  114. }
  115. }
  116. - (void)didReachToValue:(id)aValue
  117. {
  118. POPAnimationEvent *event = create_event(self, kPOPAnimationEventDidReachToValue, aValue);
  119. [_events addObject:event];
  120. }
  121. - (void)autoreversed
  122. {
  123. POPAnimationEvent *event = create_event(self, kPOPAnimationEventAutoreversed);
  124. [_events addObject:event];
  125. }
  126. - (void)start
  127. {
  128. POPAnimationState *s = POPAnimationGetState(_animation);
  129. s->tracing = true;
  130. }
  131. - (void)stop
  132. {
  133. POPAnimationState *s = POPAnimationGetState(_animation);
  134. s->tracing = false;
  135. }
  136. - (void)reset
  137. {
  138. [_events removeAllObjects];
  139. }
  140. - (NSArray *)allEvents
  141. {
  142. return [_events copy];
  143. }
  144. - (NSArray *)writeEvents
  145. {
  146. return [self eventsWithType:kPOPAnimationEventPropertyWrite];
  147. }
  148. - (NSArray *)eventsWithType:(POPAnimationEventType)aType
  149. {
  150. NSMutableArray *array = [NSMutableArray array];
  151. for (POPAnimationEvent *event in _events) {
  152. if (aType == event.type) {
  153. [array addObject:event];
  154. }
  155. }
  156. return array;
  157. }
  158. @end