I have a UICollectionView grid implementation based on a UICollectionViewCells table. To allow cells to respond to drag and drop, I separately add a UIPanGestureRecognizer to each cell.
UICollectionView still scrolls (horizontally) when I touch and scroll left / right, starting from the points between the cells, but as long as the pan gestures recognizer is added to the cell, CollectionView seems to refuse to scroll when I start using swipe in the cell.
Right now, I am separating horizontal left / right drag from vertical drag up / down, so there should be no conflict between dragging cells (vertical scroll) and CollectionView scroll (horizontal scroll). In this case, how can I pass the swipe to the collection / scroll view so that it can scroll as usual? It is really annoying to start with the border or distance between cells.
As soon as I remove the pan gestures from the cell, scrolling works fine, regardless of whether I start to start the cell or between cells.
EDIT: The desired behavior of gestures in the form is carried below as the 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 it is provided to each individual cell. It does NOT work when it is connected to the UICollectionView as a gesture recognizer, and it actually stops scrolling.
ios objective-c foundation
Cindeselia
source share