alexanderedge / SVInfiniteScrolling

Infinite scrolling for UIScrollView in both up and down directions
MIT License
7 stars 3 forks source link

This is a fork of Sam Vermette's SVPullToRefresh repository that supports infinite scrolling in both directions. SVPullToRefresh category is not included in this repository.

If your project doesn't use ARC: you must add the -fobjc-arc compiler flag to UIScrollView+SVInfiniteScrolling.m in Target Settings > Build Phases > Compile Sources.

SVInfiniteScrolling

This UIScrollView category makes it super easy to add infinite scrolling functionality to any UIScrollView (or any of its subclass). Instead of relying on delegates and/or subclassing UIViewController, SVInfiniteScrolling uses the Objective-C runtime to add the following 2 methods to UIScrollView:

- (void)addInfiniteScrollingWithActionHandler:(void (^)(void))actionHandler forPosition:(SVInfiniteScrollingPositionTop)position;

Installation

Usage

(see sample Xcode project in /Demo)

Adding Infinite Scrolling

[tableView addInfiniteScrollingWithActionHandler:^{
    // append data to data source, insert new cells at the end of table view
    // call [tableView.infiniteScrollingView stopAnimating] when done
} forPosition:SVInfiniteScrollPositionTop];

If you’d like to programmatically trigger the loading (for instance in viewDidAppear:), you can do so with:

[tableView triggerInfiniteScrollingForPosition:SVInfiniteScrollPositionTop];

You can temporarily hide the infinite scrolling view by setting the showsInfiniteScrolling property:

[tableView setShowsInfiniteScrolling:NO forPosition:SVInfiniteScrollPositionTop];

Customization

The infinite scrolling view can be customized using the following methods:

- (void)setActivityIndicatorViewStyle:(UIActivityIndicatorViewStyle)activityIndicatorViewStyle;
- (void)setCustomView:(UIView *)view forState:(SVInfiniteScrollingState)state;

You can access these properties through your scroll view's infiniteScrollingView property.

Under the hood

SVInfiniteScrolling extends UIScrollView by adding new public methods as well as a dynamic properties.

It uses key-value observing to track the scrollView's contentOffset.

Credits

SVPullToRefresh is brought to you by Sam Vermette and contributors to the project. If you have feature suggestions or bug reports, feel free to help out by sending pull requests or by creating new issues. If you're using SVPullToRefresh in your project, attribution would be nice.

Big thanks to @seb_morel for his Demistifying the Objective-C runtime talk which really helped for this project.

Hat tip to Loren Brichter for inventing pull-to-refresh.