Reputation: 1635
I have a UICollectionView and a UIButton over it (on the right edge). But in the button's frame the collectionView loses it's scrolling property.
I wan't to know if there's any way to maintain both interactions, the Button touch up inside and the CollectionView scroll.
Upvotes: 0
Views: 762
Reputation: 1635
Well, I figured out how to do that, but instead of buttons I used the UITapGestureRecognizer as rounak suggests:
let tapGesture = UITapGestureRecognizer(target: self, action: "collectionViewDidReceiveTap:")
tapGesture.delegate = self
self.collectionView.addGestureRecognizer(tapGesture)
As I wanted a specific rect to be "tapable" I've implemented the UIGestureRecognizerDelegate protocol, this piece of code do this:
public func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {
let collectionViewCurrentCell = self.collectionView.layoutAttributesForItemAtIndexPath(NSIndexPath(forItem: Int(self.currentPage), inSection: 0))
let cellFrameInSuperview = collectionView.convertRect(collectionViewCurrentCell!.frame, toView: self)
if gestureRecognizer.isKindOfClass(UITapGestureRecognizer) && CGRectContainsPoint(cellFrameInSuperview, touch.locationInView(self)) {
return false
}
return true
}
Where the cellFrameInSuperview should be the region where the TapGesture shouldn't do anything. But different from the UIButton, the scroll continues to work in all the Collection bounds.
If you have any questions about how, or why it works, leave a comment.
Upvotes: 1
Reputation: 9397
Instead of a button, use a view with a tap gesture recognizer and let that tap gesture recognizer work simultaneously with the scrollview's pan gesture.
Upvotes: 0