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
  • 相关阅读:
    Gradle gitignore Gradle 模式 上传SVN 要忽略的文件
    加速Android Studio/Gradle构建
    JAVA unicode转换成中文
    进程与线程
    Flask快速入门
    tensorflow入门指南
    BP神经网络与Python实现
    文档数据库MongoDB
    Python虚拟环境virtualenv
    Python爬虫框架Scrapy
  • 原文地址:https://www.cnblogs.com/wobuyayi/p/9502714.html
Copyright © 2011-2022 走看看