zoukankan      html  css  js  c++  java
  • iOS中UIView的Pan手势和UIScrollView滚动手势的冲突解决方案

    tableView滚动视图中包含播放器窗口,播放器控制层包含了上下滑动手势调节音量和屏幕亮度功能,与tableView的上下滚动手势冲突。导致播放器窗口上下滚动时,tableView不滚动问题,影响用户体验。

    因此本内容主要是为了处理UIScrollView的子视图上添加UIPanGestureRecognizer后,导致上下滑动该子视图时UIScrollView无法跟随上下滚动的情况。

    新建 UIPanGestureRecognizer的子类 

    @interface ZFPanGestureRecognizer : UIPanGestureRecognizer
    
    /**
     是否自动锁住手势方向,默认为NO
     如果设置成YES,则在手势开始时根据xy的偏移量大小来确定最可能的滑动方向,并在手势有效期内保持这个方向上的有效性
     */
    @property (nonatomic, assign) BOOL  autoLock;
    
    @end

    然后实现touchesMoved方法,可以在该方法中自定义,是否支持滑动效果。

    #import "ZFPanGestureRecognizer.h"
    
    typedef NS_ENUM(NSInteger, IXPanDirection) {
        IXPanDirectionAny = 0,
        IXPanDirectionHor,
        IXPanDirectionVer
    };
    
    @interface ZFPanGestureRecognizer ()
    
    @property (nonatomic, assign) IXPanDirection    direction;
    @property (nonatomic, assign) CGPoint   beginP;
    
    @end
    
    @implementation ZFPanGestureRecognizer
    
    -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
        
        [super touchesBegan:touches withEvent:event];
        if (_autoLock) {
            _direction = IXPanDirectionAny;
            _beginP = [[touches anyObject] locationInView:self.view];
        }
    }
    
    -(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
        
        [super touchesMoved:touches withEvent:event];
        if (!_autoLock && _direction != IXPanDirectionAny) return;
        
        CGPoint nowPoint = [[touches anyObject] locationInView:self.view];
        if (_direction == IXPanDirectionAny) {
            
            if (fabs(nowPoint.x - _beginP.x) > fabs(nowPoint.y - _beginP.y)) {
                _direction = IXPanDirectionHor;
            } else {
                if (_beginP.x > self.view.bounds.size.width*0.5) {
                    _direction = IXPanDirectionVer;
                    self.state = UIGestureRecognizerStateFailed;
                }
            }
        }
    }

    使用我们的自定义pan手势

    ZFPanGestureRecognizer * ixPan = [[ZFPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)]; 
    ixPan.autoLock
    = YES;
    [view addGestureRecognizer:ixPan];
  • 相关阅读:
    WebSocket
    Spring Web MVC
    对象关系映射(ORM)数据访问
    使用JDBC进行数据访问
    【Mysql】【基础综合题解】大一下学期数据库综合训练题解
    【Mysql】【解决问题】[Err] 1055
    【JavaSE】【IO流】复制单级或多级文件夹
    C#使用FFmpeg的总结
    C#一个IM系统简单总结
    重新开启博客
  • 原文地址:https://www.cnblogs.com/dashengios/p/14080616.html
Copyright © 2011-2022 走看看