Git Product home page Git Product logo

pulltorefresh's Introduction

Customizable PullToRefresh

This component implements pure pull-to-refresh logic and you can use it for developing your own pull-to-refresh animations, like this one.

Swift 2.0

Currently we maintain a branch to provide support for Swift 2.0.

##Requirements

  • iOS 8.0+
  • Xcode 6.3
  • Swift 1.2

##Installing with CocoaPods

use_frameworks!
pod 'PullToRefresher', '~> 1.0'

##Usage

At first, import PullToRefresh:

import PullToRefresh

The easiest way to create PullToRefresh:

let refresher = PullToRefresh()

It will create a default pull-to-refresh with a simple view which has single UIActivitiIndicatorView. To add refresher to your UIScrollView subclass:

tableView.addPullToRefresh(refresher, action: {
    // action to be performed (pull data from some source)
})

After the action is completed and you want to hide the refresher:

tableView.endRefresing()

You can also start refreshing programmatically:

tableView.startRefreshing()

But you probably won’t use this component, though. UITableViewController and UICollectionViewController already have a simple type of refresher. It’s much more interesting to develop your own pull-to-refresh control.

##Creating custom PullToRefresh

To create a custom refresher you would need to initialize PullToRefresh class with two objects:

  • refreshView is a UIView object which will added to your scroll view;
  • animator is an object which will animate elements on refreshView depending on the state of PullToRefresh.
let awesomeRefrehser = PullToRefresh(refresherView: yourView, animator: yourAnimator)

###Steps for creating custom PullToRefresh

  1. Create a custom UIView with *.xib and add all images that you want to animate as subviews. Pin them with outlets:
class RefreshView: UIView {
    @IBOutlet
    private var imageView: UIImageView!
  
    // and others
}
  1. Create an Animator object that conforms RefreshViewAnimator protocol and can be initialized by your custom view:
class Animator: RefreshViewAnimator {
    private let refreshView: RefreshView
    
    init(refreshView: RefreshView) {
        self.refreshView = refreshView
    }

    func animateState(state: State) {
        // animate refreshView according to state
    }
}
  1. According to RefreshViewAnimator protocol, your animator should implement animateState method. This method is get called by PullToRefresh object every time its state is changed. There are four states:
enum State:Equatable {
    case Inital, Loading, Finished
    case Releasing(progress: double)
}
  • Initial - refresher is ready to be pulled.
  • Releasing - refresher is in the process of releasing (by a user or programmatically). This state contains a double value which represents releasing progress from 0 to 1.
  • Loading - refresher is in the loading state.
  • Finished - loading is finished.

Depending on the state that your animator gets from the PullToRefresh, it has to animate elements in refreshView:

func animateState(state: State) {
    switch state {
      case .Inital: // do inital layout for elements
      case .Releasing(let progress): // animate elements according to progress
      case .Loading: // start loading animations
      case .Finished: // show some finished state if needed
    }
}

Place the magic of animations insted of commented lines.

  1. For the convitience sake you can sublass from PullToRefresh and create separate class for your refresher:
class AwesomePullToRefresh: PullToRefresh {
    convenience init() {
        let refreshView =  NSBundle.mainBundle().loadNibNamed("RefreshView", owner: nil, options: nil).first as! RefreshView
        let animator =  Animator(refreshView: refreshView)
        self.init(refreshView: refreshView, animator: animator)
    }
}
  1. Finally, add a refresher to a UIScrollView subclass:
tableView.addPullToRefresh(refresher, action: {
    // action to be performed (pull data from some source)
})

Have fun! :)

License

The MIT License (MIT)

Copyright © 2015 Yalantis

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

pulltorefresh's People

Contributors

aeugene avatar daisuke310vvv avatar gorbannastya avatar jindulys avatar marvinnazari avatar maximletushov avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.