BSScrollViewEdgePop
https://blog.csdn.net/qq_17190231/article/details/84201956
使用场景
当我们用UIScrollView类似控件时候往往会遇到个问题,它的滑动和系统侧滑返回有冲突导致系统侧滑返回不好使。这时候我们直接使用BSScrollViewEdgePop可以很快的解决问题。
使用方法
- pod ‘BSScrollViewEdgePop’
- 代码示例
#import <UIScrollView+BSScrollViewEdgePop.h>
[self.scrollView setEdgePop:self];
- 1
- 2
效果
代码分析
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
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 这里是在window上添加个手势,这个手势的目的是为了响应侧滑返回。其中target和handleNavigationTransition即使系统侧滑需要执行的方法。这里我们必须用拖拽手势,因为该方法内部要知道滑动的偏移量。
- 最后当销毁scrollVeiw时候在移除这个手势
侧滑返回处理
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
if ([self panBack:gestureRecognizer]) {
return NO;
}
return YES;
}
- 这个方法会响应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;
}
- location_X是定义侧滑偏移量
- translationInView方法获得侧滑的方向
- locationInView方法为了获得手指滑动的区域
- 这样判断方向是向右并且是在侧滑区域内返回YES,否则返回NO