How to configure threshold/distance when swiping on UITableViewCell










3















I have swipeable tableview cells. The intention is to let the user swipe rows left or right completely (swipe out completely), and the swiped row gets removed from the table (like how Inbox by Gmail works). Everything works fine, but I had a question.



func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? 
let swipeRightAction = UIContextualAction(style: .destructive, title: "", handler: (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
let item = self.myItems[indexPath.row]
self.swipeRight(item) //do something
self.myItems.remove(at: indexPath.row)
success(true)
)

return UISwipeActionsConfiguration(actions: [swipeRightAction])



How can I set the threshold/distance (how much the user has to swipe before the action is taken)? Currently, the user has to swipe half way, before the row gets swiped out. Can I change this point so the user only needs to swipe a little (say 20% of the way) to swipe out the row?










share|improve this question
























  • 1. never knew there was something called UISwipeActionsConfiguration :) 2. Have you seen such ever implemented in any apps? 3. Interesting question overall. upvoted

    – Honey
    Nov 16 '18 at 17:55












  • While this is not a solution to your problem, I urge you to consider this. A 20% swipe to delete "may" cause incorrect deletes to register while the user is scrolling your tableview as opposed to the half-way default swipe which is a much more clear indication that the intent of the user was to delete the cell.

    – jms
    Nov 21 '18 at 5:53











  • @Honey: It was introduced in UITableView with iOS 11, so it's quite new. More about this API here.

    – Vin Gazoil
    Nov 23 '18 at 16:39















3















I have swipeable tableview cells. The intention is to let the user swipe rows left or right completely (swipe out completely), and the swiped row gets removed from the table (like how Inbox by Gmail works). Everything works fine, but I had a question.



func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? 
let swipeRightAction = UIContextualAction(style: .destructive, title: "", handler: (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
let item = self.myItems[indexPath.row]
self.swipeRight(item) //do something
self.myItems.remove(at: indexPath.row)
success(true)
)

return UISwipeActionsConfiguration(actions: [swipeRightAction])



How can I set the threshold/distance (how much the user has to swipe before the action is taken)? Currently, the user has to swipe half way, before the row gets swiped out. Can I change this point so the user only needs to swipe a little (say 20% of the way) to swipe out the row?










share|improve this question
























  • 1. never knew there was something called UISwipeActionsConfiguration :) 2. Have you seen such ever implemented in any apps? 3. Interesting question overall. upvoted

    – Honey
    Nov 16 '18 at 17:55












  • While this is not a solution to your problem, I urge you to consider this. A 20% swipe to delete "may" cause incorrect deletes to register while the user is scrolling your tableview as opposed to the half-way default swipe which is a much more clear indication that the intent of the user was to delete the cell.

    – jms
    Nov 21 '18 at 5:53











  • @Honey: It was introduced in UITableView with iOS 11, so it's quite new. More about this API here.

    – Vin Gazoil
    Nov 23 '18 at 16:39













3












3








3


1






I have swipeable tableview cells. The intention is to let the user swipe rows left or right completely (swipe out completely), and the swiped row gets removed from the table (like how Inbox by Gmail works). Everything works fine, but I had a question.



