Skip to content

Commit

Permalink
feat(ios): add method to create custom scrollview (#2839)
Browse files Browse the repository at this point in the history
  • Loading branch information
ozonelmy authored and zoomchan-cxj committed Feb 6, 2023
1 parent 5bb12ca commit e9fcbc4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
14 changes: 13 additions & 1 deletion ios/sdk/component/scrollview/HippyScrollView.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,22 @@

@protocol UIScrollViewDelegate;

@interface HippyCustomScrollView : UIScrollView <UIGestureRecognizerDelegate>

@property (nonatomic, assign) BOOL centerContent;

@end

@interface HippyScrollView : HippyView <UIScrollViewDelegate, HippyScrollableProtocol, HippyAutoInsetsProtocol, HippyInvalidating>

- (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
Expand All @@ -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;

Expand Down
23 changes: 13 additions & 10 deletions ios/sdk/component/scrollview/HippyScrollView.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@
#import "HippyI18nUtils.h"
#import "objc/runtime.h"

@interface HippyCustomScrollView : UIScrollView <UIGestureRecognizerDelegate>

@property (nonatomic, assign) BOOL centerContent;

@end

@implementation HippyCustomScrollView

- (instancetype)initWithFrame:(CGRect)frame {
Expand Down Expand Up @@ -168,7 +162,7 @@ - (void)setContentOffset:(CGPoint)contentOffset {

@end

@implementation HippyScrollView {
@interface HippyScrollView() {
HippyCustomScrollView *_scrollView;
UIView *_contentView;
NSTimeInterval _lastScrollDispatchTime;
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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];
}
Expand Down

0 comments on commit e9fcbc4

Please sign in to comment.