SwipyCell

Awesome Build Status Swift Copatibility CocoaPods Compatible Platform GitHub license Twitter

受流行的 Mailbox App 启发的可滑动 UITableViewCell,使用 Swift 实现。

预览

退出模式

.exit 模式是原始行为,类似于 Mailbox app。

切换模式

.toggle 是另一种行为,单元格在滑动后会弹回。

安装

Swift Package Manager (推荐)

Swift Package Manger 是 Apple 官方用于管理源代码分发的工具,旨在方便地共享您的代码和重用他人的代码。

要在 SwiftPM 项目中使用 SwipyCell 库,请将以下行添加到您的 Package.swift 文件中的依赖项中

.package(url: "https://github.com/moritzsternemann/SwipyCell", .upToNextMinor(from: "4.1.0")),

CocoaPods

CocoaPods 是 Cocoa 项目的依赖项管理器。

要使用 CocoaPods 将 SwipyCell 集成到您的项目中,请将其添加到您的 Podfile

pod 'SwipyCell', '~> 4.1'

Carthage

Carthage 是一个去中心化的依赖项管理器,它可以自动执行将框架添加到您的 Cocoa 应用程序的过程。

要使用 Carthage 将 SwipyCell 集成到您的项目中,请将其添加到您的 Cartfile

github "moritzsternemann/SwipyCell" >= 4.1

用法

示例

完整的示例在 Example 目录中可用。以下代码是一个非常基本的示例

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! SwipyCell
    cell.selectionStyle = .gray
    cell.contentView.backgroundColor = UIColor.white

    let checkView = viewWithImageName("check")
    let greenColor = UIColor(red: 85.0 / 255.0, green: 213.0 / 255.0, blue: 80.0 / 255.0, alpha: 1.0)

    let crossView = viewWithImageName("cross")
    let redColor = UIColor(red: 232.0 / 255.0, green: 61.0 / 255.0, blue: 14.0 / 255.0, alpha: 1.0)

    let clockView = viewWithImageName("clock")
    let yellowColor = UIColor(red: 254.0 / 255.0, green: 217.0 / 255.0, blue: 56.0 / 255.0, alpha: 1.0)

    let listView = viewWithImageName("list")
    let brownColor = UIColor(red: 206.0 / 255.0, green: 149.0 / 255.0, blue: 98.0 / 255.0, alpha: 1.0)

    cell.defaultColor = tableView.backgroundView?.backgroundColor
    cell.delegate = self

    cell.textLabel?.text = "Switch Mode Cell"
    cell.detailTextLabel?.text = "Swipe to switch"

    cell.addSwipeTrigger(forState: .state(0, .left), withMode: .toggle, swipeView: checkView, swipeColor: greenColor, completion: { cell, trigger, state, mode in
        print("Did swipe \"Checkmark\" cell")
    })

    cell.addSwipeTrigger(forState: .state(1, .left), withMode: .toggle, swipeView: crossView, swipeColor: redColor, completion: { cell, trigger, state, mode in
        print("Did swipe \"Cross\" cell")
    })

    cell.addSwipeTrigger(forState: .state(0, .right), withMode: .toggle, swipeView: clockView, swipeColor: yellowColor, completion: { cell, trigger, state, mode in
        print("Did swipe \"Clock\" cell")
    })

    cell.addSwipeTrigger(forState: .state(1, .right), withMode: .toggle, swipeView: listView, swipeColor: brownColor, completion: { cell, trigger, state, mode in
        print("Did swipe \"List\" cell")
    })

    return cell
}

SwipyCellState

SwipyCellState 表示滑动状态,例如单元格左侧的第一个状态。
可能的值有

SwipyCellMode

如上所示的 SwipyCellMode。

SwipyCellTriggerBlock

SwipyCellTriggerBlock 是以下类型的别名

(SwipyCell, SwipyCellTrigger, SwipyCellState, SwipyCellMode) -> Void

向单元格添加滑动触发器

使用此方法可以轻松地向单元格添加滑动触发器

func addSwipeTrigger(forState: SwipyCellState, withMode: SwipyCellMode, swipeView: UIView, swipeColor: UIColor, completion: SwipyCellTriggerBlock)

Delegate

SwipyCell 提供了三个委托方法,用于跟踪用户的行为。

// When the user starts swiping the cell this method is called
func swipyCellDidStartSwiping(_ cell: SwipyCell)

// When the user ends swiping the cell this method is called
func swipyCellDidFinishSwiping(_ cell: SwipyCell, atState state: SwipyCellState, triggerActivated activated: Bool)

// When the user is dragging, this method is called with the percentage from the border
func swipyCell(_ cell: SwipyCell, didSwipeWithPercentage percentage: CGFloat, currentState state: SwipyCellState, triggerActivated activated: Bool)

配置

所有可配置选项都在 SwipyCellConfig.shared 单例对象中定义。每个新单元格都将这些选项设置为默认值。要更改默认值,只需更改 SwipyCellConfig 单例对象的变量。

触发点

触发点在配置单例或每个单元格的 triggerPoints<CGFloat, SwipyCellState> 字典中定义。
每个键标记触发点的滑动百分比;对应的值是一个标识符,用于稍后引用触发点。负键标记单元格右侧的点(向左滑动),正键标记单元格左侧的点(向右滑动)。
要修改触发点,每个单元格以及配置单例上都有一些可用的方法

// Set a new trigger point for the given state
func setTriggerPoint(forState state: SwipyCellState, at point: CGFloat)

// Set a new trigger point for the given index on BOTH sides of the cell
func setTriggerPoint(forIndex index: Int, at point: CGFloat)

// Overwrite all existing trigger points with the given new ones
func setTriggerPoints(_ points: [CGFloat: SwipyCellState])
// The Integer parameter is the index for BOTH sides of the cell
func setTriggerPoints(_ points: [CGFloat: Int])

// Overwrite all existing trigger points with new ones in order of the array on BOTH sides
func setTriggerPoints(points: [CGFloat])

// Get all existing trigger points
func getTriggerPoints() -> [CGFloat: SwipyCellState]

// Clear all existing trigger points
func clearTriggerPoints()

默认值:每侧 25% 和 75%

swipeViewPadding

var swipeViewPadding: CGFloat

swipeViewPadding 是滑动视图与单元格外边缘之间的填充。

默认值:24.0

shouldAnimateSwipeViews

var shouldAnimateSwipeViews: Bool

shouldAnimateSwipeViews 设置滑动视图是否应在滑动时随单元格移动,还是停留在外边缘。

默认值:true

defaultSwipeViewColor

var defaultSwipeViewColor: UIColor

defaultSwipeViewColor 是当前状态为 .none 时滑动的颜色。

默认值:UIColor.white

重置单元格位置

当使用 .exit 模式时,您可以使用 swipeToOrigin(_:) 方法将单元格动画返回到其默认位置。如果您的应用程序要求用户确认,而用户想要取消操作,这将非常有用。

cell.swipeToOrigin {
    print("Swiped back")
}

作者

我是 Moritz Sternemann,慕尼黑 工业大学 的计算机科学学生。

许可证

SwipyCell 在 MIT 许可证下可用。有关更多信息,请参见 LICENSE 文件。