In each cell of my collection view is a circular UIView. This has been achieved by creating a custom subclass of UIView
, which I have called CircleView
, and setting layer.cornerRadius = self.frame.size.width/2
in the subclass' awakeFromNib()
I want to add a gesture recognizer to each CircleView. I have done this in the collection view's cellForItemAtIndexPath
:
let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(tap(_:)))
cell.circleView.addGestureRecognizer(gestureRecognizer)
The problem is that the gesture recognizer is called whenever a tap occurs anywhere within the bounds of the original square UIView. I want to only recognize taps that occur within the circle.
I have tried to solve this issue in the following ways:
In the CircleView's awakeFromNib()
I set self.clipsToBounds = true
(no effect)
Also in the CircleView's awakeFromNib()
I set layer.masksToBounds = true
(no effect)
Thank you in advance for your ideas and suggestions.