Infinity 中文教程
Infinity
is a simple to use library written in Swift2.1. there are some advantages:
- Flexibility: You can write your animations.
- Easy to use: One line code make UIScrollView support pull-to-refresh or infinity-scrolling
Screens More Sample Images
- iOS 8.0+
- Swift 2.0+
Add following to your Podfile
and run pod install
.
pod 'Infinity'
Add the following code to your Cartfile
and run Carthage update
.
github "DanisFabric/Infinity"
- Download the sample project
- add the files in Infinity folder to your project
Import Infinity
import Infinity
You need 2 steps to add pull-to-refresh to UIScrollView:
- create animator which to show the progress of pull-to-refresh
- add animator to your UIScrollView
let animator = DefaultRefreshAnimator(frame: CGRect(x: 0, y: 0, width: 24, height: 24))
self.tableView.addPullToRefresh(animator: animator, action: { [weak self] () -> Void in
// ...
self?.tableView?.endRefreshing() //stop refreshing
})
Removing pull-to-refresh is also simple:
tableView.removePullToRefresh()
If you want to stop refreshing:
tableView.endRefreshing()
If you want to start refreshing programmatically:
tableView.startRefreshing()
You need 2 steps to add infinite-scrolling to UIScrollView:
- create animator to show the state of infinity-scroll:
- add animator to your UIScrollView
let animator = DefaultInfiniteAnimator(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
self.tableView.addInfiniteScroll(animator: animator, action: { [weak self] () -> Void in
self?.tableView?.endInfiniteScrolling()
})
Remove infinite-scrolling:
tableView.removeInfiniteScroll()
If you want to stop refreshing:
tableView.endInfiniteScrolling()
If you want to start infinity-scrolling programmatically:
tableView.beginInfiniteScrolling()
addPullToRefresh
/addInfiniteScroll
inviewDidLoad
ofUIViewController
removePullToRefresh
/removeInfiniteScroll
indeinit
ofUIViewController
override func viewDidLoad() {
super.viewDidLoad()
let animator = DefaultRefreshAnimator(frame: CGRect(x: 0, y: 0, width: 24, height: 24))
self.tableView.addPullToRefresh(animator: animator, action: { [weak self] () -> Void in
self?.tableView?.endRefreshing()
})
let animator = DefaultInfiniteAnimator(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
self.tableView.addInfiniteScroll(animator: animator, action: { [weak self] () -> Void in
self?.tableView?.endInfiniteScrolling()
})
}
deinit {
self.tableView.removePullToRefresh()
self.tableView.removeInfiniteScroll()
}
automaticallyAdjustsScrollViewInsets
property on UIViewController which is by default to true bother the Infinity
control UIScrollView, so it will be automatically set to false when add pull-to-refresh.
You need to adjsut the contentInset of UIScrollView by your self. Infinity
offers some frequently used conentInset for you:
tableView.setInsetType(withTop: .NavigationBar, bottom: .None)
Let's see the definition of add/bind operations:
// PullToRefresh
public func addPullToRefresh(height: CGFloat = 60.0, animator: CustomPullToRefreshAnimator, action:(()->Void)?)
public func bindPullToRefresh(height: CGFloat = 60.0, toAnimator: CustomPullToRefreshAnimator, action:(()->Void)?)
//InfinityScroll
public func addInfinityScroll(height: CGFloat = 80.0, animator: CustomInfinityScrollAnimator, action: (() -> Void)?)
public func bindInfinityScroll(height: CGFloat = 80.0, toAnimator: CustomInfinityScrollAnimator, action: (() -> Void)?)
The parameters of bind operation is the same as parameters of add operation, following is the differences:
- add operation will add animator to UIScrollView as a subview
- bind operation don't do anything to animator, the animator just receive messages from pull-to-refresh/infinity-scrolling. It means you can bind any object to pull-to-refresh/infinity-scrolling, and you can control that object completely.
You just need to confirm one of following protocols to create your animator whose all behavior is under your control.
public protocol CustomPullToRefreshAnimator {
func animateState(state: PullToRefreshState)
}
public protocol CustomInfinityScrollAnimator {
func animateState(state: InfinityScrollState)
}
Let's create a most simple Animator which onlu has a label to show the state of pull-to-refresh.
class TextAnimator: UIView, CustomPullToRefreshAnimator {
var textLabel = UILabel()
override init(frame: CGRect) {
super.init(frame: frame)
textLabel.frame = self.bounds
self.addSubview(textLabel)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func animateState(state: PullToRefreshState) {
switch state {
case .None:
textLabel.text = "Pull Me To Refresh"
case .Releasing(let progress):
textLabel.text = String(progress)
case .Loading:
textLabel.text = "Loading ......."
}
}
}
// add TextAniamtor to UIScrollView
let animator = TextAnimator(frame: CGRect(x: 0, y: 0, width: 200, height: 24))
self.tableView.addPullToRefresh(animator: animator, action: { () -> Void in
// ...
self.tableView?.endRefreshing()
})
A bool value of UIScrollView to support spring effect
tableView.supportSpringBounces = true
I'd be happy if you sent me links to your apps where you use Infinity
. If you have any questions or suggestion, send me an email to let me know.
Email : DanisFabric
The MIT License (MIT)
Copyright © 2015 DanisFabric
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.