我已经为集合视图细胞图像添加了UITapGestureRecognizer,如
代码:使用此代码,如果我点击两个细胞图像,则两个都会显示.如果我点击第二个细胞图像,那么如何从前景中删除第一个图像
当我在新的ImageView(但在集合视图上)之外点击时,这里touchesBegan
没有呼叫,为什么?对此有什么解决办法吗?
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()
}
}
如果我点击细胞图像,那么如何删除所有在前景中的图像.请给我带路