From ac743e902af29fd2748399cff5f0115d0afde0a2 Mon Sep 17 00:00:00 2001 From: James Ide Date: Sat, 28 Feb 2015 04:22:26 -0800 Subject: [PATCH] [Scroll] Add scrollWithoutAnimationTo to immediately scroll This is an API like setting the contentOffset except it is imperative, which is usually what you want unless the scroll view is a controlled component. --- Libraries/Components/ScrollView/ScrollView.js | 8 ++++++++ React/Modules/RCTUIManager.m | 14 ++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/Libraries/Components/ScrollView/ScrollView.js b/Libraries/Components/ScrollView/ScrollView.js index 92cdab05ac0bc1..a51a93b9e9259f 100644 --- a/Libraries/Components/ScrollView/ScrollView.js +++ b/Libraries/Components/ScrollView/ScrollView.js @@ -195,6 +195,14 @@ var ScrollView = React.createClass({ ); }, + scrollWithoutAnimationTo: function(destY?: number, destX?: number) { + RCTUIManager.scrollWithoutAnimationTo( + this.getNodeHandle(), + destX || 0, + destY || 0 + ); + }, + render: function() { var contentContainerStyle = [ this.props.horizontal && styles.contentContainerHorizontal, diff --git a/React/Modules/RCTUIManager.m b/React/Modules/RCTUIManager.m index a0e7285c6b0dfb..833ada9725395e 100644 --- a/React/Modules/RCTUIManager.m +++ b/React/Modules/RCTUIManager.m @@ -1048,6 +1048,20 @@ - (void)scrollToOffsetWithView:(NSNumber *)reactTag scrollToOffsetX:(NSNumber *) }]; } +- (void)scrollToOffsetWithoutAnimationWithView:(NSNumber *)reactTag scrollToOffsetX:(NSNumber *)offsetX offsetY:(NSNumber *)offsetY +{ + RCT_EXPORT(scrollWithoutAnimationTo); + + [self addUIBlock:^(RCTUIManager *uiManager, RCTSparseArray *viewRegistry){ + UIView *view = viewRegistry[reactTag]; + if ([view conformsToProtocol:@protocol(RCTScrollableProtocol)]) { + [(id)view scrollToOffset:CGPointMake([offsetX floatValue], [offsetY floatValue]) animated:NO]; + } else { + RCTLogError(@"tried to scrollToOffsetWithoutAnimation: on non-RCTScrollableProtocol view %@ with tag %@", view, reactTag); + } + }]; +} + - (void)zoomToRectWithView:(NSNumber *)reactTag rect:(NSDictionary *)rectDict { RCT_EXPORT(zoomToRect);