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

Moved the pull to refresh to a separate subclass #195

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Moved the pull to refresh to a separate subclass so that you can inhe…
…rit and custom your own pull to refresh view
  • Loading branch information
ipodishima committed Apr 23, 2014
commit c45e93077d19d44658d8b6d5dc9e24c85fe58abb
6 changes: 6 additions & 0 deletions Demo/SVPullToRefreshDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
22E0D94B1545F63300BB6BB5 /* README.md in Resources */ = {isa = PBXBuildFile; fileRef = 22E0D94A1545F63300BB6BB5 /* README.md */; };
22FDEC971639082E00DB53A8 /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = 22FDEC961639082E00DB53A8 /* [email protected] */; };
22FDEC9D16390CC800DB53A8 /* UIScrollView+SVInfiniteScrolling.m in Sources */ = {isa = PBXBuildFile; fileRef = 2288146016047C06005C6461 /* UIScrollView+SVInfiniteScrolling.m */; };
E0CC069D190801C700905FFD /* SVDemoPullToRefresh.m in Sources */ = {isa = PBXBuildFile; fileRef = E0CC069C190801C700905FFD /* SVDemoPullToRefresh.m */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -46,6 +47,8 @@
22E0D9401545EE9000BB6BB5 /* QuartzCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = QuartzCore.framework; path = System/Library/Frameworks/QuartzCore.framework; sourceTree = SDKROOT; };
22E0D94A1545F63300BB6BB5 /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = README.md; path = ../README.md; sourceTree = "<group>"; };
22FDEC961639082E00DB53A8 /* [email protected] */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = "[email protected]"; path = "../[email protected]"; sourceTree = "<group>"; };
E0CC069B190801C700905FFD /* SVDemoPullToRefresh.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SVDemoPullToRefresh.h; sourceTree = "<group>"; };
E0CC069C190801C700905FFD /* SVDemoPullToRefresh.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SVDemoPullToRefresh.m; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -102,6 +105,8 @@
22E0D92A1545EE5B00BB6BB5 /* SVViewController.h */,
22E0D92B1545EE5B00BB6BB5 /* SVViewController.m */,
22E0D92D1545EE5B00BB6BB5 /* SVViewController.xib */,
E0CC069B190801C700905FFD /* SVDemoPullToRefresh.h */,
E0CC069C190801C700905FFD /* SVDemoPullToRefresh.m */,
22E0D91F1545EE5B00BB6BB5 /* Supporting Files */,
);
name = Demo;
Expand Down Expand Up @@ -201,6 +206,7 @@
buildActionMask = 2147483647;
files = (
22E0D9251545EE5B00BB6BB5 /* main.m in Sources */,
E0CC069D190801C700905FFD /* SVDemoPullToRefresh.m in Sources */,
22E0D9291545EE5B00BB6BB5 /* SVAppDelegate.m in Sources */,
22E0D92C1545EE5B00BB6BB5 /* SVViewController.m in Sources */,
22E0D93D1545EE7600BB6BB5 /* UIScrollView+SVPullToRefresh.m in Sources */,
Expand Down
13 changes: 13 additions & 0 deletions Demo/SVPullToRefreshDemo/SVDemoPullToRefresh.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// SVDemoPullToRefresh.h
// SVPullToRefreshDemo
//
// Created by Marian Paul on 23/04/2014.
// Copyright (c) 2014 Home. All rights reserved.
//

#import "UIScrollView+SVPullToRefresh.h"

@interface SVDemoPullToRefresh : SVPullToRefreshView

@end
101 changes: 101 additions & 0 deletions Demo/SVPullToRefreshDemo/SVDemoPullToRefresh.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
//
// SVDemoPullToRefresh.m
// SVPullToRefreshDemo
//
// Created by Marian Paul on 23/04/2014.
// Copyright (c) 2014 Home. All rights reserved.
//

#import "SVDemoPullToRefresh.h"

const static CGFloat kDeltaScaleForPullToRefresh = 0.2f;

@implementation SVDemoPullToRefresh
{
UIView *_circleView;
UIActivityIndicatorView *_activity;
BOOL _triggered;
}

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
_circleView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 20.0f, 20.0f)];

UIBezierPath* ovalPath = [UIBezierPath bezierPathWithOvalInRect:_circleView.bounds];
CAShapeLayer *plane = [CAShapeLayer layer];
plane.fillColor = [UIColor redColor].CGColor;
plane.path = ovalPath.CGPath;
plane.frame = _circleView.bounds;
plane.anchorPoint = CGPointMake(0.5, 0.5);
plane.anchorPointZ = 0.5;

[_circleView.layer addSublayer:plane];

[self addSubview:_circleView];

_activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[_activity setHidesWhenStopped:YES];
[self addSubview:_activity];
}
return self;
}

- (void)handleNewState:(SVPullToRefreshState)state
{
switch (state) {
case SVPullToRefreshStateStopped:
[_activity stopAnimating];
_circleView.hidden = NO;
break;
case SVPullToRefreshStateTriggered:

break;
case SVPullToRefreshStateLoading:
{
if (!_triggered) {
_triggered = YES;
CGFloat duration = 0.6f;

[UIView animateWithDuration:duration
animations:^{
_circleView.center = CGPointMake(CGRectGetWidth(self.frame)/2.0f, CGRectGetHeight(self.frame) / 2.0f);
_circleView.transform = CGAffineTransformIdentity;
}
completion:^(BOOL finished) {
if (finished && self.state == SVPullToRefreshStateLoading)
{
[_activity setCenter:_circleView.center];
[_activity startAnimating];
_circleView.hidden = YES;
}
}];
}

}
break;
case SVPullToRefreshStateAll:

break;
default:
break;
}
}

