From 32fa650215a5be27dbfb4963b4e4d4933dc94fcd Mon Sep 17 00:00:00 2001 From: Martin Kovachev Date: Wed, 25 Nov 2015 19:59:48 +0200 Subject: [PATCH 1/2] Added a way to manually trigger the loading progress --- Class/CBStoreHouseRefreshControl.h | 11 ++++ Class/CBStoreHouseRefreshControl.m | 83 +++++++++++++++++++----------- 2 files changed, 63 insertions(+), 31 deletions(-) diff --git a/Class/CBStoreHouseRefreshControl.h b/Class/CBStoreHouseRefreshControl.h index 01692c9..e4fbdb0 100644 --- a/Class/CBStoreHouseRefreshControl.h +++ b/Class/CBStoreHouseRefreshControl.h @@ -8,8 +8,16 @@ #import +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 @@ -32,5 +40,8 @@ - (void)scrollViewDidEndDragging; - (void)finishingLoading; +- (void)finishingLoading:(BOOL)animated; + +- (void)triggerLoading; @end diff --git a/Class/CBStoreHouseRefreshControl.m b/Class/CBStoreHouseRefreshControl.m index 9a32d13..9b5f927 100644 --- a/Class/CBStoreHouseRefreshControl.m +++ b/Class/CBStoreHouseRefreshControl.m @@ -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"; @@ -150,30 +144,34 @@ - (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)); @@ -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; @@ -286,4 +302,9 @@ - (void)finishingLoading self.disappearProgress = 1; } +- (void)finishingLoading +{ + [self finishingLoading:YES]; +} + @end From 6888b1aedd71d3aa9a70c168a22c7eec5879a6f3 Mon Sep 17 00:00:00 2001 From: Martin Kovachev Date: Fri, 27 May 2016 14:56:20 +0300 Subject: [PATCH 2/2] Changes --- Class/CBStoreHouseRefreshControl.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Class/CBStoreHouseRefreshControl.m b/Class/CBStoreHouseRefreshControl.m index 9b5f927..480cbcd 100644 --- a/Class/CBStoreHouseRefreshControl.m +++ b/Class/CBStoreHouseRefreshControl.m @@ -174,7 +174,7 @@ - (void)startLoading { - (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