2

I am trying to creating check list in a table view. Inside table view radio button and UILabel, here adding swipe gesture to UILabel. For single swipe of UILabel will show radio button or double swipe of UILabel will show dash line.

I have tried of adding gesture to UILabel inside table by using selector method, it print successfully but UILabel is not swiping.

Here is the code tried of swiping UILabel:

In viewDidLoad:

let gestureRec = UISwipeGestureRecognizer(target: self, action: #selector(didTap(sender:)))
    tableView.addGestureRecognizer(gestureRec)
    gestureRec.delegate = self as? UIGestureRecognizerDelegate

And created function called didTap:

     @objc func didTap(sender : UISwipeGestureRecognizer)
      {
      if sender.state == .ended {
        let location = sender.location(in: self.tableView)
        let indexPath = self.tableView.indexPathForRow(at: location)
        var cell = self.tableView.cellForRow(at: indexPath!) as! textCell
        print("swipe")
        cell.labelView.frame(forAlignmentRect: CGRect(x: 10, y: 8, width: 20, 
     height: 15))

    }
   }
PvDev
  • 791
  • 21
  • 67
  • It seems like you are after the same functionality as when you are deleting a message (i.e. when a user swipes, the cell moves and shows additional options). If that is what you are after, then you need to look into `UITableView`'s methods such as `func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool` & `func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath)` – Malik Apr 17 '18 at 05:56

3 Answers3

4

Try this code. Tested and working fine 100 %

let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    cell.textLabel?.text = tableDataSource[indexPath.row]
    cell.textLabel?.isUserInteractionEnabled = true
    let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(self.labelSwipedLeft(sender:)))
    cell.textLabel?.addGestureRecognizer(swipeLeft)
    return cell

@objc func labelSwipedLeft(sender: UITapGestureRecognizer) {
    print("labelSwipedLeft called")
  }

See the output enter image description here


Additional: If you want to detect which row's label was tapped, then assign a tag to the label in cellForRowAt like

cell.textLabel?.tag = indexPath.row

And get the row index in the function labelSwipedLeft using

print("labelSwipedLeft called for row \(sender.view!.tag)")
Awais Fayyaz
  • 2,275
  • 1
  • 22
  • 45
  • @objc func swipedToLeft(sender : UISwipeGestureRecognizer) { if sender.state == .ended { let location = sender.location(in: self.tableView) let indexPath = self.tableView.indexPathForRow(at: location) var cell = self.tableView.cellForRow(at: indexPath!) as! textCell print("swipe") // cell.labelView.isUserInteractionEnabled = true // cell.labelView.frame = CGRect(x: 10, y: 8, width: 20, height: 30) } } Is this correct?????? – BizDev Apr 17 '18 at 05:57
  • @BizDev I don't think that label should move. This code is for recognizing a gesture. We have to move the label using some sort of animation i guess – Awais Fayyaz Apr 17 '18 at 06:01
  • Thanks right now the label is moving. how to do double swipe ? – BizDev Apr 17 '18 at 06:09
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/169135/discussion-between-awais-fayyaz-and-bizdev). – Awais Fayyaz Apr 17 '18 at 06:27
  • 2
    To use left swipe only, specify `swipeLeft.direction = .left` else it will work for left and right – Jay Strawn Feb 10 '20 at 22:32
3

The first thing I noticed is that you are adding the gesture on UITableView and not UILabel. So you need to fix that first.
Secondly, for a gesture to work correctly on a control, you must enable the user interaction property of that control, else it won't respond to the gesture. So :

label.isUserInteractionEnabled = true  

You can add the gesture as follows :

let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(swipedToLeft))
swipeLeft.direction = UISwipeGestureRecognizerDirection.Left
label.addGestureRecognizer(swipeLeft)
Nitish
  • 13,845
  • 28
  • 135
  • 263
  • Thanks @Nitish how add gesture to uilabel?? – PvDev Apr 17 '18 at 05:18
  • Same as you are doing for UITableView – Nitish Apr 17 '18 at 05:20
  • do you want add it in viewdidload let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(swipedToLeft)) swipeLeft.direction = UISwipeGestureRecognizerDirection.Left label.addGestureRecognizer(swipeLeft) – PvDev Apr 17 '18 at 05:33
  • here label is in tableview cell label.addGestureRecognizer(swipeLeft) – PvDev Apr 17 '18 at 05:33
  • It has to be in cellForRowAtIndexPath – Nitish Apr 17 '18 at 05:36
  • @Nitish how to move UIlabel to right while swiping. It prints successfully – BizDev Apr 17 '18 at 05:50
  • Is this correct ??? @objc func swipedToLeft(sender : UISwipeGestureRecognizer) { if sender.state == .ended { let location = sender.location(in: self.tableView) let indexPath = self.tableView.indexPathForRow(at: location) var cell = self.tableView.cellForRow(at: indexPath!) as! textCell print("swipe") // cell.labelView.isUserInteractionEnabled = true // cell.labelView.frame = CGRect(x: 10, y: 8, width: 20, height: 30) } } – BizDev Apr 17 '18 at 05:57
  • @objc func labelSwipe(sender: UITapGestureRecognizer) { print("labelSwipedLeft called") } – Nitish Apr 17 '18 at 06:21
1

I would suggest adding swipe gesture in cell class, rather than ViewController.

func setupGestures() {
    let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(handleLeftSwipe(_:)))
    swipeLeft.direction = UISwipeGestureRecognizerDirection.Left
    label.addGestureRecognizer(swipeLeft)
    label.isUserInteractionEnabled = true
}

@objc func handleLeftSwipe(_ recognizer: UISwipeGestureRecognizer) {

}

call setupGestures in awakeFromNib. Also if you want to drag the label, use pan gesture

Vaibhav Parmar
  • 461
  • 4
  • 14
  • This is a good suggestion, which I tried, but if you also use `leadingSwipeActionsConfigurationForRowAt:` or `trailing....`, then your code won't be executed. – Zonker.in.Geneva Aug 07 '19 at 11:40