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

horizontal support for infinite scrolling #203

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions SVPullToRefresh/UIScrollView+SVInfiniteScrolling.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
@interface UIScrollView (SVInfiniteScrolling)

- (void)addInfiniteScrollingWithActionHandler:(void (^)(void))actionHandler;
- (void)addHorizontalInfiniteScrollingWithActionHandler:(void (^)(void))actionHandler;
- (void)triggerInfiniteScrolling;

@property (nonatomic, strong, readonly) SVInfiniteScrollingView *infiniteScrollingView;
Expand Down
120 changes: 85 additions & 35 deletions SVPullToRefresh/UIScrollView+SVInfiniteScrolling.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@


static CGFloat const SVInfiniteScrollingViewHeight = 60;
static CGFloat const SVInfiniteScrollingViewWidth = SVInfiniteScrollingViewHeight;

@interface SVInfiniteScrollingDotView : UIView

Expand All @@ -29,9 +30,10 @@ @interface SVInfiniteScrollingView ()
@property (nonatomic, readwrite) SVInfiniteScrollingState state;
@property (nonatomic, strong) NSMutableArray *viewForState;
@property (nonatomic, weak) UIScrollView *scrollView;
@property (nonatomic, readwrite) CGFloat originalBottomInset;
@property (nonatomic, readwrite) CGFloat originalInset;
@property (nonatomic, assign) BOOL wasTriggeredByUser;
@property (nonatomic, assign) BOOL isObserving;
@property (nonatomic, assign) BOOL isHorizontal;

- (void)resetScrollViewContentInset;
- (void)setScrollViewContentInsetForInfiniteScrolling;
Expand Down Expand Up @@ -59,7 +61,22 @@ - (void)addInfiniteScrollingWithActionHandler:(void (^)(void))actionHandler {
view.scrollView = self;
[self addSubview:view];

view.originalBottomInset = self.contentInset.bottom;
view.originalInset = self.contentInset.bottom;
self.infiniteScrollingView = view;
self.showsInfiniteScrolling = YES;
}
}

- (void)addHorizontalInfiniteScrollingWithActionHandler:(void (^)(void))actionHandler {

if(!self.infiniteScrollingView) {
SVInfiniteScrollingView *view = [[SVInfiniteScrollingView alloc] initWithFrame:CGRectMake(self.contentSize.width, 0, SVInfiniteScrollingViewWidth, self.contentSize.height)];
view.infiniteScrollingHandler = actionHandler;
view.scrollView = self;
view.isHorizontal = YES;
[self addSubview:view];

view.originalInset = self.contentInset.right;
self.infiniteScrollingView = view;
self.showsInfiniteScrolling = YES;
}
Expand All @@ -86,23 +103,28 @@ - (void)setShowsInfiniteScrolling:(BOOL)showsInfiniteScrolling {
self.infiniteScrollingView.hidden = !showsInfiniteScrolling;

if(!showsInfiniteScrolling) {
if (self.infiniteScrollingView.isObserving) {
[self removeObserver:self.infiniteScrollingView forKeyPath:@"contentOffset"];
[self removeObserver:self.infiniteScrollingView forKeyPath:@"contentSize"];
[self.infiniteScrollingView resetScrollViewContentInset];
self.infiniteScrollingView.isObserving = NO;
}
if (self.infiniteScrollingView.isObserving) {
[self removeObserver:self.infiniteScrollingView forKeyPath:@"contentOffset"];
[self removeObserver:self.infiniteScrollingView forKeyPath:@"contentSize"];
[self.infiniteScrollingView resetScrollViewContentInset];
self.infiniteScrollingView.isObserving = NO;
}
}
else {
if (!self.infiniteScrollingView.isObserving) {
[self addObserver:self.infiniteScrollingView forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];
[self addObserver:self.infiniteScrollingView forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:nil];
[self.infiniteScrollingView setScrollViewContentInsetForInfiniteScrolling];
self.infiniteScrollingView.isObserving = YES;

[self.infiniteScrollingView setNeedsLayout];
self.infiniteScrollingView.frame = CGRectMake(0, self.contentSize.height, self.infiniteScrollingView.bounds.size.width, SVInfiniteScrollingViewHeight);
}
if (!self.infiniteScrollingView.isObserving) {
[self addObserver:self.infiniteScrollingView forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];
[self addObserver:self.infiniteScrollingView forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:nil];
[self.infiniteScrollingView setScrollViewContentInsetForInfiniteScrolling];
self.infiniteScrollingView.isObserving = YES;

[self.infiniteScrollingView setNeedsLayout];
if (self.infiniteScrollingView.isHorizontal) {
self.infiniteScrollingView.frame = CGRectMake(self.contentSize.width, 0, SVInfiniteScrollingViewWidth, self.contentSize.height);
}
else {
self.infiniteScrollingView.frame = CGRectMake(0, self.contentSize.height, self.infiniteScrollingView.bounds.size.width, SVInfiniteScrollingViewHeight);
}
}
}
}

Expand Down Expand Up @@ -143,11 +165,11 @@ - (void)willMoveToSuperview:(UIView *)newSuperview {
if (self.superview && newSuperview == nil) {
UIScrollView *scrollView = (UIScrollView *)self.superview;
if (scrollView.showsInfiniteScrolling) {
if (self.isObserving) {
[scrollView removeObserver:self forKeyPath:@"contentOffset"];
[scrollView removeObserver:self forKeyPath:@"contentSize"];
self.isObserving = NO;
}
if (self.isObserving) {
[scrollView removeObserver:self forKeyPath:@"contentOffset"];
[scrollView removeObserver:self forKeyPath:@"contentSize"];
self.isObserving = NO;
}
}
}
}
Expand All @@ -160,13 +182,23 @@ - (void)layoutSubviews {

- (void)resetScrollViewContentInset {
UIEdgeInsets currentInsets = self.scrollView.contentInset;
currentInsets.bottom = self.originalBottomInset;
if (self.isHorizontal) {
currentInsets.right = self.originalInset;
}
else {
currentInsets.bottom = self.originalInset;
}
[self setScrollViewContentInset:currentInsets];
}

- (void)setScrollViewContentInsetForInfiniteScrolling {
UIEdgeInsets currentInsets = self.scrollView.contentInset;
currentInsets.bottom = self.originalBottomInset + SVInfiniteScrollingViewHeight;
if (self.isHorizontal) {
currentInsets.right = self.originalInset + SVInfiniteScrollingViewWidth;
}
else {
currentInsets.bottom = self.originalInset + SVInfiniteScrollingViewHeight;
}
[self setScrollViewContentInset:currentInsets];
}

Expand All @@ -182,26 +214,44 @@ - (void)setScrollViewContentInset:(UIEdgeInsets)contentInset {

#pragma mark - Observing

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if([keyPath isEqualToString:@"contentOffset"])
[self scrollViewDidScroll:[[change valueForKey:NSKeyValueChangeNewKey] CGPointValue]];
else if([keyPath isEqualToString:@"contentSize"]) {
[self layoutSubviews];
self.frame = CGRectMake(0, self.scrollView.contentSize.height, self.bounds.size.width, SVInfiniteScrollingViewHeight);
if (self.isHorizontal) {
self.frame = CGRectMake(self.scrollView.contentSize.width, 0, SVInfiniteScrollingViewWidth, self.scrollView.contentSize.height);
}
else {
self.frame = CGRectMake(0, self.scrollView.contentSize.height, self.bounds.size.width, SVInfiniteScrollingViewHeight);
}
}
}

- (void)scrollViewDidScroll:(CGPoint)contentOffset {
if(self.state != SVInfiniteScrollingStateLoading && self.enabled) {
CGFloat scrollViewContentHeight = self.scrollView.contentSize.height;
CGFloat scrollOffsetThreshold = scrollViewContentHeight-self.scrollView.bounds.size.height;

if(!self.scrollView.isDragging && self.state == SVInfiniteScrollingStateTriggered)
self.state = SVInfiniteScrollingStateLoading;
else if(contentOffset.y > scrollOffsetThreshold && self.state == SVInfiniteScrollingStateStopped && self.scrollView.isDragging)
self.state = SVInfiniteScrollingStateTriggered;
else if(contentOffset.y < scrollOffsetThreshold && self.state != SVInfiniteScrollingStateStopped)
self.state = SVInfiniteScrollingStateStopped;
if (self.isHorizontal) {
CGFloat scrollViewContentWidth = self.scrollView.contentSize.width;
CGFloat scrollOffsetThreshold = scrollViewContentWidth - self.scrollView.bounds.size.width;

if(!self.scrollView.isDragging && self.state == SVInfiniteScrollingStateTriggered)
self.state = SVInfiniteScrollingStateLoading;
else if(contentOffset.x > scrollOffsetThreshold && self.state == SVInfiniteScrollingStateStopped && self.scrollView.isDragging)
self.state = SVInfiniteScrollingStateTriggered;
else if(contentOffset.x < scrollOffsetThreshold && self.state != SVInfiniteScrollingStateStopped)
self.state = SVInfiniteScrollingStateStopped;
}
else {
CGFloat scrollViewContentHeight = self.scrollView.contentSize.height;
CGFloat scrollOffsetThreshold = scrollViewContentHeight-self.scrollView.bounds.size.height;

if(!self.scrollView.isDragging && self.state == SVInfiniteScrollingStateTriggered)
self.state = SVInfiniteScrollingStateLoading;
else if(contentOffset.y > scrollOffsetThreshold && self.state == SVInfiniteScrollingStateStopped && self.scrollView.isDragging)
self.state = SVInfiniteScrollingStateTriggered;
else if(contentOffset.y < scrollOffsetThreshold && self.state != SVInfiniteScrollingStateStopped)
self.state = SVInfiniteScrollingStateStopped;
}
}
}

Expand Down