测试已测试 | ✗ |
语言语言 | SwiftSwift |
许可 | MIT |
发布时间最新发布 | 2017年1月 |
SwiftSwift 版本 | 3.0 |
SPM支持 SPM | ✗ |
由DanisFabric维护。
Infinity
是一个用 Swift3.0 编写的简单易用库。它有以下优点
将下面的代码添加到你的Cartfile
,然后运行Carthage update
。
github "DanisFabric/Infinity"
导入 Infinity
import Infinity
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()
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
在UIViewController
的viewDidLoad
中removePullToRefresh
/removeInfiniteScroll
在UIViewController
的deinit
中 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
属性在UIViewController
上默认为true,这将影响Infinity
控制UIScrollView,因此添加下拉刷新时将自动设置为false。
tableView.automaticallyAdjustsScrollViewInsets = false
tableView.contentInset = UIEdgeInsets(top: 64, left: 0, bottom: 0, right: 0)
让我们看看添加/绑定操作的定义
// 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)?)
绑定操作的参数与添加操作的参数相同,以下为两者的区别
您只需确认以下协议之一,就可以创建完全受您控制的动画器。
public protocol CustomPullToRefreshAnimator {
func animateState(state: PullToRefreshState)
}
public protocol CustomInfinityScrollAnimator {
func animateState(state: InfinityScrollState)
}
让我们创建一个最简单的动画器,它只有一个标签来显示下拉刷新的状态。
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()
}
UIScrollView的一个布尔值,用于支持弹簧效果
tableView.supportSpringBounces = true
如果您使用Infinity
的app链接,我将很高兴。如果您有任何问题或建议,请发送电子邮件让我知道。
邮箱: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.