为什么UITapGestureRecognizer不能在Swift集合视图中对单元格图像正常工作?

zu0ti5jz  于 2023-06-21  发布在  Swift
关注(0)|答案(1)|浏览(87)

我添加了UITapGestureRecognizer用于collectionview单元格图片,如
代码:与此代码,如果我点击两个细胞的图像,然后都显示.在这里,如果我点击第二个单元格图像,然后如何从前景中删除第一个图像
在这里touchesBegan不调用时,我点击外部的newImageView(但在collectionview)为什么?有什么解决办法吗
o/p screen of the code

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: FriendCollectionViewCell.cellId, for: indexPath) as! FriendCollectionViewCell

let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(cellTappedMethod(_:)))
cell.profileImageView?.gestureRecognizers?.removeAll()
cell.profileImageView.isUserInteractionEnabled = true
cell.profileImageView.tag = indexPath.row
cell.profileImageView.addGestureRecognizer(tapGestureRecognizer)

return cell

}

@objc func cellTappedMethod(_ sender: UITapGestureRecognizer){

let imageView = sender.view as! UIImageView
let newImageView = UIImageView(image: imageView.image)
newImageView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width * 0.7, height: 300.0)//UIScreen.main.bounds
UIView.animate(withDuration: 0.2) { [self] in
    
    newImageView.center = CGPoint(x: UIScreen.main.bounds.center.x - 15, y: UIScreen.main.bounds.center.y - 200)
    
    newImageView.backgroundColor = UIColor.clear
    newImageView.contentMode = .scaleAspectFit
    newImageView.isUserInteractionEnabled = true
    let tap = UITapGestureRecognizer(target: self, action: #selector(dismissFullscreenImage))
    newImageView.addGestureRecognizer(tap)
    
    self.view.addSubview(newImageView)
}
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
{

let touch = touches.first
print("touch..outside")

var sender = UITapGestureRecognizer()
print(sender.view?.tag)

if ((touch?.view) != nil){
    print("touch..")
    sender.view?.removeFromSuperview()
}
}


@objc func dismissFullscreenImage(sender: UITapGestureRecognizer) {
UIView.animate(withDuration: 0.2) { [self] in
    sender.view?.removeFromSuperview()
}
}

如果我点击单元格图像,那么如何删除前景中所有图像。请指引我

eit6fx6z

eit6fx6z1#

要实现在点击新单元格图像时从前景中移除前一个图像的所需行为,您可以跟踪当前显示的图像视图,并在添加新图像之前将其移除。

var currentImageView: UIImageView?

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: FriendCollectionViewCell.cellId, for: indexPath) as! FriendCollectionViewCell
    
    let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(cellTappedMethod(_:)))
    cell.profileImageView?.gestureRecognizers?.removeAll()
    cell.profileImageView.isUserInteractionEnabled = true
    cell.profileImageView.tag = indexPath.row
    cell.profileImageView.addGestureRecognizer(tapGestureRecognizer)
    
    return cell
}

@objc func cellTappedMethod(_ sender: UITapGestureRecognizer) {
    let imageView = sender.view as! UIImageView
    
    currentImageView?.removeFromSuperview()
    
    let newImageView = UIImageView(image: imageView.image)
    newImageView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width * 0.7, height: 300.0)
    newImageView.center = CGPoint(x: UIScreen.main.bounds.center.x - 15, y: UIScreen.main.bounds.center.y - 200)
    newImageView.backgroundColor = UIColor.clear
    newImageView.contentMode = .scaleAspectFit
    newImageView.isUserInteractionEnabled = true
    
    let tap = UITapGestureRecognizer(target: self, action: #selector(dismissFullscreenImage))
    newImageView.addGestureRecognizer(tap)
    self.view.addSubview(newImageView)
    currentImageView = newImageView
}

@objc func dismissFullscreenImage(sender: UITapGestureRecognizer) {
    sender.view?.removeFromSuperview()
    currentImageView = nil
}

在这段代码中,currentImageView变量是在类级别声明的,以跟踪当前显示在屏幕上的图像视图。点击单元格图像时,将从超级视图中删除前一个图像视图(如果有),然后再添加新视图。currentImageView变量将使用新的图像视图进行更新,并且在关闭全屏图像时,将清除引用以指示当前未显示任何图像视图。
至于在newImageView外部点击时没有调用touchesBegan的问题,这是因为newImageView位于集合视图的顶部并拦截了触摸事件。一种解决方案是子类化UICollectionView并覆盖子类中的touchesBegan方法。

class MyCollectionView: UICollectionView {
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
    }
}

相关问题