ios – 如何从UICollectionViewCell将pan手势传递给UICollectionVIew?

前端之家收集整理的这篇文章主要介绍了ios – 如何从UICollectionViewCell将pan手势传递给UICollectionVIew?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个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作为其手势识别器时它不起作用,并且它实际上停止所有滚动.

解决方法

而不是将UIPanGestureRecognizer附加到每个单元格(这会降低性能),而是将UIPanGestureRecognizer添加到UICollectionView,当发生平移手势时,使用 locationInView获取平移开始的UICollectionView中的点,然后是 indexPathForItemAtPoint,它将返回索引路径对于你应该动画的单元格.

这样,您将只有一个手势识别器(好!)用于整个集合视图,同时还在视图控制器中保持控制(如您所愿) – 双赢!

使用此解决方案,在视图控制器中,您将实现gestureRecognizer:shouldReceiveTouch:,获取给定的gestureRecognizer,确保它是您的UIPanGestureRecognizer并使用其translationInView:方法来确定转换是在X轴还是Y轴上.使用该信息来决定是否要返回YES或NO.例如:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
  if([gestureRecognizer isEqual:myTapGesture]) {
    CGPoint point = [gestureRecognizer translationInView:self.collectionView];
    if(point.x != 0) { //adjust this condition if you want some leniency on the X axis
      //The translation was on the X axis,i.e. right/left,//so this gesture recognizer shouldn't do anything about it
      return NO;
    }
  }
  return YES;
}

猜你在找的iOS相关文章