From 3163ca8a82c2b8eab32d4ae08770daf8d98ff125 Mon Sep 17 00:00:00 2001 From: luomy Date: Thu, 5 Jan 2023 15:53:58 +0800 Subject: [PATCH] feat(ios): add method to create custom scrollview (#2839) --- .../component/scrollview/HippyScrollView.h | 14 ++++++++++- .../component/scrollview/HippyScrollView.m | 23 +++++++++++-------- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/ios/sdk/component/scrollview/HippyScrollView.h b/ios/sdk/component/scrollview/HippyScrollView.h index b841fb01c5e..faf9ffecabb 100644 --- a/ios/sdk/component/scrollview/HippyScrollView.h +++ b/ios/sdk/component/scrollview/HippyScrollView.h @@ -29,10 +29,22 @@ @protocol UIScrollViewDelegate; +@interface HippyCustomScrollView : UIScrollView + +@property (nonatomic, assign) BOOL centerContent; + +@end + @interface HippyScrollView : HippyView - (instancetype)initWithEventDispatcher:(HippyEventDispatcher *)eventDispatcher NS_DESIGNATED_INITIALIZER; +/** + * This is where subclasses should create their custom scroll view hierarchy if they dont want to use default scroll view. + * Should never be called directly. + */ +- (HippyCustomScrollView *)loadScrollView; + /** * The `HippyScrollView` may have at most one single subview. This will ensure * that the scroll view's `contentSize` will be efficiently set to the size of @@ -49,7 +61,7 @@ @property (nonatomic, assign) CGSize contentSize; /** - * The underlying scrollView (TODO: can we remove this?) + * Get the underlying scrollview. */ @property (nonatomic, readonly) UIScrollView *scrollView; diff --git a/ios/sdk/component/scrollview/HippyScrollView.m b/ios/sdk/component/scrollview/HippyScrollView.m index 0770a9ae7b8..aff1cb9678a 100644 --- a/ios/sdk/component/scrollview/HippyScrollView.m +++ b/ios/sdk/component/scrollview/HippyScrollView.m @@ -33,12 +33,6 @@ #import "HippyI18nUtils.h" #import "objc/runtime.h" -@interface HippyCustomScrollView : UIScrollView - -@property (nonatomic, assign) BOOL centerContent; - -@end - @implementation HippyCustomScrollView - (instancetype)initWithFrame:(CGRect)frame { @@ -168,7 +162,7 @@ - (void)setContentOffset:(CGPoint)contentOffset { @end -@implementation HippyScrollView { +@interface HippyScrollView() { HippyCustomScrollView *_scrollView; UIView *_contentView; NSTimeInterval _lastScrollDispatchTime; @@ -184,13 +178,14 @@ @implementation HippyScrollView { __weak HippyRootView *_rootView; } +@end + +@implementation HippyScrollView + - (instancetype)initWithEventDispatcher:(HippyEventDispatcher *)eventDispatcher { HippyAssertParam(eventDispatcher); if ((self = [super initWithFrame:CGRectZero])) { - _scrollView = [[HippyCustomScrollView alloc] initWithFrame:CGRectZero]; - _scrollView.delegate = self; - _scrollView.delaysContentTouches = NO; _automaticallyAdjustContentInsets = YES; _contentInset = UIEdgeInsetsZero; _contentSize = CGSizeZero; @@ -202,6 +197,7 @@ - (instancetype)initWithEventDispatcher:(HippyEventDispatcher *)eventDispatcher _scrollListeners = [NSHashTable weakObjectsHashTable]; _contentOffsetCache = [NSMutableDictionary dictionaryWithCapacity:32]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveMemoryWarning) name:UIApplicationDidReceiveMemoryWarningNotification object:nil]; + _scrollView = [self loadScrollView]; [self addSubview:_scrollView]; if ([self needsLayoutForRTL]) { _scrollView.transform = CGAffineTransformMakeRotation(M_PI); @@ -210,6 +206,13 @@ - (instancetype)initWithEventDispatcher:(HippyEventDispatcher *)eventDispatcher return self; } +- (HippyCustomScrollView *)loadScrollView { + HippyCustomScrollView *scrollview = [[HippyCustomScrollView alloc] initWithFrame:CGRectZero]; + scrollview.delegate = self; + scrollview.delaysContentTouches = NO; + return scrollview; +} + - (void)didReceiveMemoryWarning { [_contentOffsetCache removeAllObjects]; }