Giter Site home page Giter Site logo

nicolasgomollon / ptatableviewcell Goto Github PK

View Code? Open in Web Editor NEW
11.0 2.0 1.0 107 KB

Convenient UITableViewCell subclass that supports pan gestures to trigger actions (as seen in apps such as Clear, Mailbox, Tweetbot, and more).

License: MIT License

Swift 100.00%

ptatableviewcell's Introduction

PTATableViewCell

PTATableViewCell (PTA is short for “Pan to Trigger Action”) is a convenient UITableViewCell subclass that supports pan gestures to trigger actions (as seen in apps such as Clear, Mailbox, Tweetbot, and more). PTATableViewCell is written completely in Swift (adapted from Objective-C, original code by: alikaragoz/MCSwipeTableViewCell).

Sample Screenshot

Usage

Here’s an example usage, with various attributes modified to show a few of the cell’s properties.

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
	let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! PTATableViewCell
	
	cell.textLabel.text = objects[indexPath.row]
	
	
	cell.delegate = self
	
	cell.setPanGesture(.leftToRight, mode: .switch, trigger: PTATableViewItemTrigger(kind: .offset, value: 64.0), color: view.tintColor, view: viewWithImage(named: "check"))
	cell.leftToRightAttr.viewBehavior = .dragWithPanThenStick
	
	let redColor = UIColor(red: 232.0/255.0, green: 61.0/255.0, blue: 14.0/255.0, alpha: 1.0)
	cell.setPanGesture(.rightToLeft, mode: .exit, color: redColor, view: viewWithImage(named: "cross"))
	
	cell.rightToLeftAttr.trigger.value = 0.4
	cell.rightToLeftAttr.rubberbandBounce = false
	cell.rightToLeftAttr.viewBehavior = .dragWithPan
	
	
	return cell
}

It’s important that you implement the following delegate method to perform an action when the cell’s state is triggered:

func tableView(cell: PTATableViewCell, didTrigger state: PTATableViewItemState, with mode: PTATableViewItemMode) {
	guard let indexPath = tableView.indexPath(for: cell) else { return }
	switch mode {
	case .switch:
		print("row \(indexPath.row)'s switch was triggered")
		// Do something interesting here.
	case .exit:
		print("row \(indexPath.row)'s exit was triggered")
		// Do something interesting here.
		objects.remove(at: indexPath.row)
		tableView.deleteRows(at: [indexPath], with: .fade)
	default:
		break
	}
}

There are also a few optional delegate methods you may implement:

// Asks the delegate whether a given cell should be swiped. Defaults to `true` if not implemented.
func tableViewShouldSwipe(cell: PTATableViewCell) -> Bool

// Tells the delegate that the specified cell is being swiped with the offset and percentage.
func tableViewIsSwiping(cell: PTATableViewCell, with offset: CGFloat, percentage: Double)

// Tells the delegate that the specified cell started swiping.
func tableViewDidStartSwiping(cell: PTATableViewCell)

// Tells the delegate that the specified cell ended swiping.
func tableViewDidEndSwiping(cell: PTATableViewCell)

See the ActionTest demo project included in this repository for a working example of the project, including the code above.

Requirements

Since PTATableViewCell is written in Swift 3, it requires Xcode 8 or above and works on iOS 8 and above.

License

PTATableViewCell is released under the MIT License.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.