前提准备:
- 集成一个MMDrawerController
- 重现这种状况
/* 以下为集成一个MMDrawerController */
HomeViewController *home = [[HomeViewController alloc] init];
// 使用MMDrawerController
LeftMenuViewController *leftVC = [[LeftMenuViewController alloc] init];
self.drawerController = [[MMDrawerController alloc] initWithCenterViewController:home leftDrawerViewController:leftVC];
// 展开宽度
_drawerController.maximumLeftDrawerWidth = SRN_WIDTH * 0.8;
// 打开手势
[_drawerController setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeAll];
[_drawerController setCloseDrawerGestureModeMask:MMCloseDrawerGestureModeAll];
self.window.rootViewController = _drawerController;
接下来添加三个控制器的View到主控器上
UIScrollView *view = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SRN_WIDTH, SRN_HEIGHT - HEIGHT)];
view.contentSize = CGSizeMake(SRN_WIDTH * 3, 0);
// 去掉滚动条
view.showsVerticalScrollIndicator = NO;
view.showsHorizontalScrollIndicator = NO;
// 设置分页
view.pagingEnabled = YES;
// 设置代理
view.delegate = self;
// 去掉弹簧效果
view.bounces = NO;
CGFloat height = SRN_HEIGHT - 49;
// 添加子视图
MainViewController *main = [[MainViewController alloc] init];
main.view.frame = CGRectMake(0, 0, SRN_WIDTH, SRN_HEIGHT - HEIGHT);
main.view.height = height;
[self addChildViewController:main];
[view addSubview:main.view];
BestHotViewController *hot = [[BestHotViewController alloc] init];
hot.view.frame = CGRectMake(0, 0, SRN_WIDTH, SRN_HEIGHT - HEIGHT);
hot.view.x = SRN_WIDTH;
hot.view.height = height;
[self addChildViewController:hot];
[view addSubview:hot.view];
CategoryViewController *cate = [[CategoryViewController alloc] init];
cate.view.frame = CGRectMake(0, 0, SRN_WIDTH, SRN_HEIGHT - HEIGHT);
cate.view.x = SRN_WIDTH * 2;
cate.view.height = height;
[self addChildViewController:cate];
[view addSubview:cate.view];
[self.view addSubview:view];
self.scrollView = view;
分析: MMDC 中也使用这个手势 UIPanGestureRecognizer
, UIScrollView中手势拦截了 MMDC手势的生效.
解决办法:
@implementation XTScrollView
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
UIPanGestureRecognizer *pan = (UIPanGestureRecognizer *)gestureRecognizer;
if ([pan translationInView:self].x > 0.0f && self.contentOffset.x == 0.0f) {
// 这里返回 NO
return NO;
}
}
return [super gestureRecognizerShouldBegin:gestureRecognizer];
}
@end
重新初始化ScrollView
XTScrollView *view = [[XTScrollView alloc] initWithFrame:CGRectMake(0, 0, SRN_WIDTH, SRN_HEIGHT - HEIGHT)];
最后感谢您的阅读, 如果感觉有帮助可以关注我 和我一起学习!
我是夏天, 暖暖的夏天
End
原文链接
文章取自我的博客 暖暖的夏天