Infinity 3.0.2

Infinity 3.0.2

测试已测试
语言语言 SwiftSwift
许可 MIT
发布时间最新发布2017年1月
SwiftSwift 版本3.0
SPM支持 SPM

DanisFabric维护。



Infinity 3.0.2

  • DanisFabric

logo

Infinity 中文教程

介绍

Infinity 是一个用 Swift3.0 编写的简单易用库。它有以下优点

  1. 灵活性:你可以编写自己的动画。
  2. 易于使用:一行代码使 UIScrollView 支持下拉刷新或无限滚动

屏幕更多示例图片

screen1 screen1

要求

  • iOS 8.0+
  • Swift 3.0+

安装

Carthage

将下面的代码添加到你的Cartfile,然后运行Carthage update

github "DanisFabric/Infinity"

手动

  1. 下载示例项目
  2. 将Infinity文件夹中的文件添加到你的项目

用法

导入 Infinity

import Infinity

下拉刷新

向 UIScrollView 添加下拉刷新

  1. 创建一个动画器,显示下拉刷新的进度
  2. 将动画器添加到你的 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()

无限滚动

为UIScrollView添加无限滚动

  1. 创建控制器以显示无限滚动的状态
  2. 将动画器添加到你的 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/addInfiniteScrollUIViewControllerviewDidLoad
  • removePullToRefresh/removeInfiniteScrollUIViewControllerdeinit
    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

automaticallyAdjustsScrollViewInsets属性在UIViewController上默认为true,这将影响Infinity控制UIScrollView,因此添加下拉刷新时将自动设置为false。

tableView.automaticallyAdjustsScrollViewInsets = false
tableView.contentInset = UIEdgeInsets(top: 64, left: 0, bottom: 0, right: 0)

绑定 VS 添加

让我们看看添加/绑定操作的定义

// 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)?)

绑定操作的参数与添加操作的参数相同,以下为两者的区别

  • 添加操作将动画器添加到UIScrollView作为子视图
  • 绑定操作不对动画器进行操作,动画器仅接收下拉刷新/无限滚动的消息。这意味着您可以绑定任何对象到下拉刷新/无限滚动,并且您可以完全控制该对象。

自定义动画器

您只需确认以下协议之一,就可以创建完全受您控制的动画器。

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()
}

其他

supportSpringBounces

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.