RCTDiffClampAnimatedNode.m 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. #import <React/RCTDiffClampAnimatedNode.h>
  8. #import <React/RCTLog.h>
  9. @implementation RCTDiffClampAnimatedNode
  10. {
  11. NSNumber *_inputNodeTag;
  12. CGFloat _min;
  13. CGFloat _max;
  14. CGFloat _lastValue;
  15. }
  16. - (instancetype)initWithTag:(NSNumber *)tag
  17. config:(NSDictionary<NSString *, id> *)config
  18. {
  19. if (self = [super initWithTag:tag config:config]) {
  20. _inputNodeTag = config[@"input"];
  21. _min = [config[@"min"] floatValue];
  22. _max = [config[@"max"] floatValue];
  23. }
  24. return self;
  25. }
  26. - (void)onAttachedToNode:(RCTAnimatedNode *)parent
  27. {
  28. [super onAttachedToNode:parent];
  29. self.value = _lastValue = [self inputNodeValue];
  30. }
  31. - (void)performUpdate
  32. {
  33. [super performUpdate];
  34. CGFloat value = [self inputNodeValue];
  35. CGFloat diff = value - _lastValue;
  36. _lastValue = value;
  37. self.value = MIN(MAX(self.value + diff, _min), _max);
  38. }
  39. - (CGFloat)inputNodeValue
  40. {
  41. RCTValueAnimatedNode *inputNode = (RCTValueAnimatedNode *)[self.parentNodes objectForKey:_inputNodeTag];
  42. if (![inputNode isKindOfClass:[RCTValueAnimatedNode class]]) {
  43. RCTLogError(@"Illegal node ID set as an input for Animated.DiffClamp node");
  44. return 0;
  45. }
  46. return inputNode.value;
  47. }
  48. @end