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

feat(ios): add method to create custom scrollview #2839

Merged
merged 1 commit into from
Jan 5, 2023
Merged
Show file tree
Hide file tree
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
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