Skip to content

Commit

Permalink
scroll content with animation (#725)
Browse files Browse the repository at this point in the history
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 <mowang@fb.com>
  • Loading branch information
marty-wang and Mo Wang authored Feb 18, 2021
1 parent 3e01028 commit f37f72e
Showing 1 changed file with 29 additions and 21 deletions.
50 changes: 29 additions & 21 deletions React/Views/ScrollView/RCTScrollView.m
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ - (void)setContentOffset:(CGPoint)contentOffset
if (!NSEqualPoints(contentOffset, self.documentVisibleRect.origin))
{
[self.contentView scrollToPoint:contentOffset];
[self reflectScrolledClipView:self.contentView];
}
#else // ]TODO(macOS ISS#2323203)
super.contentOffset = CGPointMake(
Expand All @@ -245,6 +246,21 @@ - (void)setContentOffset:(CGPoint)contentOffset
#endif // TODO(macOS ISS#2323203)
}

#if TARGET_OS_OSX // [TODO(macOS ISS#2323203)
- (void)setContentOffset:(CGPoint)contentOffset
animated:(BOOL)animated
{
if (animated) {
[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setDuration:0.3];
[[self.contentView animator] setBoundsOrigin:contentOffset];
[NSAnimationContext endGrouping];
} else {
self.contentOffset = contentOffset;
}
}
#endif // TODO(macOS ISS#2323203)

- (void)setFrame:(CGRect)frame
{
// Preserving and revalidating `contentOffset`.
Expand Down Expand Up @@ -321,12 +337,12 @@ - (BOOL)resignFirstResponder
return YES;
}

- (void)setAccessibilityLabel:(NSString *)accessibilityLabel
- (void)setAccessibilityLabel:(NSString *)accessibilityLabel
{
[super setAccessibilityLabel:accessibilityLabel];
[[self documentView] setAccessibilityLabel:accessibilityLabel];
}
- (void)setDocumentView:(__kindof NSView *)documentView
- (void)setDocumentView:(__kindof NSView *)documentView
{
[super setDocumentView:documentView];
[documentView setAccessibilityLabel:[self accessibilityLabel]];
Expand Down Expand Up @@ -457,7 +473,7 @@ - (BOOL)resignFirstResponder
return [_scrollView resignFirstResponder];
}

- (void)setAccessibilityLabel:(NSString *)accessibilityLabel
- (void)setAccessibilityLabel:(NSString *)accessibilityLabel
{
[_scrollView setAccessibilityLabel:accessibilityLabel];
}
Expand Down Expand Up @@ -494,7 +510,7 @@ - (void)insertReactSubview:(RCTUIView *)view atIndex:(NSInteger)atIndex // TODO(
[super insertReactSubview:view atIndex:atIndex];
#if TARGET_OS_OSX // [TODO(macOS ISS#2323203)
RCTAssert(self.contentView == nil, @"RCTScrollView may only contain a single subview");

_scrollView.documentView = view;
#else // ]TODO(macOS ISS#2323203)
#if !TARGET_OS_TV
Expand Down Expand Up @@ -714,19 +730,16 @@ - (void)scrollToOffset:(CGPoint)offset animated:(BOOL)animated
0.01)); // Make width and height greater than 0
// Ensure at least one scroll event will fire
_allowNextScrollNoMatterWhat = YES;
#if TARGET_OS_OSX // [TODO(macOS ISS#2323203)
(void) animated;
_scrollView.contentOffset = offset;
#else // ]TODO(macOS ISS#2323203)

if (!CGRectContainsPoint(maxRect, offset) && !self.scrollToOverflowEnabled) {
CGFloat x = fmax(offset.x, CGRectGetMinX(maxRect));
x = fmin(x, CGRectGetMaxX(maxRect));
CGFloat y = fmax(offset.y, CGRectGetMinY(maxRect));
y = fmin(y, CGRectGetMaxY(maxRect));
offset = CGPointMake(x, y);
}

[_scrollView setContentOffset:offset animated:animated];
#endif // TODO(macOS ISS#2323203)
}
}

Expand All @@ -753,12 +766,7 @@ - (void)scrollToEnd:(BOOL)animated
if (!CGPointEqualToPoint(_scrollView.contentOffset, offset)) {
// Ensure at least one scroll event will fire
_allowNextScrollNoMatterWhat = YES;
#if TARGET_OS_OSX // [TODO(macOS ISS#2323203)
(void) animated;
_scrollView.contentOffset = offset;
#else // ]TODO(macOS ISS#2323203)
[_scrollView setContentOffset:offset animated:animated];
#endif // TODO(macOS ISS#2323203)
}
}

Expand Down Expand Up @@ -787,7 +795,7 @@ - (void)flashScrollIndicators
#endif
}
// ]TODO(macOS ISS#2323203)

#pragma mark - ScrollView delegate

#if TARGET_OS_OSX // [TODO(macOS ISS#2323203)
Expand Down Expand Up @@ -1277,22 +1285,22 @@ - (NSString*)keyCommandFromKeyCode:(NSInteger)keyCode
{
case 36:
return @"ENTER";

case 116:
return @"PAGE_UP";

case 121:
return @"PAGE_DOWN";

case 123:
return @"LEFT_ARROW";

case 124:
return @"RIGHT_ARROW";

case 125:
return @"DOWN_ARROW";

case 126:
return @"UP_ARROW";
}
Expand All @@ -1308,7 +1316,7 @@ - (void)keyDown:(UIEvent*)theEvent
RCT_SEND_SCROLL_EVENT(onScrollKeyDown, (@{ @"key": keyCommand}));
} else {
[super keyDown:theEvent];

// AX: if a tab key was pressed and the first responder is currently clipped by the scroll view,
// automatically scroll to make the view visible to make it navigable via keyboard.
if ([theEvent keyCode] == 48) { //tab key
Expand Down

0 comments on commit f37f72e

Please sign in to comment.