BigFire
BigFire

Reputation: 337

Detect which `View` was clicked on a `Cell`

i have three UIImageView on a single Cell when i click on any of the UIImageView on the cell i want to detect which one was clicked on onCellSelection, without placing a UITapGestureRecognizer on each UIImageview

func SocialViewRow(address: SocialMedia)-> ViewRow<SocialMediaViewFile> {

let viewRow = ViewRow<SocialMediaViewFile>() { (row) in

    row.tag = UUID.init().uuidString
    }
    .cellSetup { (cell, row) in
        //  Construct the view
        let bundle = Bundle.main
        let nib = UINib(nibName: "SocialMediaView", bundle: bundle)

        cell.view = nib.instantiate(withOwner: self, options: nil)[0] as? SocialMediaViewFile
        cell.view?.backgroundColor = cell.backgroundColor
        cell.height =  { 50 }
        print("LINK \(address.facebook?[0] ?? "")")

        cell.view?.iconOne.tag = 90090

        //self.itemDetails.activeURL = address

        let openFace = UITapGestureRecognizer(target: self, action: #selector(QuickItemDetailVC.openFace))
        let openT = UITapGestureRecognizer(target: self, action: #selector(QuickItemDetailVC.openTwit))
        let you = UITapGestureRecognizer(target: self, action: #selector(QuickItemDetailVC.openYouYub))

         cell.view?.iconOne.addGestureRecognizer(openFace)

         cell.view?.iconTwo.addGestureRecognizer(openT)
         cell.view?.iconThree.addGestureRecognizer(you)



        cell.frame.insetBy(dx: 5.0, dy: 5.0)
        cell.selectionStyle = .none

    }.onCellSelection() {cell,row in

     //example   
    //print(iconTwo was clicked)


}


return viewRow
}

Upvotes: 1

Views: 103

Answers (2)

Andy K
Andy K

Reputation: 7292

Using UITapGestureRecogniser (or UIButton) would be a better approach. These classes intended for tasks like this.

If you still want to use different approach, add method to your cell subclass (replace imageView1, imageView2, imageView3 with your own properties)

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

Docs:

Upvotes: 4

Alexander C
Alexander C

Reputation: 686

Override the touchesbegan function. This method is called every time the user touches the screen. Every time it is called, check to see if the touches began in the same location an image is.

 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
}

Location will be a CGPoint. You should be able to get the CGPoints for the bounds of your images and then determine if the touchBegan in those bounds. If you want to include the entire path the user touched, there are ways to do that too but the beginning touch should be sufficient for what you want.

Upvotes: 2

Related Questions