检测在“单元格”上单击了哪个“视图”
Detect which `View` was clicked on a `Cell`
我在一个 Cell
上有三个 UIImageView
当我点击单元格上的任何一个 UIImageView 时我想检测哪个被点击 onCellSelection
,而不是放置一个UITapGestureRecognizer
每个 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
}
重写 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
}
Location 将是一个 CGPoint。您应该能够获取图像边界的 CGPoints,然后确定这些边界内是否有 touchBegan。如果你想包括用户触摸的整个路径,也有一些方法可以做到,但开始的触摸应该足以满足你的需要。
使用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
}
}
文档:
我在一个 Cell
上有三个 UIImageView
当我点击单元格上的任何一个 UIImageView 时我想检测哪个被点击 onCellSelection
,而不是放置一个UITapGestureRecognizer
每个 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
}
重写 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
}
Location 将是一个 CGPoint。您应该能够获取图像边界的 CGPoints,然后确定这些边界内是否有 touchBegan。如果你想包括用户触摸的整个路径,也有一些方法可以做到,但开始的触摸应该足以满足你的需要。
使用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
}
}
文档: