Infinity 中文教程
Infinity
is a simple to use library written in Swift3.0. 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 3.0+
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
- 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))
tableView.fty.pullToRefresh.add(animator: animator) { [unowned self] in
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
print("end refreshing")
self.tableView.fty.pullToRefresh.end()
}
}
tableView.fty.pullToRefresh.end()
tableView.fty.pullToRefresh.remove()
tableView.fty.pullToRefresh.begin()
- 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))
tableView.fty.infiniteScroll.add(animator: animator) { [unowned self] in
DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {
self.tableView.fty.infiniteScroll.end()
}
}
tableView.fty.infiniteScroll.end()
tableView.fty.infiniteScroll.remove()
tableView.fty.infiniteScroll.begin()
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))
tableView.fty.pullToRefresh.add(animator: animator) { [unowned self] in
// 耗时操作(数据库读取,网络读取)
self.tableView.fty.pullToRefresh.end() // 调用此方法来停止刷新的动画
}
let animator = DefaultInfiniteAnimator(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
tableView.fty.infiniteScroll.add(animator: animator) { [unowned self] in
// 耗时操作(数据库读取,网络读取)
self.tableView.fty.infiniteScroll.end()
}
}
deinit {
tableView.fty.pullToRefresh.remove()
tableView.fty.infiniteScroll.remove()
// 或者使用下面这句代码,和上面代码效果相同
tableView.fty.clear()
}
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.
tableView.automaticallyAdjustsScrollViewInsets = false
tableView.contentInset = UIEdgeInsets(top: 64, left: 0, bottom: 0, right: 0)
Let's see the definition of add/bind operations:
// PullToRefresh
public func add(height: CGFloat = 60.0, animator: CustomPullToRefreshAnimator, action:(()->Void)?)
public func bind(height: CGFloat = 60.0, toAnimator: CustomPullToRefreshAnimator, action:(()->Void)?)
//InfinityScroll
public func add(height: CGFloat = 80.0, animator: CustomInfinityScrollAnimator, action: (() -> Void)?)
public func bind(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))
tableView.fty.pullToRefresh.add(animator: animator){ [unowned self] in
// 耗时操作(数据库读取,网络读取)
self.tableView.fty.pullToRefresh.end()
}
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.