- (void) updateForPercentage:(CGFloat)percentage
{
percentage = MAX(0.0f, percentage);

if (percentage == 0.0f) _triggered = NO;

if (_triggered) return;

CGFloat deltaScale = percentage * kDeltaScaleForPullToRefresh + 1.0f;

[_circleView setTransform:CGAffineTransformMakeScale(deltaScale, deltaScale)];
_circleView.center = CGPointMake(CGRectGetWidth(self.frame)/2.0f, CGRectGetHeight(self.frame) * (1.0f - percentage*0.3f) - 13.0f);
_activity.center = _circleView.center;
}

@end
3 changes: 2 additions & 1 deletion Demo/SVPullToRefreshDemo/SVViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#import "SVViewController.h"
#import "SVPullToRefresh.h"
#import "SVDemoPullToRefresh.h"

@interface SVViewController () <UITableViewDelegate, UITableViewDataSource>

Expand All @@ -25,7 +26,7 @@ - (void)viewDidLoad {
__weak SVViewController *weakSelf = self;

// setup pull-to-refresh
[self.tableView addPullToRefreshWithActionHandler:^{
[self.tableView addPullToRefresh:[SVDemoPullToRefresh class] withActionHandler:^{
[weakSelf insertRowAtTop];
}];

Expand Down
36 changes: 25 additions & 11 deletions SVPullToRefresh/UIScrollView+SVPullToRefresh.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ typedef NS_ENUM(NSUInteger, SVPullToRefreshPosition) {
SVPullToRefreshPositionBottom,
};

- (void)addPullToRefreshWithActionHandler:(void (^)(void))actionHandler;
- (void)addPullToRefreshWithActionHandler:(void (^)(void))actionHandler position:(SVPullToRefreshPosition)position;
- (void)addPullToRefreshWithActionHandler:(void (^)(void))actionHandler DEPRECATED_MSG_ATTRIBUTE("Use `addPullToRefresh:withActionHandler:` instead");
- (void)addPullToRefreshWithActionHandler:(void (^)(void))actionHandler position:(SVPullToRefreshPosition)position DEPRECATED_MSG_ATTRIBUTE("Use `addPullToRefresh:withActionHandler:position:` instead");

- (void)addPullToRefresh:(Class)pullToRefreshClass withActionHandler:(void (^)(void))actionHandler;
- (void)addPullToRefresh:(Class)pullToRefreshClass withActionHandler:(void (^)(void))actionHandler position:(SVPullToRefreshPosition)position;

- (void)triggerPullToRefresh;

@property (nonatomic, strong, readonly) SVPullToRefreshView *pullToRefreshView;
Expand All @@ -37,31 +41,41 @@ typedef NS_ENUM(NSUInteger, SVPullToRefreshState) {
SVPullToRefreshStateAll = 10
};

#pragma mark - The class to inherit from
@interface SVPullToRefreshView : UIView
@property (nonatomic, readonly) SVPullToRefreshState state;
@property (nonatomic, readonly) SVPullToRefreshPosition position;

- (void)startAnimating;
- (void)stopAnimating;

// To override if needed
- (void) handleNewState:(SVPullToRefreshState)state;
- (void) updateForPercentage:(CGFloat)percentage; // This is not bounded to 0.0f -> 1.0f by convenience

// deprecated; use [self.scrollView triggerPullToRefresh] instead
- (void)triggerRefresh DEPRECATED_ATTRIBUTE;

@end


#pragma mark - An example with the classic arrow

@interface SVArrowPullToRefreshView : SVPullToRefreshView
@property (nonatomic, strong) UIColor *arrowColor;
@property (nonatomic, strong) UIColor *textColor;
@property (nonatomic, strong, readonly) UILabel *titleLabel;
@property (nonatomic, strong, readonly) UILabel *subtitleLabel;
@property (nonatomic, strong, readwrite) UIColor *activityIndicatorViewColor NS_AVAILABLE_IOS(5_0);
@property (nonatomic, readwrite) UIActivityIndicatorViewStyle activityIndicatorViewStyle;

@property (nonatomic, readonly) SVPullToRefreshState state;
@property (nonatomic, readonly) SVPullToRefreshPosition position;

- (void)setTitle:(NSString *)title forState:(SVPullToRefreshState)state;
- (void)setSubtitle:(NSString *)subtitle forState:(SVPullToRefreshState)state;
- (void)setCustomView:(UIView *)view forState:(SVPullToRefreshState)state;

- (void)startAnimating;
- (void)stopAnimating;

// deprecated; use setSubtitle:forState: instead
@property (nonatomic, strong, readonly) UILabel *dateLabel DEPRECATED_ATTRIBUTE;
@property (nonatomic, strong) NSDate *lastUpdatedDate DEPRECATED_ATTRIBUTE;
@property (nonatomic, strong) NSDateFormatter *dateFormatter DEPRECATED_ATTRIBUTE;

// deprecated; use [self.scrollView triggerPullToRefresh] instead
- (void)triggerRefresh DEPRECATED_ATTRIBUTE;

@end
Loading