ios - 将手势识别器添加到表格单元格中的 ImageView

标签 ios swift uitableview

如何将手势识别器添加到表格单元格中的 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)

这是函数

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

            }
        }
    }

What the cell looks like

作为次要问题,如果我想向单元格添加手势识别器和单元格内的 ImageView 是否有任何冲突?

最佳答案

您正在根据需要在 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)
}

注意:请确保启用 ImageView 的userinteraction

cell.yourimageview.userInteractionEnabled = YES;

根据您的要求,我会建议使用 UILongPressGestureRecognizer,因为它在手势和选择中发生冲突的可能性较小。您可以在 viewDidLoad 中添加 UILongPressGestureRecognizer 并根据您的要求访问它。

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

定义方法为

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

        }
}

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

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

关于ios - 将手势识别器添加到表格单元格中的 ImageView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45048891/

相关文章:

python - Core ML 模型转换失败,返回 "Unable to infer input name and dimensions"

iOS - AutoLayout 约束设置激活在第二次崩溃

swift - 有没有办法判断传入的变量是引用类型还是值类型?

swift - 在 Alamofire Swift 2.2 中使用 completionHandler

iphone - 在某些单元格上设置 UITableViewCell 附件类型,但不是全部

Swift:从 Firebase 数据库检索数据以进行标签

ios - 提交前如何获取app在appstore的链接

ios - 错误域=EKCADErrorDomain 代码=1013 "The operation couldn’ t 完成。 (EKCADErrorDomain 错误 1013。)”

ios - 传入的推送通知设置警报两次 swift 4

ios - Swift TableView in ViewController In Navigation VC in Tab Bar : Cells won't populate with data from list