Skip to content
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

scroll content with animation #725

Merged
merged 1 commit into from
Feb 18, 2021
Merged
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
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