1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
__*Upgrading from an earlier version?*: On October 25th, a complete and overdue refactor of this class was pushed that fixes memory issues as well as adds new feature people have been asking for a long time. I tried my best to make this a seamless transition (you will get some deprecation warnings).__
__*Important note if your project doesn't use ARC*: you must add the @-fobjc-arc@ compiler flag to @UIScrollView+SVPullToRefresh.m@ and @UIScrollView+SVInfiniteScrolling.m@ in Target Settings > Build Phases > Compile Sources.__
h1. SVPullToRefresh + SVInfiniteScrolling
These UIScrollView categories makes it super easy to add pull-to-refresh and infinite scrolling fonctionalities to any UIScrollView (or any of its subclass). Instead of depending on delegates and/or subclassing @UIViewController@, SVPullToRefresh uses the Objective-C runtime to add the following 2 methods:
<pre>
- (void)addPullToRefreshWithActionHandler:(void (^)(void))actionHandler;
- (void)addInfiniteScrollingWithActionHandler:(void (^)(void))actionHandler;
</pre>
h2. Installation
* Drag the @SVPullToRefresh/SVPullToRefresh@ folder into your project.
* Add the *QuartzCore* framework to your project.
* Import @UIScrollView+SVPullToRefresh.h@ and/or @UIScrollView+SVInfiniteScrolling.m@
h2. Usage
(see sample Xcode project in @/Demo@)
h3. Adding Pull to Refresh
<pre>
[tableView addPullToRefreshWithActionHandler:^{
// prepend data to dataSource, insert cells at top of table view
// call [tableView.pullToRefreshView stopAnimating] when done
}];
</pre>
If you’d like to programmatically trigger the refresh (for instance in viewDidLoad), you can do so with:
<pre>
[tableView triggerPullToRefresh];
</pre>
You can temporarily hide the pull to refresh view by setting the @showsPullToRefresh@ property:
<pre>
tableView.showsPullToRefresh = NO;
</pre>
h4. Customization
The pull to refresh view can be customized using the following properties/methods:
<pre>
@property (nonatomic, strong) UIColor *arrowColor;
@property (nonatomic, strong) UIColor *textColor;
@property (nonatomic, readwrite) UIActivityIndicatorViewStyle activityIndicatorViewStyle;
- (void)setTitle:(NSString *)title forState:(SVPullToRefreshState)state;
- (void)setSubtitle:(NSString *)subtitle forState:(SVPullToRefreshState)state;
- (void)setCustomView:(UIView *)view forState:(SVPullToRefreshState)state;
</pre>
You can access these properties through your scroll view's @pullToRefreshView@ property.
For instance, you would set the @arrowColor@ property using:
<pre>
tableView.pullToRefreshView.arrowColor = [UIColor whiteColor];
</pre>
h3. Adding Infinite Scrolling
<pre>
[tableView addInfiniteScrollingWithActionHandler:^{
// append data to data source, insert new cells at the end of table view
}];
</pre>
If you’d like to programmatically trigger the refresh (for instance in viewDidLoad), you can do so with:
<pre>
[tableView triggerInfiniteScrolling];
</pre>
You can temporarily hide the infinite scrolling view by setting the @showsInfiniteScrolling@ property:
<pre>
tableView.showsInfiniteScrolling = NO;
</pre>
If you'd like to keep the infinite scrolling view around but disable the action handler and state changing:
<pre>
tableView.infiniteScrollingView.enabled = NO;
</pre>
When disabled, the `state` property will always return `SVInfiniteScrollingStateStopped`.
h4. Customization
The infinite scrolling view can be customized using the following properties/methods:
<pre>
@property (nonatomic, readwrite) UIActivityIndicatorViewStyle activityIndicatorViewStyle;
- (void)setCustomView:(UIView *)view forState:(SVInfiniteScrollingState)state;
</pre>
You can access these properties through your scroll view's @infiniteScrollingView@ property.
h2. Under the hood
SVPullToRefresh extends @UIScrollView@ by adding new public methods as well as a dynamic properties (thanks "@seb_morel":http://twitter.com/seb_morel!). It uses key-value observing to track the scrollView's @contentOffset@, which removes the need for the view to be linked to the @UIScrollViewDelegate@ protocol.
h2. Credits
SVPullToRefresh is brought to you by "Sam Vermette":http://samvermette.com and "contributors to the project":https://github.com/samvermette/SVPullToRefresh/contributors. If you have feature suggestions or bug reports, feel free to help out by sending pull requests or by "creating new issues":https://github.com/samvermette/SVPullToRefresh/issues/new. If you're using SVPullToRefresh in your project, attribution would be nice.
Big thanks to "@seb_morel":http://twitter.com/seb_morel for his "Demistifying the Objective-C runtime":http://cocoaheadsmtl.s3.amazonaws.com/demistifying-runtime.pdf talk, which permitted the level of abstraction found in SVPullToRefresh.
Hat tip to "Loren Brichter":http://twitter.com/lorenb for inventing such a great UI mechanism.
|