I have a collection view with lots of different pictures, and I want that when a picture is tapped, a border comes around it.
I made a custom cell, and a UICollectionViewCell with the imageView embedding in another view (which is the outline) for the pic. So I set up a UITapGestureRecognizer which works to get the index, but when I set the outer view to have a boder it doesn't work.
Here is my Cell:
import UIKit
class PicViewCell: UICollectionViewCell {
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var imageOutline: UIView!
}
This is in my UICollectionViewController:
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! PicViewCell
// Configure the cell
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap(sender:)))
cell.imageOutline.addGestureRecognizer(tapGesture)
return cell
Here is the method for the tapGesture
func handleTap(sender: UITapGestureRecognizer) {
print("tap")
if let indexPath = self.collectionView?.indexPathForItem(at: sender.location(in: self.collectionView)) {
let cell = collectionView?.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! PicViewCell
cell.imageOutline.layer.borderWidth = 5
} else {
print("")
}
}