iOS 手势冲突(JXCategoryView 滚动手势与侧滑)
此处我使用的是 FDFullscreenPopGesture
解决后的效果图
JXCategoryListContainerViewDelegate 提供了一个可选的协议方法:
/**
返回自定义UIScrollView或UICollectionView的Class
某些特殊情况需要自己处理UIScrollView内部逻辑。比如项目用了FDFullscreenPopGesture,需要处理手势相关代理。
@param listContainerView JXCategoryListContainerView
@return 自定义UIScrollView实例
*/
- (Class)scrollViewClassInlistContainerView:(JXCategoryListContainerView *)listContainerView;
看到这个我就知道怎么弄了,咱们可以自定义一个自定义 UICollectionView 在里面实现手势的控制,让它兼容多种手势共存
1、新建 HJCollectionView 继承自 UICollectionView, 添加UIGestureRecognizerDelegate 代理
HJCollectionView.h
@interface HJCollectionView : UICollectionView<UIGestureRecognizerDelegate>
@end
2 .HJCollectionView.m
#import "HJCollectionView.h"
@implementation HJCollectionView
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
if (self.contentOffset.x <= 0) {
return YES;
}
return NO;
}
@end
3. 在使用 JXCategoryView 的控制器中实现 JXCategoryListContainerViewDelegate
#import "FAMineCouponVC.h"
#import "FAMyCouponVC.h"
#import <HJUIKit/HJCollectionView.h>
#import <JXCategoryView/JXCategoryView.h>
@interface FAMineCouponVC ()<JXCategoryViewDelegate, JXCategoryListContainerViewDelegate>
@property (nonatomic, strong) NSArray *titles;
@property (nonatomic, strong) JXCategoryTitleView *categoryView;
@property (nonatomic, strong) JXCategoryListContainerView *listContainerView;
@end
@implementation FAMineCouponVC
// MARK: - 1.interface
// MARK: - 2.lift cycle
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @"优惠劵";
[self.view addSubview:self.categoryView];
[self.categoryView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.top.right.equalTo(self.view);
make.height.mas_equalTo(40);
}];
[self.view addSubview:self.listContainerView];
[self.listContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.categoryView.mas_bottom);
make.left.bottom.right.equalTo(self.view);
}];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
// 处于第一个item的时候,才允许屏幕边缘手势返回
self.navigationController.interactivePopGestureRecognizer.enabled = (self.categoryView.selectedIndex == 0);
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
// 离开页面的时候,需要恢复屏幕边缘手势,不能影响其他页面
self.navigationController.interactivePopGestureRecognizer.enabled = YES;
}
// MARK: - 3.private methods
// MARK: - 4.event response
// MARK: - 5.getter
// 分页菜单视图
- (NSArray *)titles {
return @[@"未使用", @"已使用", @"已过期"];
}
- (JXCategoryTitleView *)categoryView {
if (!_categoryView) {
JXCategoryTitleView * newView = [[JXCategoryTitleView alloc] init];
newView.titleColor = RGBC(85);
newView.titleSelectedColor = KTextColor;
newView.delegate = self;
newView.titles = self.titles;
// !!!: 将列表容器视图关联到 categoryView
newView.listContainer = self.listContainerView;
JXCategoryIndicatorLineView * lineView = [[JXCategoryIndicatorLineView alloc] init];
lineView.indicatorColor = KTextColor;
lineView.indicatorWidth = 27;
newView.indicators = @[lineView];
_categoryView = newView;
}
return _categoryView;
}
// 列表容器视图
- (JXCategoryListContainerView *)listContainerView {
if (!_listContainerView) {
JXCategoryListContainerView * newView = [[JXCategoryListContainerView alloc] initWithType:JXCategoryListContainerType_CollectionView delegate:self];
_listContainerView = newView;
}
return _listContainerView;
}
// MARK: - JXCategoryViewDelegate
- (void)categoryView:(JXCategoryBaseView *)categoryView didSelectedItemAtIndex:(NSInteger)index {
NSLog(@"%@", NSStringFromSelector(_cmd));
// 侧滑手势处理
self.navigationController.interactivePopGestureRecognizer.enabled = (index == 0);
}
// MARK: - JXCategoryListContainerViewDelegate
- (NSInteger)numberOfListsInlistContainerView:(JXCategoryListContainerView *)listContainerView {
return self.titles.count;
}
// MARK: JXCategoryListContentViewDelegate
- (id<JXCategoryListContentViewDelegate>)listContainerView:(JXCategoryListContainerView *)listContainerView initListForIndex:(NSInteger)index {
FAMyCouponVC * list = [[FAMyCouponVC alloc] init];
list.status = index;
return list;
}
- (Class)scrollViewClassInlistContainerView:(JXCategoryListContainerView *)listContainerView {
return [HJCollectionView class];
}
@end
4.在子viewController中实现 jx的代理方法,也就是我上面的FAMyCouponVC
// MARK: JXCategoryListContentViewDelegate
- (UIView *)listView {
return self.view;
}