zoukankan      html  css  js  c++  java
  • UIScrollView的左右滑动和侧滑手势冲突的解决办法

    转载自:https://blog.csdn.net/kst_123/article/details/77762811

    当ViewController中添加了一个全屏的UIScrollView的时候,UIScrollView的左滑手势会和系统的左滑返回冲突,系统的左滑返回将会失效。解决办法如下:

    自定义一个CustomScrollView继承于UIScrollView,然后重写一下gestureRecognizerShouldBegin方法。

    左边侧滑:

    - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
    {
        CGPoint velocity = [(UIPanGestureRecognizer *)gestureRecognizer velocityInView:self];
        CGPoint location = [gestureRecognizer locationInView:self];
        
        if (velocity.x > 0.0f&&(int)location.x%(int)[UIScreen mainScreen].bounds.size.width<60) {
            return NO;
        }
        return YES;
    }

    右边侧滑:

    - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
    {
        CGPoint velocity = [(UIPanGestureRecognizer *)gestureRecognizer velocityInView:self];
        CGPoint location = [gestureRecognizer locationInView:self];
        
        if (velocity.x > 0.0f&&(int)location.x%(int)[UIScreen mainScreen].bounds.size.width>[UIScreen mainScreen].bounds.size.width-60) {
            return NO;
        }
        return YES;
    }

    奉上一个自定义的UIScrollView的代码:

    DXCustomScrollView.h

    #import <UIKit/UIKit.h>
    
    @interface DXCustomScrollView : UIScrollView
    
    @end

    DXCustomScrollView.m

    #import "DXCustomScrollView.h"
    
    @interface DXCustomScrollView()
    
    @end
    
    @implementation DXCustomScrollView
    
    - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
    {
        CGPoint velocity = [(UIPanGestureRecognizer *)gestureRecognizer velocityInView:self];
        CGPoint location = [gestureRecognizer locationInView:self];
        
        if (velocity.x > 0.0f&&(int)location.x%(int)[UIScreen mainScreen].bounds.size.width<60) {
            return NO;
        }
        return YES;
    }
    
    @end
  • 相关阅读:
    免费素材:推荐最新的免费图标字体Sosa
    如何选择Javascript模板引擎(javascript template engine)?
    名片设计最佳实践和技巧分享
    帮助你生成翻页效果的jQuery插件 bookblock
    网页网站收集
    main xml信息
    c# winrar 打包
    c# 压缩文件
    前端工程师
    fash 3D 游戏
  • 原文地址:https://www.cnblogs.com/wobuyayi/p/9502714.html
Copyright © 2011-2022 走看看