Ios 如何在集合视图单元格内为视图添加手势?
我正在集合视图单元格(也有其他视图)中添加一个视图,其中包含一组Ios 如何在集合视图单元格内为视图添加手势?,ios,swift,uicollectionviewcell,uitapgesturerecognizer,Ios,Swift,Uicollectionviewcell,Uitapgesturerecognizer,我正在集合视图单元格(也有其他视图)中添加一个视图,其中包含一组图像(socialView),必须在该单元格上执行公共单击。此单击不应与集合视图单元格单击相同 我正在考虑每次在CollectionView委托方法cellForItemAt中为socialView添加uitappesturerecognizer。但我想知道这是正确的方法吗?此外,我想获取调用了socialView的indexPath/位置 let tapGestureRecognizer = UITapGestureRecogni
图像(socialView)
,必须在该单元格上执行公共单击。此单击不应与集合视图单元格单击相同
我正在考虑每次在CollectionView
委托方法cellForItemAt
中为socialView
添加uitappesturerecognizer
。但我想知道这是正确的方法吗?此外,我想获取调用了socialView
的indexPath/位置
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.socialViewTapped))
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "bidderAuctionCell", for: indexPath) as! BidderAuctionViewCell
cell.socialView.addGestureRecognizer(tapGestureRecognizer)
}
@objc func socialViewTapped() {
// Which item cell's socialview is clicked
}
更新
我已按照以下建议进行了操作,但我无法在自定义单元格中找到应在何处添加UITapGestureRecognizer。下面是我创建的自定义单元格
class CustomViewCell: UICollectionViewCell {
var index : IndexPath!
var socialViewDelegate : SocialViewDelegate!
@IBOutlet weak var socialView: UIView!
override init(frame: CGRect) {
super.init(frame:frame)
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.viewTapped))
socialView.addGestureRecognizer(tapGestureRecognizer)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder:aDecoder)
}
@objc func viewTapped(){
socialViewDelegate.socialViewTapped(at: index)
}
}
未调用init函数。我还尝试输入required init,但是那里的社会视图没有初始化。所以被撞坏了
最终答案
class CustomViewCell: UICollectionViewCell {
var index : IndexPath!
var socialViewDelegate : SocialViewDelegate!
@IBOutlet weak var socialView: UIView!
override func awakeFromNib() {
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap))
socialView.isUserInteractionEnabled = true
socialView.addGestureRecognizer(tapGesture)
}
@objc func handleTap(){
socialViewDelegate.socialViewTapped(at: index)
}
}
protocol SocialViewDelegate {
func socialViewTapped(at index: IndexPath)
}
要添加点击手势,请将以下代码添加到集合视图的单元格中
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.socialViewTapped))
socialView?.addGestureRecognizer(tapGestureRecognizer)
socialView?.isUserInteractionEnabled = true
将此代码添加到集合视图单元格。@Anurag您可以使用以下概念之一解决此问题:委派、闭包、通知。我建议您使用iOS广泛遵循的委托模式,将其应用到UITableView、UICollectionView等组件中
- 在实现委派模式之前,向视图中添加轻触手势识别器。
- 在CollectionViewCell中声明一个协议方法以委派点击/操作
- 确认视图控制器中的代理实现。
- 在视图控制器中实施委托方法。
import UIKit
Protocol ViewTappedDelegate: class {
func viewTapped(_ photo : String) { }
}
class YourCell: UICollectionViewCell, UIGestureRecognizerDelegate {
@IBOutlet weak var containerView: UIView!
weak var delegate : ViewTappedDelegate?
override func awakeFromNib() {
let tapGesture = UITapGestureRecognizer(target: self,
action: #selector(handleTap(recognizer:)))
tapGesture.delegate = self
containerView.isUserInteractionEnabled = true
containerView.addGestureRecognizer(tapGesture)
}
@objc func handleTap(recognizer:UITapGestureRecognizer) {
//Call Delegate method from here...
}
}
2。在ViewController中
func collectionView(_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellIdentifier",
for: indexPath) as! YourCell
cell.delegate = self
return cell
}
// MARK: Delegate
extension YourViewController : ViewTappedDelegate {
func viewTapped(_ photo : String) {
// Handle delegation here...
}
}
如果你还没有找到办法,我希望这能奏效 在自定义单元格中使用这样的协议
import UIKit
protocol CustomDelegate {
func showData(indexPath: IndexPath?)
}
class CustomCell: UICollectionViewCell {
// properties
var delegate: CustomDelegate?
var selectedAtIndex: IndexPath?
// outlets
override func awakeFromNib() {
super.awakeFromNib()
myView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(dothis)))
}
@objc func dothis(){
delegate?.showData(indexPath: selectedAtIndex)
}
@IBOutlet weak var myView: UIView!
}
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, CustomDelegate{
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CustomCell
cell.delegate = self
cell.selectedAtIndex = indexPath
return cell
}
func showData(indexPath: IndexPath?) {
if let ip = indexPath{
print("\(ip) is the index path for tapped view")
}
}
}
像这样编写视图控制器
import UIKit
protocol CustomDelegate {
func showData(indexPath: IndexPath?)
}
class CustomCell: UICollectionViewCell {
// properties
var delegate: CustomDelegate?
var selectedAtIndex: IndexPath?
// outlets
override func awakeFromNib() {
super.awakeFromNib()
myView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(dothis)))
}
@objc func dothis(){
delegate?.showData(indexPath: selectedAtIndex)
}
@IBOutlet weak var myView: UIView!
}
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, CustomDelegate{
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CustomCell
cell.delegate = self
cell.selectedAtIndex = indexPath
return cell
}
func showData(indexPath: IndexPath?) {
if let ip = indexPath{
print("\(ip) is the index path for tapped view")
}
}
}
请告诉我这是否有帮助@RobertDresler添加了代码。请check@JarvisTheAvenger我已经更新了代码,无法在自定义类中添加手势。您可以通过将indexPath传递给自定义类来解决此问题。并将点击的内容委托给您的ViewController您是否已将该类添加到cell的XIB?您尚未将UIgestureRecognitizerDelegate添加到自定义类这是任何视图的通用解决方案,您能否告诉我如何获取已单击社交视图的indexPath.row。我添加了代码基本思想是将此代码添加到您创建的cell类中,因此将有一个不同的cell对象。如果需要indexPath,可以在单元格中创建一个变量,并在cellForRow at方法中设置它。现在可以检查吗@让我们在cell类中创建Override awakeFromNib方法,并在其中添加点击手势。谢谢,它成功了。但您不需要添加tappostate.delegate=self。只是我必须用awakeFromNib编写代码。不过非常感谢。