Reputation: 31
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
Upvotes: 1
Views: 40