Last active
November 6, 2016 17:15
-
-
Save kreeger/4612414 to your computer and use it in GitHub Desktop.
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. Attempts to be as similar in fashion as possible to the interface set forth in SVPullToRefresh, with a couple of minor exceptions.
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
It crash the app when it try to dealloc the UIViewControll that contain it some times, it happens to you too?