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

Added a way to manually trigger the loading progress #36

Open
wants to merge 2 commits 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
11 changes: 11 additions & 0 deletions Class/CBStoreHouseRefreshControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,16 @@

#import <UIKit/UIKit.h>

typedef enum {
CBStoreHouseRefreshControlStateIdle = 0,
CBStoreHouseRefreshControlStateRefreshing = 1,
CBStoreHouseRefreshControlStateDisappearing = 2
} CBStoreHouseRefreshControlState;

@interface CBStoreHouseRefreshControl : UIView

@property (nonatomic, readonly) CBStoreHouseRefreshControlState state;

+ (CBStoreHouseRefreshControl*)attachToScrollView:(UIScrollView *)scrollView
target:(id)target
refreshAction:(SEL)refreshAction
Expand All @@ -32,5 +40,8 @@
- (void)scrollViewDidEndDragging;

- (void)finishingLoading;
- (void)finishingLoading:(BOOL)animated;

- (void)triggerLoading;

@end
85 changes: 53 additions & 32 deletions Class/CBStoreHouseRefreshControl.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@
static const CGFloat kdisappearDuration = 1.2;
static const CGFloat krelativeHeightFactor = 2.f/5.f;

typedef enum {
CBStoreHouseRefreshControlStateIdle = 0,
CBStoreHouseRefreshControlStateRefreshing = 1,
CBStoreHouseRefreshControlStateDisappearing = 2
} CBStoreHouseRefreshControlState;

NSString *const startPointKey = @"startPoints";
NSString *const endPointKey = @"endPoints";
NSString *const xKey = @"x";
Expand Down Expand Up @@ -150,33 +144,37 @@ - (void)scrollViewDidEndDragging

if (self.state == CBStoreHouseRefreshControlStateRefreshing) {

UIEdgeInsets newInsets = self.scrollView.contentInset;
newInsets.top = self.originalTopContentInset + self.dropHeight;
CGPoint contentOffset = self.scrollView.contentOffset;

[UIView animateWithDuration:0 animations:^(void) {
self.scrollView.contentInset = newInsets;
self.scrollView.contentOffset = contentOffset;
}];

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"

if ([self.target respondsToSelector:self.action])
[self.target performSelector:self.action withObject:self];

#pragma clang diagnostic pop

[self startLoadingAnimation];
[self startLoading];
}
}
}

#pragma mark Private Methods

- (void)startLoading {
UIEdgeInsets newInsets = self.scrollView.contentInset;
newInsets.top = self.originalTopContentInset + self.dropHeight;
CGPoint contentOffset = self.scrollView.contentOffset;

[UIView animateWithDuration:0 animations:^(void) {
self.scrollView.contentInset = newInsets;
self.scrollView.contentOffset = contentOffset;
}];

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"

if ([self.target respondsToSelector:self.action])
[self.target performSelector:self.action withObject:self];

#pragma clang diagnostic pop

[self startLoadingAnimation];
}


- (CGFloat)animationProgress
{
return MIN(1.f, MAX(0, fabsf(self.realContentOffsetY)/self.dropHeight));
return MIN(1.f, MAX(0, fabs(self.realContentOffsetY)/self.dropHeight));
}

- (CGFloat)realContentOffsetY
Expand Down Expand Up @@ -263,19 +261,37 @@ - (void)updateDisappearAnimation

#pragma mark Public Methods

- (void)finishingLoading
- (void)triggerLoading {
//if (self.state == CBStoreHouseRefreshControlStateIdle || self.state == CBStoreHouseRefreshControlStateDisappearing) {
self.state = CBStoreHouseRefreshControlStateRefreshing;
self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x, -self.dropHeight);
[self updateBarItemsWithProgress:1];
[self startLoading];
//}
}

- (void)finishingLoading:(BOOL)animated
{
self.state = CBStoreHouseRefreshControlStateDisappearing;

UIEdgeInsets newInsets = self.scrollView.contentInset;
newInsets.top = self.originalTopContentInset;
[UIView animateWithDuration:kdisappearDuration animations:^(void) {
self.scrollView.contentInset = newInsets;
} completion:^(BOOL finished) {

if (animated) {
[UIView animateWithDuration:kdisappearDuration animations:^(void) {
self.scrollView.contentInset = newInsets;
} completion:^(BOOL finished) {
self.state = CBStoreHouseRefreshControlStateIdle;
[self.displayLink invalidate];
self.disappearProgress = 1;
}];
} else {
self.state = CBStoreHouseRefreshControlStateIdle;
[self.displayLink invalidate];
self.disappearProgress = 1;
}];

self.scrollView.contentInset = newInsets;
}

for (BarItem *barItem in self.barItems) {
[barItem.layer removeAllAnimations];
barItem.alpha = kbarDarkAlpha;
Expand All @@ -286,4 +302,9 @@ - (void)finishingLoading
self.disappearProgress = 1;
}

- (void)finishingLoading
{
[self finishingLoading:YES];
}

@end