I have added UITapGestureRecognizer for collectionview cell image like
code: with this code if i tap on two cell images then both are showing. Here if i tap on second cell image then how to remove first image from foreground
here touchesBegan not calling when i tap on outside of the newImageView(but on collectionview) why? is there any solution for this
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()
}
}
if i tap on cell image then how to remove all image which are in foreground. please guide me
To achieve the desired behavior of removing the previous image from the foreground when tapping on a new cell image, you can keep track of the currently displayed image view and remove it before adding a new one.
In this code the
currentImageViewvariable is declared at the class level to keep track of the image view currently displayed on the screen. When a cell image is tapped, the previous image view (if any) is removed from the superview before adding a new one. ThecurrentImageViewvariable is updated with the new image view, and when dismissing the full-screen image, the reference is cleared to indicate that no image view is currently displayed.As for the issue with
touchesBegannot being called when tapping outside thenewImageView, it's because thenewImageViewis on top of the collection view and intercepts the touch events. One solution is to subclassUICollectionViewand override thetouchesBeganmethod in the subclass.