func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? 
let swipeRightAction = UIContextualAction(style: .destructive, title: "", handler: (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
let item = self.myItems[indexPath.row]
self.swipeRight(item) //do something
self.myItems.remove(at: indexPath.row)
success(true)
)

return UISwipeActionsConfiguration(actions: [swipeRightAction])



How can I set the threshold/distance (how much the user has to swipe before the action is taken)? Currently, the user has to swipe half way, before the row gets swiped out. Can I change this point so the user only needs to swipe a little (say 20% of the way) to swipe out the row?










share|improve this question
















I have swipeable tableview cells. The intention is to let the user swipe rows left or right completely (swipe out completely), and the swiped row gets removed from the table (like how Inbox by Gmail works). Everything works fine, but I had a question.



func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? 
let swipeRightAction = UIContextualAction(style: .destructive, title: "", handler: (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
let item = self.myItems[indexPath.row]
self.swipeRight(item) //do something
self.myItems.remove(at: indexPath.row)
success(true)
)

return UISwipeActionsConfiguration(actions: [swipeRightAction])



How can I set the threshold/distance (how much the user has to swipe before the action is taken)? Currently, the user has to swipe half way, before the row gets swiped out. Can I change this point so the user only needs to swipe a little (say 20% of the way) to swipe out the row?







ios swift uitableview






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 17:52









rmaddy

240k27313377




240k27313377










asked Nov 12 '18 at 15:23









PrabhuPrabhu

4,9302195157




4,9302195157












  • 1. never knew there was something called UISwipeActionsConfiguration :) 2. Have you seen such ever implemented in any apps? 3. Interesting question overall. upvoted

    – Honey
    Nov 16 '18 at 17:55












  • While this is not a solution to your problem, I urge you to consider this. A 20% swipe to delete "may" cause incorrect deletes to register while the user is scrolling your tableview as opposed to the half-way default swipe which is a much more clear indication that the intent of the user was to delete the cell.

    – jms
    Nov 21 '18 at 5:53











  • @Honey: It was introduced in UITableView with iOS 11, so it's quite new. More about this API here.

    – Vin Gazoil
    Nov 23 '18 at 16:39

















  • 1. never knew there was something called UISwipeActionsConfiguration :) 2. Have you seen such ever implemented in any apps? 3. Interesting question overall. upvoted

    – Honey
    Nov 16 '18 at 17:55












  • While this is not a solution to your problem, I urge you to consider this. A 20% swipe to delete "may" cause incorrect deletes to register while the user is scrolling your tableview as opposed to the half-way default swipe which is a much more clear indication that the intent of the user was to delete the cell.

    – jms
    Nov 21 '18 at 5:53











  • @Honey: It was introduced in UITableView with iOS 11, so it's quite new. More about this API here.

    – Vin Gazoil
    Nov 23 '18 at 16:39
















1. never knew there was something called UISwipeActionsConfiguration :) 2. Have you seen such ever implemented in any apps? 3. Interesting question overall. upvoted

– Honey
Nov 16 '18 at 17:55






1. never knew there was something called UISwipeActionsConfiguration :) 2. Have you seen such ever implemented in any apps? 3. Interesting question overall. upvoted

– Honey
Nov 16 '18 at 17:55














While this is not a solution to your problem, I urge you to consider this. A 20% swipe to delete "may" cause incorrect deletes to register while the user is scrolling your tableview as opposed to the half-way default swipe which is a much more clear indication that the intent of the user was to delete the cell.

– jms
Nov 21 '18 at 5:53





While this is not a solution to your problem, I urge you to consider this. A 20% swipe to delete "may" cause incorrect deletes to register while the user is scrolling your tableview as opposed to the half-way default swipe which is a much more clear indication that the intent of the user was to delete the cell.

– jms
Nov 21 '18 at 5:53













@Honey: It was introduced in UITableView with iOS 11, so it's quite new. More about this API here.

– Vin Gazoil
Nov 23 '18 at 16:39





@Honey: It was introduced in UITableView with iOS 11, so it's quite new. More about this API here.

– Vin Gazoil
Nov 23 '18 at 16:39












1 Answer
1






active

oldest

votes


















3





+50









There is no direct configuration method for it.



Solution



But you can build it yourself:



  • add a UIPanGestureRecognizer to the table

  • allow simultaneous recognitions of gestures

  • when a swipe ends check for direction and compute percentage out of the translation

  • determine the affected table row

  • delete it

Code



A swipe of the cells to the left by 20%, leading to the deletion of a row, would look like this in the code:



class ViewController: UITableViewController, UIGestureRecognizerDelegate {

override func viewDidLoad()
super.viewDidLoad()
...
let swipeGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(onSwiped(_:)))
swipeGestureRecognizer.delegate = self
self.tableView.addGestureRecognizer(swipeGestureRecognizer)


func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool
return true


@objc private func onSwiped(_ gestureRecognizer: UIPanGestureRecognizer)
if gestureRecognizer.state == .ended
let translation = gestureRecognizer.translation(in: self.view)
guard translation.x < 0 else return
let width = self.tableView.bounds.width
let percentage = -translation.x / width
print("swiped left percentage:(percentage)")
if percentage > 0.2
let location = gestureRecognizer.location(in: self.tableView)
if let indexPath = self.tableView.indexPathForRow(at: location)
print("delete row: (indexPath.row)")
self.dataSource.remove(at: indexPath.row)
self.tableView.reloadData()






Demo



swipe 20% to delete row






