How to add swipe gesture for uilabel inside tableview in ios swift?

不打扰是莪最后的温柔 提交于 2019-12-05 06:24:23

问题


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))

    }
   }

回答1:


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


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)")



回答2:


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)



回答3:


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



来源:https://stackoverflow.com/questions/49870225/how-to-add-swipe-gesture-for-uilabel-inside-tableview-in-ios-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!