起因:公司有个项目包含左边侧滑栏,内容是一个CollectionView嵌套多个View
侧滑栏用的是MMDrawerController,感谢!
问题出现在边缘侧滑的时候想调出侧滑栏,手势与Collectiuonview的pan手势冲突了。
网上google了很多答案,看到一个朋友给了一个思路。在中间视图左侧加一个透明的customView,手势在customView才会触发侧滑抽屉事件,万幸MMDrawerController的作者提供了自定义手势响应视图的block,不能再感谢。如下:
myDrawerController setGestureShouldRecognizeTouchBlock:^BOOL(MMDrawerController *drawerController, UIGestureRecognizer *gesture, UITouch *touch) {
BOOLshouldRecognizeTouch =NO;
if (drawerController.openSide == MMDrawerSideNone && [gesture isKindOfClass:[UIPanGestureRecognizer class]]) {
UIView* customView = [drawerController.centerViewController myCustomSubview];
CGPointlocation = [touchlocationInView:customView];
shouldRecognizeTouch = (CGRectContainsPoint(customView.bounds, location)
}
returnshouldRecognizeTouch;
}];
在此之前需要注意的是,MMDrawerController需要设置侧滑模式为>MMOpenDrawerGestureModeCustom
[self.drawerControllersetOpenDrawerGestureModeMask:MMOpenDrawerGestureModeCustom];
自己项目的代码:
[self.drawerControllersetGestureShouldRecognizeTouchBlock:^BOOL(MMDrawerController*drawerController,UIGestureRecognizer*gesture,UITouch*touch) {
BOOL shouldRecognizeTouch =NO;
if(drawerController.openSide==MMDrawerSideNone&&
[gesture isKindOfClass:[UIPanGestureRecognizer class]]){
UIView*customView = [mainVC custumview];
CGPoint location = [touch locationInView:customView];
shouldRecognizeTouch = (CGRectContainsPoint(customView.bounds, location));
}
returnshouldRecognizeTouch;
}];
customView为中间视图的一个公开的属性,内部设定为:
//用于触发侧滑栏的view
CGFloat custH = kContentY;
self.custumview= [[UIView alloc] initWithFrame:CGRectMake(0, custH,15,kScreenHeight- custH)];
[self.view addSubview:self.custumview];
我的项目是只有中间视图的根视图才能触发侧滑栏
,如果想在全局都可以触发侧滑栏,可以直接把customView直接加载window上
之前用的别的第三方的侧滑框架大多无法解决我的项目中手势逻辑的问题,现在完美解决~
有类似项目的同学可以借鉴一下,不对之处请轻拍~
欧了~
网友评论