share|improve this answer

























  • This would get used in conjunction with leadingSwipeActionsConfigurationForRowAt right?

    – Prabhu
    Nov 16 '18 at 22:39






  • 1





    this example would be used in conjunction with a trailingSwipeActionsConfigurationForRowAt since we swipe to the left but the same mechanism could be used with a swipe to the right (changing the guard to translation.x > 0 and percentage = translation.x / width)

    – Stephan Schlecht
    Nov 16 '18 at 22:53











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53265211%2fhow-to-configure-threshold-distance-when-swiping-on-uitableviewcell%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3





+50









There is no direct configuration method for it.



Solution



But you can build it yourself:



  • add a UIPanGestureRecognizer to the table

  • allow simultaneous recognitions of gestures

  • when a swipe ends check for direction and compute percentage out of the translation

  • determine the affected table row

  • delete it

Code



A swipe of the cells to the left by 20%, leading to the deletion of a row, would look like this in the code:



class ViewController: UITableViewController, UIGestureRecognizerDelegate {

override func viewDidLoad()
super.viewDidLoad()
...
let swipeGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(onSwiped(_:)))
swipeGestureRecognizer.delegate = self
self.tableView.addGestureRecognizer(swipeGestureRecognizer)


func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool
return true


@objc private func onSwiped(_ gestureRecognizer: UIPanGestureRecognizer)
if gestureRecognizer.state == .ended
let translation = gestureRecognizer.translation(in: self.view)
guard translation.x < 0 else return
let width = self.tableView.bounds.width
let percentage = -translation.x / width
print("swiped left percentage:(percentage)")
if percentage > 0.2
let location = gestureRecognizer.location(in: self.tableView)
if let indexPath = self.tableView.indexPathForRow(at: location)
print("delete row: (indexPath.row)")
self.dataSource.remove(at: indexPath.row)
self.tableView.reloadData()






Demo



swipe 20% to delete row






share|improve this answer

























  • This would get used in conjunction with leadingSwipeActionsConfigurationForRowAt right?

    – Prabhu
    Nov 16 '18 at 22:39






  • 1





    this example would be used in conjunction with a trailingSwipeActionsConfigurationForRowAt since we swipe to the left but the same mechanism could be used with a swipe to the right (changing the guard to translation.x > 0 and percentage = translation.x / width)

    – Stephan Schlecht
    Nov 16 '18 at 22:53
















3





+50









There is no direct configuration method for it.



Solution



But you can build it yourself:



  • add a UIPanGestureRecognizer to the table

  • allow simultaneous recognitions of gestures

  • when a swipe ends check for direction and compute percentage out of the translation

  • determine the affected table row

  • delete it

Code



A swipe of the cells to the left by 20%, leading to the deletion of a row, would look like this in the code:



class ViewController: UITableViewController, UIGestureRecognizerDelegate {

override func viewDidLoad()
super.viewDidLoad()
...
let swipeGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(onSwiped(_:)))
swipeGestureRecognizer.delegate = self
self.tableView.addGestureRecognizer(swipeGestureRecognizer)


func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool
return true


@objc private func onSwiped(_ gestureRecognizer: UIPanGestureRecognizer)
if gestureRecognizer.state == .ended
let translation = gestureRecognizer.translation(in: self.view)
guard translation.x < 0 else return
let width = self.tableView.bounds.width
let percentage = -translation.x / width
print("swiped left percentage:(percentage)")
if percentage > 0.2
let location = gestureRecognizer.location(in: self.tableView)
if let indexPath = self.tableView.indexPathForRow(at: location)
print("delete row: (indexPath.row)")
self.dataSource.remove(at: indexPath.row)
self.tableView.reloadData()






Demo



swipe 20% to delete row






share|improve this answer

























  • This would get used in conjunction with leadingSwipeActionsConfigurationForRowAt right?

    – Prabhu
    Nov 16 '18 at 22:39






  • 1





    this example would be used in conjunction with a trailingSwipeActionsConfigurationForRowAt since we swipe to the left but the same mechanism could be used with a swipe to the right (changing the guard to translation.x > 0 and percentage = translation.x / width)

    – Stephan Schlecht
    Nov 16 '18 at 22:53














3





+50







3





+50



3




+50





There is no direct configuration method for it.



Solution



But you can build it yourself:



  • add a UIPanGestureRecognizer to the table

  • allow simultaneous recognitions of gestures

  • when a swipe ends check for direction and compute percentage out of the translation

  • determine the affected table row

  • delete it

Code



A swipe of the cells to the left by 20%, leading to the deletion of a row, would look like this in the code:



