检测在“单元格”上单击了哪个“视图”

时间:2019-06-01 19:39:29

标签: ios swift uitableview eureka-forms

当我单击单元格上的任何UIImageView时,我想在单个accounts/<username>/上拥有三个UIImageView,我想检测在Cell上单击了哪个,而不放置{{每个onCellSelection

上的1}}
UITapGestureRecognizer

2 个答案:

答案 0 :(得分:3)

使用UITapGestureRecogniser(或UIButton)将是更好的方法。这些类旨在完成此类任务。

如果您仍然想使用其他方法,请将方法添加到您的单元格子类中(用自己的属性替换imageView1, imageView2, imageView3

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
  guard let touch = touches.first else { return }
  let point = touch.location(in: view)
  if imageView1.frame.containsPoint(point) {
    // code for 1st image view
  } else if imageView2.frame.containsPoint(point) {
    // code for 2nd image view
  } else if imageView3.frame.containsPoint(point) {
    // code for 3rd image view
  }
}

文档:

答案 1 :(得分:1)

覆盖touchesbegan功能。每次用户触摸屏幕时都会调用此方法。每次调用时,请检查触摸是否在与图像相同的位置开始。

 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
     let touch = touches.first!
     let location = touch.location(in: self)
     //check here, include code that compares the locations of the image
}

位置将为CGPoint。您应该能够获取图像边界的CGPoint,然后确定touchBegan是否在这些边界内。如果您想包括用户触摸过的整个路径,也可以使用多种方法来完成,但是开始触摸就可以满足您的需求。