将手势识别器添加到表格单元格中的图像视图

b.g*_*son 5 uitableview ios swift

如何UIImageView在表格单元格中添加手势识别器?我想要它,这样如果用户点击单元格中的图像,图像将会改变,数据模型将更新.

我知道这需要设置在UITableViewController.我的代码当前可以执行命令,如果单元格中的任何位置被轻击,但我希望它只在图像被轻敲时执行,而不是在单元格中的任何位置执行.

我在中设置了手势识别器 viewDidLoad

override func viewDidLoad() {
    super.viewDidLoad()

    // Load sample data
    loadSampleHabits()

    // Initialize tap gesture recognizer
    var recognizer = UITapGestureRecognizer(target: self, action: #selector(tapEdit(recognizer:)))
    // Add gesture recognizer to the view
    self.tableView.addGestureRecognizer(recognizer)
Run Code Online (Sandbox Code Playgroud)

这就是功能

//action method for gesture recognizer
func tapEdit(recognizer: UITapGestureRecognizer) {
    if recognizer.state == UIGestureRecognizerState.ended {
        let tapLocation = recognizer.location(in: self.tableView)
        if let tapIndexPath = self.tableView.indexPathForRow(at: tapLocation) {
            if let tappedCell = self.tableView.cellForRow(at: tapIndexPath) as? HabitTableViewCell {
                print("Row Selected")

            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

细胞看起来像什么

作为第二个问题,如果我想在单元格中添加手势识别器和单元格中的图像视图,是否存在任何冲突?

luc*_*hra 9

您正在根据需要在tableview上添加手势识别器而不是imageView.你需要将你的代码从viewDidLoad移动cellForRowAtIndexPath到每个单元格中的imageView并添加手势,同时配置你的单元格.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
     var recognizer = UITapGestureRecognizer(target: self, action: #selector(tapEdit(recognizer:)))
     // Add gesture recognizer to your image view
     cell.yourimageview.addGestureRecognizer(recognizer)
}
Run Code Online (Sandbox Code Playgroud)

注意:确保启用userinteraction图像视图

cell.yourimageview.userInteractionEnabled = YES;
Run Code Online (Sandbox Code Playgroud)

根据您的要求,我建议使用UILongPressGestureRecognizer,因为它在手势和选择中的冲突几率较小.Yo可以在viewDidLoad中添加UILongPressGestureRecognizer,并根据您的要求访问它.

let lpgr = UILongPressGestureRecognizer(target: self, action: #selector(ViewController.handleLongPress(_:)))
lpgr.minimumPressDuration = 1
tableView.addGestureRecognizer(lpgr)
Run Code Online (Sandbox Code Playgroud)

将方法定义为

func handleLongPress(_ gesture: UILongPressGestureRecognizer){
if gesture.state != .began { return }
let tapLocation = gesture.location(in: self.tableView)
    if let tapIndexPath = self.tableView.indexPathForRow(at: tapLocation) {
        if let tappedCell = self.tableView.cellForRow(at: tapIndexPath) as? HabitTableViewCell {
            print("Row Selected")

        }
}
Run Code Online (Sandbox Code Playgroud)

您可以尝试if recognizer.state == UIGestureRecognizerState.ended从方法中删除 条件.

UITapGestureRecognizer是一个离散手势,因此,只有在识别手势时才会调用您的事件处理程序.您根本不需要检查状态.当然,你不会收到.Began州的电话.有关更多信息,请在此处考虑@Rob ans .


BHA*_*HAL 4

在索引路径处的行的单元格中添加此行

     var recognizer = UITapGestureRecognizer(target: self, action: #selector(tapEdit(recognizer:)))
        // Add gesture recognizer to the view
        cell.yourimageviewname.addGestureRecognizer(recognizer)

cell.yourimageviewname.userInteractionEnabled = true;
Run Code Online (Sandbox Code Playgroud)