class ViewController: UITableViewController, UIGestureRecognizerDelegate {

override func viewDidLoad()
super.viewDidLoad()
...
let swipeGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(onSwiped(_:)))
swipeGestureRecognizer.delegate = self
self.tableView.addGestureRecognizer(swipeGestureRecognizer)


func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool
return true


@objc private func onSwiped(_ gestureRecognizer: UIPanGestureRecognizer)
if gestureRecognizer.state == .ended
let translation = gestureRecognizer.translation(in: self.view)
guard translation.x < 0 else return
let width = self.tableView.bounds.width
let percentage = -translation.x / width
print("swiped left percentage:(percentage)")
if percentage > 0.2
let location = gestureRecognizer.location(in: self.tableView)
if let indexPath = self.tableView.indexPathForRow(at: location)
print("delete row: (indexPath.row)")
self.dataSource.remove(at: indexPath.row)
self.tableView.reloadData()






Demo



swipe 20% to delete row






share|improve this answer















There is no direct configuration method for it.



Solution



But you can build it yourself:



  • add a UIPanGestureRecognizer to the table

  • allow simultaneous recognitions of gestures

  • when a swipe ends check for direction and compute percentage out of the translation

  • determine the affected table row

  • delete it

Code



A swipe of the cells to the left by 20%, leading to the deletion of a row, would look like this in the code:



class ViewController: UITableViewController, UIGestureRecognizerDelegate {

override func viewDidLoad()
super.viewDidLoad()
...
let swipeGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(onSwiped(_:)))
swipeGestureRecognizer.delegate = self
self.tableView.addGestureRecognizer(swipeGestureRecognizer)


func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool
return true


@objc private func onSwiped(_ gestureRecognizer: UIPanGestureRecognizer)
if gestureRecognizer.state == .ended
let translation = gestureRecognizer.translation(in: self.view)
guard translation.x < 0 else return
let width = self.tableView.bounds.width
let percentage = -translation.x / width
print("swiped left percentage:(percentage)")
if percentage > 0.2
let location = gestureRecognizer.location(in: self.tableView)
if let indexPath = self.tableView.indexPathForRow(at: location)
print("delete row: (indexPath.row)")
self.dataSource.remove(at: indexPath.row)
self.tableView.reloadData()






Demo



swipe 20% to delete row







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 16 '18 at 22:31

























answered Nov 16 '18 at 22:25









Stephan SchlechtStephan Schlecht

4,8201810




4,8201810












  • This would get used in conjunction with leadingSwipeActionsConfigurationForRowAt right?

    – Prabhu
    Nov 16 '18 at 22:39






  • 1





    this example would be used in conjunction with a trailingSwipeActionsConfigurationForRowAt since we swipe to the left but the same mechanism could be used with a swipe to the right (changing the guard to translation.x > 0 and percentage = translation.x / width)

    – Stephan Schlecht
    Nov 16 '18 at 22:53


















  • This would get used in conjunction with leadingSwipeActionsConfigurationForRowAt right?

    – Prabhu
    Nov 16 '18 at 22:39






  • 1





    this example would be used in conjunction with a trailingSwipeActionsConfigurationForRowAt since we swipe to the left but the same mechanism could be used with a swipe to the right (changing the guard to translation.x > 0 and percentage = translation.x / width)

    – Stephan Schlecht
    Nov 16 '18 at 22:53

















This would get used in conjunction with leadingSwipeActionsConfigurationForRowAt right?

– Prabhu
Nov 16 '18 at 22:39





This would get used in conjunction with leadingSwipeActionsConfigurationForRowAt right?

– Prabhu
Nov 16 '18 at 22:39




1




1





this example would be used in conjunction with a trailingSwipeActionsConfigurationForRowAt since we swipe to the left but the same mechanism could be used with a swipe to the right (changing the guard to translation.x > 0 and percentage = translation.x / width)

– Stephan Schlecht
Nov 16 '18 at 22:53






this example would be used in conjunction with a trailingSwipeActionsConfigurationForRowAt since we swipe to the left but the same mechanism could be used with a swipe to the right (changing the guard to translation.x > 0 and percentage = translation.x / width)

– Stephan Schlecht
Nov 16 '18 at 22:53


















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53265211%2fhow-to-configure-threshold-distance-when-swiping-on-uitableviewcell%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

Syphilis

Darth Vader #20