Skip to content

jeremyrea/JRLongPressGestureHandler

Repository files navigation

JRLongPressGestureHandler

Carthage compatible

Notice

This project is no longer maintained and was last compiled with Swift 3. I'd instead recommend using iOS's native support for drag and drop in tableviews, available as of iOS11.

This framework was originally a translation of UILongPressGestureRecognizer, written in Objective-C, found at https://github.com/moayes/UDo/ (as appeared on Ray Wenderlich) and I've since brought a few improvements so I bundled it to be easily reused in other iOS projects by others.

Preview

JRLongPressGestureHandler preview

Install

Using Carthage:

Add github "jeremyrea/JRLongPressGestureHandler" to your Cartfile and run carthage update.

Drag and drop the newly created JRLongPressGestureHandler.framework and JRLongPressGestureHandler.dSYM files found in $projectRoot/Carthage/Build/iOS/ into your project in Xcode.

The final step is adding the framework to your project's Embedded Binaries.

Usage

In your UITableViewController:

import JRLongPressGestureHandler

Create your touch recognizer at the top of the class:

private let longPress: UILongPressGestureRecognizer = {
  let recognizer = UILongPressGestureRecognizer()
  return recognizer
}()

private var gestureHandler: JRLongPressGestureHandler!


required init? (coder aDecoder: NSCoder) {
  super.init(coder: aDecoder)
  // ...
  gestureHandler = JRLongPressGestureHandler(delegate: self)
}

override func viewDidLoad() {
  // ...
  self.longPress.addTarget(self, action: #selector(longPressGestureRecognized(_:)))
  self.tableView.addGestureRecognizer(longPress)
}

func longPressGestureRecognized(gesture: UILongPressGestureRecognizer) {
  gestureHandler.longPressGestureRecognized(self.tableView, gesture: gesture)
}

You'll now want to specify the handler's protocol in your UITableViewController's declaration:

class TableViewController: UITableViewController, JRLongPressGestureHandlerDelegate

To conform to the protocol, you'll need to implement the following function:

func didEndLongPress(startIndexPath: NSIndexPath, endIndexPath: NSIndexPath) { {
  // You will want to update your datasource to swap
  // the items located at these positions
}

About

Swift framework to ease the reordering of UITableView cells with the long press gesture

Resources

License

Stars

Watchers

Forks

Packages

No packages published