From d1f7fc35e89534f6b0eb7bd16aff985532c85f12 Mon Sep 17 00:00:00 2001 From: liangyuxin Date: Sat, 11 Jan 2025 13:37:39 +0800 Subject: [PATCH] feat: add support for customizable scroll indicators in PdfView component - Added `showsHorizontalScrollIndicator` and `showsVerticalScrollIndicator` props to PdfView.js for better user control over scroll indicators. - Refactored RNPDFPdfView.mm to streamline the handling of scroll indicator visibility using a new `setScrollIndicators` method, improving code maintainability. --- PdfView.js | 4 +++ ios/RNPDFPdf/RNPDFPdfView.mm | 53 +++++++++++++----------------------- 2 files changed, 23 insertions(+), 34 deletions(-) diff --git a/PdfView.js b/PdfView.js index 277b4026..02b09c5f 100644 --- a/PdfView.js +++ b/PdfView.js @@ -40,6 +40,8 @@ export default class PdfView extends Component { singlePage: PropTypes.bool, onPageSingleTap: PropTypes.func, onScaleChanged: PropTypes.func, + showsHorizontalScrollIndicator: PropTypes.bool, + showsVerticalScrollIndicator: PropTypes.bool, }; static defaultProps = { @@ -62,6 +64,8 @@ export default class PdfView extends Component { }, onScaleChanged: (scale) => { }, + showsHorizontalScrollIndicator: true, + showsVerticalScrollIndicator: true, }; constructor(props) { diff --git a/ios/RNPDFPdf/RNPDFPdfView.mm b/ios/RNPDFPdf/RNPDFPdfView.mm index af94cb78..cddec766 100644 --- a/ios/RNPDFPdf/RNPDFPdfView.mm +++ b/ios/RNPDFPdf/RNPDFPdfView.mm @@ -479,40 +479,8 @@ - (void)didSetProps:(NSArray *)changedProps } } - if (_pdfDocument && ([changedProps containsObject:@"path"] || [changedProps containsObject:@"showsHorizontalScrollIndicator"])) { - if (_showsHorizontalScrollIndicator) { - for (UIView *subview in _pdfView.subviews) { - if ([subview isKindOfClass:[UIScrollView class]]) { - UIScrollView *scrollView = (UIScrollView *)subview; - scrollView.showsHorizontalScrollIndicator = YES; - } - } - } else { - for (UIView *subview in _pdfView.subviews) { - if ([subview isKindOfClass:[UIScrollView class]]) { - UIScrollView *scrollView = (UIScrollView *)subview; - scrollView.showsHorizontalScrollIndicator = NO; - } - } - } - } - - if (_pdfDocument && ([changedProps containsObject:@"path"] || [changedProps containsObject:@"showsVerticalScrollIndicator"])) { - if (_showsVerticalScrollIndicator) { - for (UIView *subview in _pdfView.subviews) { - if ([subview isKindOfClass:[UIScrollView class]]) { - UIScrollView *scrollView = (UIScrollView *)subview; - scrollView.showsVerticalScrollIndicator = YES; - } - } - } else { - for (UIView *subview in _pdfView.subviews) { - if ([subview isKindOfClass:[UIScrollView class]]) { - UIScrollView *scrollView = (UIScrollView *)subview; - scrollView.showsVerticalScrollIndicator = NO; - } - } - } + if (_pdfDocument && ([changedProps containsObject:@"path"] || [changedProps containsObject:@"showsHorizontalScrollIndicator"] || [changedProps containsObject:@"showsVerticalScrollIndicator"])) { + [self setScrollIndicators:self horizontal:_showsHorizontalScrollIndicator vertical:_showsVerticalScrollIndicator depth:0]; } if (_pdfDocument && ([changedProps containsObject:@"path"] || [changedProps containsObject:@"scrollEnabled"])) { @@ -928,6 +896,23 @@ - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecogni return !_singlePage; } +- (void)setScrollIndicators:(UIView *)view horizontal:(BOOL)horizontal vertical:(BOOL)vertical depth:(int)depth { + // max depth, prevent infinite loop + if (depth > 10) { + return; + } + + if ([view isKindOfClass:[UIScrollView class]]) { + UIScrollView *scrollView = (UIScrollView *)view; + scrollView.showsHorizontalScrollIndicator = horizontal; + scrollView.showsVerticalScrollIndicator = vertical; + } + + for (UIView *subview in view.subviews) { + [self setScrollIndicators:subview horizontal:horizontal vertical:vertical depth:depth + 1]; + } +} + @end #ifdef RCT_NEW_ARCH_ENABLED