I've implemented non scrollable UICollectionView inside a UIscrollview.
The size of the scrollview is 100x100 and that of collection view is 100x200;
And the content size of the scroll view is 100x200.
My problem is, the didSelectItemAtIndexPath is not getting called when I touch some cells (the cells out of the 100x100 rect).
User interaction of the cells are enabled. All cells are touchable when I increment the scrollview height equal to the height of the collection view.
Thanks in advance.
Because scrollView overlapped on Cell... Best way is add tap Gesture on UIScrollView such like,
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gestureAction:)];
[recognizer setNumberOfTapsRequired:1];
self.scrollViu.userInteractionEnabled = YES;
[self.scrollViu addGestureRecognizer:recognizer];
Add above code in cellForItemAtIndexPath method and Write gesture action method such like
-(void)gestureAction:(UITapGestureRecognizer *) sender
{
CGPoint touchLocation = [sender locationOfTouch:0 inView:self.YourCollectionViewName];
NSIndexPath *indexPath = [self.YourCollectionViewName indexPathForRowAtPoint:touchLocation];
NSLog(@"%d", indexPath.item);
}
Here in above gesture (action) method you can get indexPath as same as didSelectItemAtIndexPath.
My suggestion would be like this take a container whose size is same to collection view i.e 100x200 and add collection view on container and then add container on scroll view. This should fix this issue.
As i was having the similar issue as i was unable to scroll the collection view and select the collection view when making interaction on non visible part of scrollview.
Swift 4.0
Register gesture in viewDidLoad function
override func viewDidLoad() {
super.viewDidLoad()
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(didTapView(gesture:)))
view.addGestureRecognizer(tapGesture)
}
@objc func didTapView(gesture: UITapGestureRecognizer) {
view.endEditing(true)
let touchLocation:CGPoint = gesture.location(ofTouch: 0, in: self.collectionView)
let indexPath = self.collectionView.indexPathForItem(at: touchLocation)
if indexPath != nil {
let cell = self.collectionView.cellForItem(at: indexPath!)
if (cell?.isSelected)! {
//PREFORM DESELECT
} else {
//PREFORM SELECT HERE
}
}
}