UIPanGestureRecognizer 左右滑动卡片,下拉滑动就是下拉刷新

做项目遇到手势冲突问题.

  为了实现右滑划走,左滑划过来.下拉是下拉刷新功能.思考良久.我的卡片在tableview的header头视图上,所以左右滑能实现,卡片手势上下滑,没法带动下拉刷新.因为下拉刷新放在tableview上.

其实tableview继承UIScrollView自带默认手势

@property(nonatomic, readonly) UIPanGestureRecognizer *panGestureRecognizer NS_AVAILABLE_IOS(5_0);

    开始通过获取卡片手势位移坐标

- (CGPoint)translationInView:(nullable UIView *)view; 

 

   判断左右上下滑动方向.做事件分发处理.改变tableview的contentoffset但是发现tableview的下拉刷新通过判断 dragging,但是

 

@property(nonatomic,readonly,getter=isDragging)     BOOL dragging; 是只读的

   所以最后还是从手势上面下功夫,通过查博客,看API文档,发现手势的代理方法,部分如下:

 

 

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer;

询问一个手势接收者是否应该开始解释执行一个触摸接收事件 

 

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;

 一个控件的手势识别后是否阻断手势识别继续向下传播,默认返回NO;如果为YES,响应者链上层对象触发手势识别后,如果下层对象也添加了手势并成功识别也会继续执行,否则上层对象识别后则不再继续传播;简单来说返回false响应一个,返回true 都响应

 

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer NS_AVAILABLE_IOS(7_0);

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer NS_AVAILABLE_IOS(7_0);

上面两个大致意思,每个视图都可以设置到,可以在两个之间设置交互.返回yes当前手势有效,false当前手势无效(第一条是这样)

 

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch;

询问delegate是否允许手势接收者接收一个touch对象 

 // 返回YES,则允许对这个touch对象审核,NO,则不允许。  

// 这个方法在touchesBegan:withEvent:之前调用,为一个新的touch对象进行调用 

 

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceivePress:(UIPress *)press;

   是否允许接受按得手势

 

最后解决办法是

 

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{

    NSLog(@"%@",[[otherGestureRecognizer view] class]);

    if ([[otherGestureRecognizer view] isKindOfClass:[CarCardView class]]) {

        NSLog(@"%.2f,%.2f",[(ZYZPanGestureRecognizer*)otherGestureRecognizer translationInView:self].y,[(ZYZPanGestureRecognizer*)otherGestureRecognizer translationInView:self].x);

        if (fabs([(ZYZPanGestureRecognizer*)otherGestureRecognizer translationInView:self].y) > fabs([(ZYZPanGestureRecognizer*)otherGestureRecognizer translationInView:self].x)) { //上下拉

            [self cancelOtherGestureRecognizer:otherGestureRecognizer];

           // [self cancelOtherGestureRecognizer:gestureRecognizer];

        }else{

            //[self cancelOtherGestureRecognizer:gestureRecognizer];

        }

    }

    return NO;// 同时响应,false就是响应一个

}

 

//取消当前手势响应事件

 

- (void)cancelOtherGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer

{

    ZYZPanGestureRecognizer *ges = (ZYZPanGestureRecognizer*)otherGestureRecognizer;

    NSSet *touchs = [ges.event touchesForGestureRecognizer:otherGestureRecognizer];

    [otherGestureRecognizer touchesCancelled:touchs withEvent:ges.event];

}

 

 让多个手势同时,只响应一个手势,判断位移,当前卡片手势左右位移,只显示一个没问题.当手势显示下拉,则取消当前手势的响应.默认取到手势响应链的上一层实现了,下拉刷新.

参考链接:

http://blog.csdn.net/namehzf/article/details/7424882

http://www.cnblogs.com/kenshincui/p/3950646.html

有不对的地方欢迎指出

 

 

 

 

 

发布了29 篇原创文章 · 获赞 11 · 访问量 4万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览