Skip to content

Commit f55a999

Browse files
marty-wangMo Wang
authored andcommitted
scroll content with animation (microsoft#725)
Summary: please see test plan Test Plan: |Before|After| |{F371503806}|{F371499708}| Reviewers: skyle Reviewed By: skyle Subscribers: zackargyle Differential Revision: https://phabricator.intern.facebook.com/D26354172 Tasks: T84165504 Signature: 26354172:1612920735:2cd8455b1bae06ee555bd98cfd41c4dfb29c288e Co-authored-by: Mo Wang <[email protected]>
1 parent fbe63db commit f55a999

File tree

1 file changed

+29
-21
lines changed

1 file changed

+29
-21
lines changed

React/Views/ScrollView/RCTScrollView.m

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ - (void)setContentOffset:(CGPoint)contentOffset
237237
if (!NSEqualPoints(contentOffset, self.documentVisibleRect.origin))
238238
{
239239
[self.contentView scrollToPoint:contentOffset];
240+
[self reflectScrolledClipView:self.contentView];
240241
}
241242
#else // ]TODO(macOS ISS#2323203)
242243
super.contentOffset = CGPointMake(
@@ -245,6 +246,21 @@ - (void)setContentOffset:(CGPoint)contentOffset
245246
#endif // TODO(macOS ISS#2323203)
246247
}
247248

249+
#if TARGET_OS_OSX // [TODO(macOS ISS#2323203)
250+
- (void)setContentOffset:(CGPoint)contentOffset
251+
animated:(BOOL)animated
252+
{
253+
if (animated) {
254+
[NSAnimationContext beginGrouping];
255+
[[NSAnimationContext currentContext] setDuration:0.3];
256+
[[self.contentView animator] setBoundsOrigin:contentOffset];
257+
[NSAnimationContext endGrouping];
258+
} else {
259+
self.contentOffset = contentOffset;
260+
}
261+
}
262+
#endif // TODO(macOS ISS#2323203)
263+
248264
- (void)setFrame:(CGRect)frame
249265
{
250266
// Preserving and revalidating `contentOffset`.
@@ -321,12 +337,12 @@ - (BOOL)resignFirstResponder
321337
return YES;
322338
}
323339

324-
- (void)setAccessibilityLabel:(NSString *)accessibilityLabel
340+
- (void)setAccessibilityLabel:(NSString *)accessibilityLabel
325341
{
326342
[super setAccessibilityLabel:accessibilityLabel];
327343
[[self documentView] setAccessibilityLabel:accessibilityLabel];
328344
}
329-
- (void)setDocumentView:(__kindof NSView *)documentView
345+
- (void)setDocumentView:(__kindof NSView *)documentView
330346
{
331347
[super setDocumentView:documentView];
332348
[documentView setAccessibilityLabel:[self accessibilityLabel]];
@@ -457,7 +473,7 @@ - (BOOL)resignFirstResponder
457473
return [_scrollView resignFirstResponder];
458474
}
459475

460-
- (void)setAccessibilityLabel:(NSString *)accessibilityLabel
476+
- (void)setAccessibilityLabel:(NSString *)accessibilityLabel
461477
{
462478
[_scrollView setAccessibilityLabel:accessibilityLabel];
463479
}
@@ -494,7 +510,7 @@ - (void)insertReactSubview:(RCTUIView *)view atIndex:(NSInteger)atIndex // TODO(
494510
[super insertReactSubview:view atIndex:atIndex];
495511
#if TARGET_OS_OSX // [TODO(macOS ISS#2323203)
496512
RCTAssert(self.contentView == nil, @"RCTScrollView may only contain a single subview");
497-
513+
498514
_scrollView.documentView = view;
499515
#else // ]TODO(macOS ISS#2323203)
500516
#if !TARGET_OS_TV
@@ -714,19 +730,16 @@ - (void)scrollToOffset:(CGPoint)offset animated:(BOOL)animated
714730
0.01)); // Make width and height greater than 0
715731
// Ensure at least one scroll event will fire
716732
_allowNextScrollNoMatterWhat = YES;
717-
#if TARGET_OS_OSX // [TODO(macOS ISS#2323203)
718-
(void) animated;
719-
_scrollView.contentOffset = offset;
720-
#else // ]TODO(macOS ISS#2323203)
733+
721734
if (!CGRectContainsPoint(maxRect, offset) && !self.scrollToOverflowEnabled) {
722735
CGFloat x = fmax(offset.x, CGRectGetMinX(maxRect));
723736
x = fmin(x, CGRectGetMaxX(maxRect));
724737
CGFloat y = fmax(offset.y, CGRectGetMinY(maxRect));
725738
y = fmin(y, CGRectGetMaxY(maxRect));
726739
offset = CGPointMake(x, y);
727740
}
741+
728742
[_scrollView setContentOffset:offset animated:animated];
729-
#endif // TODO(macOS ISS#2323203)
730743
}
731744
}
732745

@@ -753,12 +766,7 @@ - (void)scrollToEnd:(BOOL)animated
753766
if (!CGPointEqualToPoint(_scrollView.contentOffset, offset)) {
754767
// Ensure at least one scroll event will fire
755768
_allowNextScrollNoMatterWhat = YES;
756-
#if TARGET_OS_OSX // [TODO(macOS ISS#2323203)
757-
(void) animated;
758-
_scrollView.contentOffset = offset;
759-
#else // ]TODO(macOS ISS#2323203)
760769
[_scrollView setContentOffset:offset animated:animated];
761-
#endif // TODO(macOS ISS#2323203)
762770
}
763771
}
764772

@@ -787,7 +795,7 @@ - (void)flashScrollIndicators
787795
#endif
788796
}
789797
// ]TODO(macOS ISS#2323203)
790-
798+
791799
#pragma mark - ScrollView delegate
792800

793801
#if TARGET_OS_OSX // [TODO(macOS ISS#2323203)
@@ -1277,22 +1285,22 @@ - (NSString*)keyCommandFromKeyCode:(NSInteger)keyCode
12771285
{
12781286
case 36:
12791287
return @"ENTER";
1280-
1288+
12811289
case 116:
12821290
return @"PAGE_UP";
12831291

12841292
case 121:
12851293
return @"PAGE_DOWN";
1286-
1294+
12871295
case 123:
12881296
return @"LEFT_ARROW";
1289-
1297+
12901298
case 124:
12911299
return @"RIGHT_ARROW";
1292-
1300+
12931301
case 125:
12941302
return @"DOWN_ARROW";
1295-
1303+
12961304
case 126:
12971305
return @"UP_ARROW";
12981306
}
@@ -1308,7 +1316,7 @@ - (void)keyDown:(UIEvent*)theEvent
13081316
RCT_SEND_SCROLL_EVENT(onScrollKeyDown, (@{ @"key": keyCommand}));
13091317
} else {
13101318
[super keyDown:theEvent];
1311-
1319+
13121320
// AX: if a tab key was pressed and the first responder is currently clipped by the scroll view,
13131321
// automatically scroll to make the view visible to make it navigable via keyboard.
13141322
if ([theEvent keyCode] == 48) { //tab key

0 commit comments

Comments
 (0)