BSScrollViewEdgePop

栏目: IOS · 发布时间: 6年前

内容简介:当我们用UIScrollView类似控件时候往往会遇到个问题,它的滑动和系统侧滑返回有冲突导致系统侧滑返回不好使。这时候我们直接使用BSScrollViewEdgePop可以很快的解决问题。

当我们用UIScrollView类似控件时候往往会遇到个问题,它的滑动和系统侧滑返回有冲突导致系统侧滑返回不好使。这时候我们直接使用BSScrollViewEdgePop可以很快的解决问题。

使用方法

  1. pod 'BSScrollViewEdgePop'
  2. 代码示例
#import <UIScrollView+BSScrollViewEdgePop.h>
[self.scrollView setEdgePop:self];
复制代码

效果

BSScrollViewEdgePop

代码分析

setEdgePop程序入口

- (void)setEdgePop:(UIViewController *)vc{
    id target = vc.navigationController.interactivePopGestureRecognizer.delegate;
    self.panGr = [[UIPanGestureRecognizer alloc] initWithTarget:target action:NSSelectorFromString(@"handleNavigationTransition:")];
    [[UIApplication sharedApplication].keyWindow addGestureRecognizer:self.panGr];
}

- (void)dealloc{
    [[UIApplication sharedApplication].keyWindow removeGestureRecognizer:self.panGr];
}
复制代码
  1. 这里是在window上添加个手势,这个手势的目的是为了响应侧滑返回。其中target和handleNavigationTransition即使系统侧滑需要执行的方法。这里我们必须用拖拽手势,因为该方法内部要知道滑动的偏移量。
  2. 最后当销毁scrollVeiw时候在移除这个手势

侧滑返回处理

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    
    if ([self panBack:gestureRecognizer]) {
        return NO;
    }
    return YES;
}
复制代码
  1. 这个方法会响应scrollVeiw的滑动,即scrollVeiw每次滑动时候都会调用该方法。在该方法内部利用panBack方法判断是否scrollView滑动的区域是侧滑返回的区域,如果是就返回NO,这样事件将会向下一级传递,即之前我们添加的手势将会执行,这样就实现了侧滑返回。当scrollView滑动的区域不是侧滑返回的区域就返回YES这样scrollView的滑动事件继续执行。

scrollVeiw滑动区域判断

- (BOOL)panBack:(UIGestureRecognizer *)gestureRecognizer {
    
    int location_X =0.15*BSEdgePopIPHONE_W;
    
    if (gestureRecognizer ==self.panGestureRecognizer) {
        UIPanGestureRecognizer *pan = (UIPanGestureRecognizer *)gestureRecognizer;
        CGPoint point = [pan translationInView:self];
        UIGestureRecognizerState state = gestureRecognizer.state;
        if (UIGestureRecognizerStateBegan == state ||UIGestureRecognizerStatePossible == state) {
            CGPoint location = [gestureRecognizer locationInView:self];
            
            int temp1 = location.x;
            int temp2 = BSEdgePopIPHONE_W;
            NSInteger XX = temp1 % temp2;
            if (point.x >0 && XX < location_X) {
                return YES;
            }
        }
    }
    return NO;
    
}
复制代码
  1. location_X是定义侧滑偏移量
  2. translationInView方法获得侧滑的方向
  3. locationInView方法为了获得手指滑动的区域
  4. 这样判断方向是向右并且是在侧滑区域内返回YES,否则返回NO

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Nature of Code

The Nature of Code

Daniel Shiffman / The Nature of Code / 2012-12-13 / GBP 19.95

How can we capture the unpredictable evolutionary and emergent properties of nature in software? How can understanding the mathematical principles behind our physical world help us to create digital w......一起来看看 《The Nature of Code》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具