About DGElasticPullToRefresh
DGElasticPullToRefresh is a flexible and customizable pull-to-refresh library for iOS. It provides an elastic animation while pulling down the scrollable content, similar to the popular pull-to-refresh gesture used in many iOS apps.
Features
- Customizable pull-to-refresh animation
- Elastic animation effect while pulling down the content
- Supports UITableView, UICollectionView, UIScrollView, and other scrollable views
- Easy integration and usage
- Supports both Objective-C and Swift
- Open-source and available on GitHub
Installation
To install DGElasticPullToRefresh, you have several options:
- Manually: Download the library from GitHub, drag and drop DGElasticPullToRefresh folder into your Xcode project.
- CocoaPods: Add the following line to your Podfile:
pod 'DGElasticPullToRefresh'
- Carthage: Add the following line to your Cartfile:
github "rsrbk/DGElasticPullToRefresh"
- Swift Package Manager: Add the following package dependency in your Xcode project:
.package(url: "https://github.com/rsrbk/DGElasticPullToRefresh.git", from: "1.2.0")
Usage
Using DGElasticPullToRefresh is easy and straightforward. Here are the general steps:
- Import DGElasticPullToRefresh module in your view controller.
- Configure the pull-to-refresh animation with desired options.
- Assign the pull-to-refresh action.
- Disable or remove the pull-to-refresh effect when it’s no longer needed.
Code Example
import DGElasticPullToRefresh
class MyViewController: UIViewController {
var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Step 1: Import DGElasticPullToRefresh module
import DGElasticPullToRefresh
// Step 2: Configure the pull-to-refresh animation
let loadingView = DGElasticPullToRefreshLoadingViewCircle()
loadingView.tintColor = UIColor(red: 0, green: 0.5, blue: 1, alpha: 1)
tableView.dg_addPullToRefreshWithActionHandler({ [weak self] () -> Void in
// Placeholder pull-to-refresh action
// Simulate a delay for loading data
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
// Stop pull-to-refresh loading animation
self?.tableView.dg_stopLoading()
}
}, loadingView: loadingView)
// Step 3: Assign the pull-to-refresh action
tableView.dg_setPullToRefreshFillColor(UIColor(red: 0.9, green: 0.9, blue: 0.9, alpha: 1))
tableView.dg_setPullToRefreshBackgroundColor(tableView.backgroundColor!)
// Step 4: Disable or remove the pull-to-refresh effect (optional)
// tableView.dg_removePullToRefresh() or tableView.dg_disablePullToRefresh()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// Step 4: Trigger the pull-to-refresh action after view appears (optional)
tableView.dg_startLoading()
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
// Step 4: Remove the pull-to-refresh effect after view disappears (optional)
tableView.dg_removePullToRefresh()
}
}
Customization
DGElasticPullToRefresh allows you to customize various aspects of the pull-to-refresh animation. Some of the customization options include:
- Change the loading view (e.g., Circle, Arc, Snake, etc.)
- Customize the loading view’s appearance (e.g., color, size, etc.)
- Change the fill color and background color of the pull-to-refresh effect
- Customize the horizontal offset and vertical tension of the elastic animation
- And more…
For detailed customization instructions, please refer to the official documentation on the GitHub repository.
Contributing
DGElasticPullToRefresh is an open-source project hosted on GitHub. You are welcome to contribute by submitting bug reports, feature requests, or pull requests to the repository. Your contribution will be greatly appreciated!
License
DGElasticPullToRefresh is released under the MIT License. See the license file for more details.