swift - UITableView 在 SWIFT 的多个 View 上带有手势识别器

标签 swift uitableview uigesturerecognizer

我尝试在带有自定义单元格的自定义 tableView 中的多个元素上使用gestureRecognizer。

我有两个链接到特定操作的 imageView。 如果我不触摸这些图像,我想为单元格的背景着色。

因此代码一定不能位于 didSelectRowAtIndexPath 方法中。

我尝试了类似的方法,但对于该程序来说,图像似乎从未被触摸过。

我哪里理解错了?

class MyTableExampleViewController: UIViewController, UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()

    // Gesture recognizer
    let tapGestureRecognizer: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "handleTapGestureRecognizer:")
    self.tableView.addGestureRecognizer(tapGestureRecognizer)
}


...

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell:CustomExampleCell = tableView.dequeueReusableCellWithIdentifier("CustomExampleCell") as! CustomExampleCell

    cell.playImageView.image = UIImage(named: play)
    cell.stopImageView.image = UIImage(named: stop)

    return cell
}


func handleTapGestureRecognizer(gestureRecognizer:UITapGestureRecognizer) {
    let touchPoint:CGPoint = gestureRecognizer.locationInView(self.tableView)
    let indexPath = self.tableView.indexPathForRowAtPoint(touchPoint)

    let cell:CustomExampleCell = tableView.dequeueReusableCellWithIdentifier("CustomExampleCell") as! CustomExampleCell

    if let _ = cell.playImageView.layer.hitTest(touchPoint) {
        print("Action Play")
    } else if let _ = cell.stopImageView.layer.hitTest(touchPoint) {
        print("Action Stop")
    } else {
        cell.backgroundColor = UIColor.greenColor()
    }

}



}

最佳答案

您需要处理 CustomExampleCell 中的触摸。然后实现一个委托(delegate)来通知您的 View Controller (如果您需要)。

关于swift - UITableView 在 SWIFT 的多个 View 上带有手势识别器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35600594/

相关文章:

iOS swift : UIPanGestureRecognizer in UIScrollView

objective-c - 何时将平移手势的结果保存到 NSUserDefaults

ios - 我将如何在 Swift 中重写这个 Objective-C 策略模式?

ios - Swift:关闭 ImageView

ipad - UITableView 部分在设备旋转后失去交互

ios6 - 如何以编程方式向下滑动 UITableView 以显示底层 UIRefreshControl

ios - 为什么之前调用 reloadRows 时 UITableView setContentOffset 无法正常工作?

swift - UITableView 作为 UIGestureRecognizerDelegate

ios - 在 SearchTextField.swift 中遇到错误

ios - 如何使用设备运动更新将 SCNNode 固定在特定位置?