123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- //
- // SwipeTableViewCell.swift
- //
- // Created by Jeremy Koch
- // Copyright © 2017 Jeremy Koch. All rights reserved.
- //
- import UIKit
- /**
- The `SwipeTableViewCell` class extends `UITableViewCell` and provides more flexible options for cell swiping behavior.
-
-
- The default behavior closely matches the stock Mail.app. If you want to customize the transition style (ie. how the action buttons are exposed), or the expansion style (the behavior when the row is swiped passes a defined threshold), you can return the appropriately configured `SwipeOptions` via the `SwipeTableViewCellDelegate` delegate.
- */
- open class SwipeTableViewCell: UITableViewCell {
-
- /// The object that acts as the delegate of the `SwipeTableViewCell`.
- public weak var delegate: SwipeTableViewCellDelegate?
-
- var state = SwipeState.center
- var actionsView: SwipeActionsView?
- var scrollView: UIScrollView? {
- return tableView
- }
- var indexPath: IndexPath? {
- return tableView?.indexPath(for: self)
- }
- var panGestureRecognizer: UIGestureRecognizer
- {
- return swipeController.panGestureRecognizer;
- }
-
- var swipeController: SwipeController!
- var isPreviouslySelected = false
-
- weak var tableView: UITableView?
-
- /// :nodoc:
- open override var frame: CGRect {
- set { super.frame = state.isActive ? CGRect(origin: CGPoint(x: frame.minX, y: newValue.minY), size: newValue.size) : newValue }
- get { return super.frame }
- }
-
- /// :nodoc:
- open override var layoutMargins: UIEdgeInsets {
- get {
- return frame.origin.x != 0 ? swipeController.originalLayoutMargins : super.layoutMargins
- }
- set {
- super.layoutMargins = newValue
- }
- }
-
- /// :nodoc:
- override public init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
- super.init(style: style, reuseIdentifier: reuseIdentifier)
-
- configure()
- }
-
- /// :nodoc:
- required public init?(coder aDecoder: NSCoder) {
- super.init(coder: aDecoder)
-
- configure()
- }
-
- deinit {
- tableView?.panGestureRecognizer.removeTarget(self, action: nil)
- }
-
- func configure() {
- clipsToBounds = false
-
- swipeController = SwipeController(swipeable: self, actionsContainerView: self)
- swipeController.delegate = self
- }
-
- /// :nodoc:
- override open func prepareForReuse() {
- super.prepareForReuse()
-
- reset()
- resetSelectedState()
- }
-
- /// :nodoc:
- override open func didMoveToSuperview() {
- super.didMoveToSuperview()
-
- var view: UIView = self
- while let superview = view.superview {
- view = superview
- if let tableView = view as? UITableView {
- self.tableView = tableView
- swipeController.scrollView = tableView;
-
- tableView.panGestureRecognizer.removeTarget(self, action: nil)
- tableView.panGestureRecognizer.addTarget(self, action: #selector(handleTablePan(gesture:)))
- return
- }
- }
- }
-
- /// :nodoc:
- override open func setEditing(_ editing: Bool, animated: Bool) {
- super.setEditing(editing, animated: animated)
-
- if editing {
- hideSwipe(animated: false)
- }
- }
-
- // Override so we can accept touches anywhere within the cell's minY/maxY.
- // This is required to detect touches on the `SwipeActionsView` sitting alongside the
- // `SwipeTableCell`.
- /// :nodoc:
- override open func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
- guard let superview = superview else { return false }
-
- let point = convert(point, to: superview)
- if !UIAccessibility.isVoiceOverRunning {
- for cell in tableView?.swipeCells ?? [] {
- if (cell.state == .left || cell.state == .right) && !cell.contains(point: point) {
- tableView?.hideSwipeCell()
- return false
- }
- }
- }
-
- return contains(point: point)
- }
-
- func contains(point: CGPoint) -> Bool {
- return point.y > frame.minY && point.y < frame.maxY
- }
-
- /// :nodoc:
- override open func setHighlighted(_ highlighted: Bool, animated: Bool) {
- if state == .center {
- super.setHighlighted(highlighted, animated: animated)
- }
- }
-
- /// :nodoc:
- override open func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
- return swipeController.gestureRecognizerShouldBegin(gestureRecognizer)
- }
-
- /// :nodoc:
- open override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
- super.traitCollectionDidChange(previousTraitCollection)
-
- swipeController.traitCollectionDidChange(from: previousTraitCollection, to: self.traitCollection)
- }
-
- @objc func handleTablePan(gesture: UIPanGestureRecognizer) {
- if gesture.state == .began {
- hideSwipe(animated: true)
- }
- }
-
- func reset() {
- swipeController.reset()
- clipsToBounds = false
- }
-
- func resetSelectedState() {
- if isPreviouslySelected {
- if let tableView = tableView, let indexPath = tableView.indexPath(for: self) {
- tableView.selectRow(at: indexPath, animated: false, scrollPosition: .none)
- }
- }
- isPreviouslySelected = false
- }
- }
- extension SwipeTableViewCell: SwipeControllerDelegate {
- func swipeController(_ controller: SwipeController, canBeginEditingSwipeableFor orientation: SwipeActionsOrientation) -> Bool {
- return self.isEditing == false
- }
-
- func swipeController(_ controller: SwipeController, editActionsForSwipeableFor orientation: SwipeActionsOrientation) -> [SwipeAction]? {
- guard let tableView = tableView, let indexPath = tableView.indexPath(for: self) else { return nil }
-
- return delegate?.tableView(tableView, editActionsForRowAt: indexPath, for: orientation)
- }
-
- func swipeController(_ controller: SwipeController, editActionsOptionsForSwipeableFor orientation: SwipeActionsOrientation) -> SwipeOptions {
- guard let tableView = tableView, let indexPath = tableView.indexPath(for: self) else { return SwipeOptions() }
-
- return delegate?.tableView(tableView, editActionsOptionsForRowAt: indexPath, for: orientation) ?? SwipeOptions()
- }
-
- func swipeController(_ controller: SwipeController, visibleRectFor scrollView: UIScrollView) -> CGRect? {
- guard let tableView = tableView else { return nil }
-
- return delegate?.visibleRect(for: tableView)
- }
-
- func swipeController(_ controller: SwipeController, willBeginEditingSwipeableFor orientation: SwipeActionsOrientation) {
- guard let tableView = tableView, let indexPath = tableView.indexPath(for: self) else { return }
-
- // Remove highlight and deselect any selected cells
- super.setHighlighted(false, animated: false)
- isPreviouslySelected = isSelected
- tableView.deselectRow(at: indexPath, animated: false)
-
- delegate?.tableView(tableView, willBeginEditingRowAt: indexPath, for: orientation)
- }
-
- func swipeController(_ controller: SwipeController, didEndEditingSwipeableFor orientation: SwipeActionsOrientation) {
- guard let tableView = tableView, let indexPath = tableView.indexPath(for: self), let actionsView = self.actionsView else { return }
-
- resetSelectedState()
-
- delegate?.tableView(tableView, didEndEditingRowAt: indexPath, for: actionsView.orientation)
- }
-
- func swipeController(_ controller: SwipeController, didDeleteSwipeableAt indexPath: IndexPath) {
- tableView?.deleteRows(at: [indexPath], with: .none)
- }
- }
|