I have a UICollectionView
implementing a grid-based layout of custom UICollectionViewCells
. To allow cells to respond to dragging, I individually add a UIPanGestureRecognizer
to each cell.
The UICollectionView
still scrolls (horizontally) when I touch down and swipe left/right starting at points between cells, but as long as the pan gesture recognizer is added to a cell, it seems like the CollectionView
refuses to scroll when I start my swipe tapping within a cell.
Right now, I separate horizontal left/right drags from vertical up/down drags, so there should not be any conflict between dragging cells out (vertical swipes) and scrolling the CollectionView
(Horizontal swipes). In this case, how can I pass the swipe to the collection/scroll view so it knows to scroll like normal? It's really annoying to have to start on the boundary or spacing between cells.
Once I remove the pan gesture from a cell, scrolling works as normal no matter if I start swiping on a cell or between cells.
EDIT:Desired pan gesture behavior posted below as current code
// Handle pans by detecting swipes:
-(void) handlePan:(UIPanGestureRecognizer*)recognizer
{
// Calculate touch location
CGPoint touchXY = [recognizer locationInView:masterWindowView];
// Handle touch
if (recognizer.state == UIGestureRecognizerStateBegan)
{
gestureWasHandled = NO;
pointCount = 1;
startPoint = touchXY;
}
if (recognizer.state == UIGestureRecognizerStateChanged)
{
++pointCount;
// Calculate whether a swipe has occurred
float dX = deltaX(touchXY, startPoint);
float dY = deltaY(touchXY, startPoint);
BOOL finished = YES;
if ((dX > kSwipeDragMin) && (ABS(dY) < kDragLimitMax)) {
touchType = TouchSwipeLeft;
NSLog(@"LEFT swipe detected");
[recognizer requireGestureRecognizerToFail:recognizer];
//[masterScrollView handlePan]
}
else if ((dX < -kSwipeDragMin) && (ABS(dY) < kDragLimitMax)) {
touchType = TouchSwipeRight;
NSLog(@"RIGHT swipe detected");
[recognizer requireGestureRecognizerToFail:recognizer];
}
else if ((dY > kSwipeDragMin) && (ABS(dX) < kDragLimitMax)) {
touchType = TouchSwipeUp;
NSLog(@"UP swipe detected");
}
else if ((dY < -kSwipeDragMin) && (ABS(dX) < kDragLimitMax)) {
touchType = TouchSwipeDown;
NSLog(@"DOWN swipe detected");
}
else
finished = NO;
// If unhandled and downward, produce a new draggable view
if (!gestureWasHandled && finished && (touchType == TouchSwipeDown))
{
[self.delegate cellBeingDragged:self];
dragView.center = touchXY;
dragView.hidden = NO;
dragView.backgroundColor = [UIColor clearColor];
masterScrollView.scrollEnabled = NO; // prevent user from scrolling during
gestureWasHandled = YES;
}
else if (gestureWasHandled)
{
// allow continued dragging after detection
dragView.center = touchXY;
}
}
if (recognizer.state == UIGestureRecognizerStateEnded)
{
// ensure that scroll view returns to scrollable
if (gestureWasHandled) {
[self.delegate cell:self dragEndedAt:touchXY];
}
}
}
// Allow simultaneous recognition
-(BOOL) gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer*)otherGestureRecognizer
{
return YES;
}
This code works when given to each individual cell. It does NOT work when attached to the UICollectionView as its gesture recognizer, and it in fact stops all scrolling.