Skip to content

RCTScrollEvent: get all required values injected rather than accessing the scroll view #15008

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 41 additions & 17 deletions React/Views/RCTScrollView.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,23 @@ @interface RCTScrollEvent : NSObject <RCTEvent>

- (instancetype)initWithEventName:(NSString *)eventName
reactTag:(NSNumber *)reactTag
scrollView:(UIScrollView *)scrollView
scrollViewContentOffset:(CGPoint)scrollViewContentOffset
scrollViewContentInset:(UIEdgeInsets)scrollViewContentInset
scrollViewContentSize:(CGSize)scrollViewContentSize
scrollViewFrame:(CGRect)scrollViewFrame
scrollViewZoomScale:(CGFloat)scrollViewZoomScale
userData:(NSDictionary *)userData
coalescingKey:(uint16_t)coalescingKey NS_DESIGNATED_INITIALIZER;

@end

@implementation RCTScrollEvent
{
UIScrollView *_scrollView;
CGPoint _scrollViewContentOffset;
UIEdgeInsets _scrollViewContentInset;
CGSize _scrollViewContentSize;
CGRect _scrollViewFrame;
CGFloat _scrollViewZoomScale;
NSDictionary *_userData;
uint16_t _coalescingKey;
}
Expand All @@ -45,7 +53,11 @@ @implementation RCTScrollEvent

- (instancetype)initWithEventName:(NSString *)eventName
reactTag:(NSNumber *)reactTag
scrollView:(UIScrollView *)scrollView
scrollViewContentOffset:(CGPoint)scrollViewContentOffset
scrollViewContentInset:(UIEdgeInsets)scrollViewContentInset
scrollViewContentSize:(CGSize)scrollViewContentSize
scrollViewFrame:(CGRect)scrollViewFrame
scrollViewZoomScale:(CGFloat)scrollViewZoomScale
userData:(NSDictionary *)userData
coalescingKey:(uint16_t)coalescingKey
{
Expand All @@ -54,7 +66,11 @@ - (instancetype)initWithEventName:(NSString *)eventName
if ((self = [super init])) {
_eventName = [eventName copy];
_viewTag = reactTag;
_scrollView = scrollView;
_scrollViewContentOffset = scrollViewContentOffset;
_scrollViewContentInset = scrollViewContentInset;
_scrollViewContentSize = scrollViewContentSize;
_scrollViewFrame = scrollViewFrame;
_scrollViewZoomScale = scrollViewZoomScale;
_userData = userData;
_coalescingKey = coalescingKey;
}
Expand All @@ -72,24 +88,24 @@ - (NSDictionary *)body
{
NSDictionary *body = @{
@"contentOffset": @{
@"x": @(_scrollView.contentOffset.x),
@"y": @(_scrollView.contentOffset.y)
@"x": @(_scrollViewContentOffset.x),
@"y": @(_scrollViewContentOffset.y)
},
@"contentInset": @{
@"top": @(_scrollView.contentInset.top),
@"left": @(_scrollView.contentInset.left),
@"bottom": @(_scrollView.contentInset.bottom),
@"right": @(_scrollView.contentInset.right)
@"top": @(_scrollViewContentInset.top),
@"left": @(_scrollViewContentInset.left),
@"bottom": @(_scrollViewContentInset.bottom),
@"right": @(_scrollViewContentInset.right)
},
@"contentSize": @{
@"width": @(_scrollView.contentSize.width),
@"height": @(_scrollView.contentSize.height)
@"width": @(_scrollViewContentSize.width),
@"height": @(_scrollViewContentSize.height)
},
@"layoutMeasurement": @{
@"width": @(_scrollView.frame.size.width),
@"height": @(_scrollView.frame.size.height)
@"width": @(_scrollViewFrame.size.width),
@"height": @(_scrollViewFrame.size.height)
},
@"zoomScale": @(_scrollView.zoomScale ?: 1),
@"zoomScale": @(_scrollViewZoomScale ?: 1),
};

if (_userData) {
Expand Down Expand Up @@ -893,7 +909,11 @@ - (void)sendScrollEventWithName:(NSString *)eventName
}
RCTScrollEvent *scrollEvent = [[RCTScrollEvent alloc] initWithEventName:eventName
reactTag:self.reactTag
scrollView:scrollView
scrollViewContentOffset:scrollView.contentOffset
scrollViewContentInset:scrollView.contentInset
scrollViewContentSize:scrollView.contentSize
scrollViewFrame:scrollView.frame
scrollViewZoomScale:scrollView.zoomScale
userData:userData
coalescingKey:_coalescingKey];
[_eventDispatcher sendEvent:scrollEvent];
Expand All @@ -909,7 +929,11 @@ - (void)sendFakeScrollEvent:(NSNumber *)reactTag
NSString *eventName = NSStringFromSelector(@selector(onScroll));
RCTScrollEvent *fakeScrollEvent = [[RCTScrollEvent alloc] initWithEventName:eventName
reactTag:reactTag
scrollView:nil
scrollViewContentOffset:CGPointZero
scrollViewContentInset:UIEdgeInsetsZero
scrollViewContentSize:CGSizeZero
scrollViewFrame:CGRectZero
scrollViewZoomScale:0
userData:nil
coalescingKey:0];
[self sendEvent:fakeScrollEvent];
Expand Down