Forked from kreeger/UIScrollView+PullToRefresh.m
Last active
August 29, 2015 14:11
-
-
Save danielgomezrico/72502c5510a75ce51b08 to your computer and use it in GitHub Desktop.
Category to add pull to refresh to any UIScrollView
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// UIScrollView+PullToRefresh.h | |
// Created by Ben Kreeger (@kreeger), 2013/01/23. | |
// | |
#import <UIKit/UIKit.h> | |
/** Adds pull to refresh for a UIScrollView (or a UITableView). Abstracts away differences in frameworks between iOS 6 | |
* and iOS 5 (in the case of the latter, `ODRefreshControl` is used. | |
*/ | |
@interface UIScrollView (PullToRefresh) | |
@property (strong, nonatomic) UIControl *pullToRefreshView; | |
@property (strong, nonatomic) void (^pullToRefreshBlock)(void); | |
@property (readonly) BOOL refreshing; | |
/** Sets up the scroll view with a pull-to-refresh view, and saves the callback block for when the view is triggered. | |
* This sets up the default color of the pull-to-refresh view to be `grayColor`. | |
* @param block the block to be called when the scroll view is pulled on. | |
*/ | |
- (void)addPullToRefreshWithActionHandler:(void (^)(void))block; | |
/** Sets up the scroll view with a pull-to-refresh view, and saves the callback block for when the view is triggered. | |
* Also allows for a tint color to be saved on the pull-to-refresh view. | |
* @param block the block to be called when the scroll view is pulled on. | |
* @param tintColor the tint color to use on the pull-to-refresh view. | |
*/ | |
- (void)addPullToRefreshWithActionHandler:(void (^)(void))block tintColor:(UIColor *)tintColor; | |
/** Tells the pull-to-refresh view to begin refreshing. | |
*/ | |
- (void)beginRefreshing; | |
/** Tells the pull-to-refresh view to end refreshing. | |
*/ | |
- (void)endRefreshing; | |
/** Calls the saved pull-to-refresh block. | |
*/ | |
- (void)triggerPullToRefresh; | |
@end | |
// | |
// UIScrollView+PullToRefresh.m | |
// Created by Ben Kreeger (@kreeger), 2013/01/23. | |
// | |
#import "UIScrollView+PullToRefresh.h" | |
#import <objc/runtime.h> | |
#import <Availability.h> | |
#if defined(__IPHONE_5_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_5_0 | |
#import <ODRefreshControl/ODRefreshControl.h> | |
#endif | |
static const void *PullToRefreshViewKey = &PullToRefreshViewKey; | |
static const void *PullToRefreshBlockKey = &PullToRefreshBlockKey; | |
@implementation UIScrollView (PullToRefresh) | |
@dynamic pullToRefreshView, pullToRefreshBlock; | |
- (void)addPullToRefreshWithActionHandler:(void (^)(void))block { | |
[self addPullToRefreshWithActionHandler:block tintColor:[UIColor grayColor]]; | |
} | |
- (void)addPullToRefreshWithActionHandler:(void (^)(void))block tintColor:(UIColor *)tintColor { | |
if (NSClassFromString(@"UIRefreshControl")) { | |
self.pullToRefreshView = [[UIRefreshControl alloc] init]; | |
[self addSubview:self.pullToRefreshView]; | |
((UIRefreshControl *)self.pullToRefreshView).tintColor = tintColor; | |
} else { | |
self.pullToRefreshView = [[ODRefreshControl alloc] initInScrollView:self]; | |
((ODRefreshControl *)self.pullToRefreshView).tintColor = tintColor; | |
} | |
self.pullToRefreshBlock = block; | |
[self.pullToRefreshView addTarget:self action:@selector(triggerPullToRefresh) | |
forControlEvents:UIControlEventValueChanged]; | |
} | |
- (void)triggerPullToRefresh { | |
self.pullToRefreshBlock(); | |
} | |
- (void)beginRefreshing { | |
if (NSClassFromString(@"UIRefreshControl")) { | |
[(UIRefreshControl *)self.pullToRefreshView beginRefreshing]; | |
} else { | |
[(ODRefreshControl *)self.pullToRefreshView beginRefreshing]; | |
} | |
} | |
- (void)endRefreshing { | |
if (NSClassFromString(@"UIRefreshControl")) { | |
[(UIRefreshControl *)self.pullToRefreshView endRefreshing]; | |
} else { | |
[(ODRefreshControl *)self.pullToRefreshView endRefreshing]; | |
} | |
} | |
- (BOOL)refreshing { | |
if (NSClassFromString(@"UIRefreshControl")) { | |
return [(UIRefreshControl *)self.pullToRefreshView isRefreshing]; | |
} else { | |
return [(ODRefreshControl *)self.pullToRefreshView refreshing]; | |
} | |
} | |
- (UIControl *)pullToRefreshView { | |
return objc_getAssociatedObject(self, PullToRefreshViewKey); | |
} | |
- (void)setPullToRefreshView:(UIControl *)pullToRefreshView { | |
objc_setAssociatedObject(self, PullToRefreshViewKey, pullToRefreshView, OBJC_ASSOCIATION_RETAIN_NONATOMIC); | |
} | |
- (void (^)(void))pullToRefreshBlock { | |
return objc_getAssociatedObject(self, PullToRefreshBlockKey); | |
} | |
- (void)setPullToRefreshBlock:(void (^)(void))pullToRefreshBlock { | |
objc_setAssociatedObject(self, PullToRefreshBlockKey, pullToRefreshBlock, OBJC_ASSOCIATION_RETAIN_NONATOMIC); | |
} | |
@end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment