Delta 5.0.1

Delta 5.0.1

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布最后发布2018 年 8 月
Swift 版本Swift 版本3.0
SPM支持 SPM

Bjarne Mogstad 维护。



Delta 5.0.1

  • Bjarne Mogstad

Delta

Build Status CocoaPods Compatible Carthage Compatible

手动处理动画和跟踪更改可能会出错,容易出错,代码难以理解。Delta 通过使用动画让您轻松确保这一点,并让您的应用程序通过使用动画发光。您的视图最终将是数据的视觉表示。

使用方法

Delta 的独特功能之一是它能够确定变化。这是通过检查项的等价性和其标识符的等价性来完成的。如果标识符相同,但模型不再是可等价的,Delta 将生成一个更改记录,可用于重新加载或手动更新单元格。

基本

要使用 Delta,您的视图模型需要符合 DeltaItem 协议。

import Delta

struct TaskListItem: DeltaItem, Equatable {
  var deltaIdentifier: Int {
    return self.model.identifier
  }
  var model: Task
}

func ==(lhs: TaskListItem, rhs: TaskListItem) -> Bool {
  return lhs.model == rhs.model
}

然后我们可以找出我们的模型有序集合之间的差异。

let records = generateItemRecords(
  section: 0, 
  from: from, 
  to: to)

我们可以使用这些记录来对表格视图或集合视图进行动画处理

tableView.performUpdates(records)

分节数据结构

Delta还支持生成分节数据结构的记录。与项目类似,Delta需要分节符合协议;DeltaSection。每个分节都有一个标识符和一个按顺序排列的项目集。

struct TaskListSection: DeltaSection {
  var deltaIdentifier: Int
  var items: TaskListItem
}

确定更改并动画更改

let records = generateRecordsForSections(from: self.data, to: data)
self.data = data
collectionView.performUpdates(records)

更新回调

发生更改时的默认行为是重新加载单元格。这并不总是期望的行为,因此Delta允许您传递一个更新回调,该回调将在每个更改的单元格上调用。

collectionView.performUpdates(records, update: { old, new in 
  if let cell = self.collectionView.cellForItemAtIndexPath(old) as? MyCollectionViewCell {
    cell.task = data[new.section].item[new.item]
  }
})

注意:由于UITableView和UICollecitonView的内部实现,我们需要使用旧的索引路径查询单元格,并从新的索引路径更新其数据。

安装

Delta将与Swift的最新公开版本兼容。旧版本将会可用,但不会发布错误修复。

Carthage

  1. 在您的“Cartfile”中添加 github "mogstad/delta" ~> 3.0.0
  2. 运行 carthage update
  3. 将Delta链接到您的目标
  4. 将Delta添加到您的复制框架脚本阶段

CocoaPods

更新podfile

  1. use_frameworks!添加到podfile[^1]
  2. pod "Delta", "~> 3.0.0"添加到您的应用程序目标
  3. 通过运行pod install来更新依赖项

[^1]: Swift 无法作为静态库包含,因此需要在您的 podfile 中添加 use_frameworks!。这样它会将依赖项作为动态框架导入。

许可证

Delta 依据 MIT 许可证发布。详细信息请见 LICENSE 文件。