RCTScrollEvent.m 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 "RCTScrollEvent.h"
  8. #import <React/RCTAssert.h>
  9. @implementation RCTScrollEvent {
  10. CGPoint _scrollViewContentOffset;
  11. UIEdgeInsets _scrollViewContentInset;
  12. CGSize _scrollViewContentSize;
  13. CGRect _scrollViewFrame;
  14. CGFloat _scrollViewZoomScale;
  15. NSDictionary *_userData;
  16. uint16_t _coalescingKey;
  17. }
  18. @synthesize viewTag = _viewTag;
  19. @synthesize eventName = _eventName;
  20. - (instancetype)initWithEventName:(NSString *)eventName
  21. reactTag:(NSNumber *)reactTag
  22. scrollViewContentOffset:(CGPoint)scrollViewContentOffset
  23. scrollViewContentInset:(UIEdgeInsets)scrollViewContentInset
  24. scrollViewContentSize:(CGSize)scrollViewContentSize
  25. scrollViewFrame:(CGRect)scrollViewFrame
  26. scrollViewZoomScale:(CGFloat)scrollViewZoomScale
  27. userData:(NSDictionary *)userData
  28. coalescingKey:(uint16_t)coalescingKey
  29. {
  30. RCTAssertParam(reactTag);
  31. if ((self = [super init])) {
  32. _eventName = [eventName copy];
  33. _viewTag = reactTag;
  34. _scrollViewContentOffset = scrollViewContentOffset;
  35. _scrollViewContentInset = scrollViewContentInset;
  36. _scrollViewContentSize = scrollViewContentSize;
  37. _scrollViewFrame = scrollViewFrame;
  38. _scrollViewZoomScale = scrollViewZoomScale;
  39. _userData = userData;
  40. _coalescingKey = coalescingKey;
  41. }
  42. return self;
  43. }
  44. RCT_NOT_IMPLEMENTED(-(instancetype)init)
  45. - (uint16_t)coalescingKey
  46. {
  47. return _coalescingKey;
  48. }
  49. - (NSDictionary *)body
  50. {
  51. NSDictionary *body = @{
  52. @"contentOffset" : @{@"x" : @(_scrollViewContentOffset.x), @"y" : @(_scrollViewContentOffset.y)},
  53. @"contentInset" : @{
  54. @"top" : @(_scrollViewContentInset.top),
  55. @"left" : @(_scrollViewContentInset.left),
  56. @"bottom" : @(_scrollViewContentInset.bottom),
  57. @"right" : @(_scrollViewContentInset.right)
  58. },
  59. @"contentSize" : @{@"width" : @(_scrollViewContentSize.width), @"height" : @(_scrollViewContentSize.height)},
  60. @"layoutMeasurement" : @{@"width" : @(_scrollViewFrame.size.width), @"height" : @(_scrollViewFrame.size.height)},
  61. @"zoomScale" : @(_scrollViewZoomScale ?: 1),
  62. };
  63. if (_userData) {
  64. NSMutableDictionary *mutableBody = [body mutableCopy];
  65. [mutableBody addEntriesFromDictionary:_userData];
  66. body = mutableBody;
  67. }
  68. return body;
  69. }
  70. - (BOOL)canCoalesce
  71. {
  72. return YES;
  73. }
  74. - (RCTScrollEvent *)coalesceWithEvent:(RCTScrollEvent *)newEvent
  75. {
  76. NSArray<NSDictionary *> *updatedChildFrames =
  77. [_userData[@"updatedChildFrames"] arrayByAddingObjectsFromArray:newEvent->_userData[@"updatedChildFrames"]];
  78. if (updatedChildFrames) {
  79. NSMutableDictionary *userData = [newEvent->_userData mutableCopy];
  80. userData[@"updatedChildFrames"] = updatedChildFrames;
  81. newEvent->_userData = userData;
  82. }
  83. return newEvent;
  84. }
  85. + (NSString *)moduleDotMethod
  86. {
  87. return @"RCTEventEmitter.receiveEvent";
  88. }
  89. - (NSArray *)arguments
  90. {
  91. return @[ self.viewTag, RCTNormalizeInputEventName(self.eventName), [self body] ];
  92. }
  93. @end