【发布时间】:2014-07-09 16:14:37
【问题描述】:
我有一个UICollectionView 实现自定义UICollectionViewCells 的基于网格的布局。为了让单元格响应拖动,我分别为每个单元格添加了UIPanGestureRecognizer。
UICollectionView 在我从单元格之间的点开始向下和向左/向右滑动时仍会滚动(水平),但只要将平移手势识别器添加到单元格,CollectionView 似乎拒绝当我在单元格内开始滑动点击时滚动。
现在,我将水平左/右拖动与垂直向上/向下拖动分开,因此将单元格拖出(垂直滑动)和滚动CollectionView(水平滑动)之间不应该有任何冲突。在这种情况下,如何将滑动传递给集合/滚动视图,以便它知道像正常一样滚动?必须从单元格之间的边界或间距开始,真的很烦人。
从单元格中移除平移手势后,无论我开始在单元格上还是在单元格之间滑动,滚动都会正常工作。
编辑:下面发布为当前代码的所需平移手势行为
// 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;
}
此代码适用于每个单独的单元格。当附加到 UICollectionView 作为其手势识别器时,它不起作用,实际上它会停止所有滚动。
【问题讨论】:
-
代码在哪里?您的手势识别器代码应该存在于您的视图控制器中。
gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer方法允许同时运行两个不同的手势,不是一个手势被两个不同的视图识别 - 这是你想要的吗?
标签: ios objective-c foundation