美文网首页
iOS-解决手势冲突

iOS-解决手势冲突

作者: malgee | 来源:发表于2018-12-12 20:17 被阅读11次

最近的项目中里面有一个需求,UIScrollerView 上面放了一个横向滑动的UICollectionView, 在UICollectionView滑动到最右侧的时候,底部的UIScrollerView响应滑动手势,继续向右切换,

Apple 提供解决双重手势的方法


// return YES to allow both to recognize simultaneously. the default implementation returns NO (by default no two gestures can be recognized simultaneously)

// 翻译过来就是:返回YES以允许两者同时识别。 默认实现返回NO(默认情况下,不能同时识别两个手势), 但是有些时候不需要都识别,需要达到特定条件才可以识别滑动收拾,就需要特殊处理
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer

说一下我的问题的解决方案:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    
    if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
        
        UIPanGestureRecognizer *panGes = (UIPanGestureRecognizer *)gestureRecognizer;
        
        CGPoint velocity = [panGes velocityInView:panGes.view];
        
        if ([gestureRecognizer.view isKindOfClass:[UIScrollView class]]) {
            
            // 使用 velocity.x < 0 判断向右滑动的时候
            if (gestureRecognizer.state == UIGestureRecognizerStateBegan && self.contentOffset.x >= floor(maxDistance) && velocity.x < 0) {
                
                return YES;
            }   
        }
    }
    return NO;
}

相关文章

网友评论

      本文标题:iOS-解决手势冲突

      本文链接:https://www.haomeiwen.com/subject/hwophqtx.html