最近写项目,遇到了一个vc下面有多个vc 指示器可以点击切换,也可也左右滑动进行切换,解除了JXCategoryTitleView 感觉很好用;
一般要求主vc遵循JXCategoryViewDelegate、JXCategoryListContainerViewDelegate两个协议 子vc遵循JXCategoryListContentViewDelegate
必写属性
//指示器view
@property (nonatomic, strong) JXCategoryTitleView *categoryView;
//正文view
@property (nonatomic, strong) JXCategoryListContainerView *listContainerView;
//vc数组 直接添加成vc可使得代码更加简洁
@property (strong, nonatomic) NSArray <__kindof UIViewController *> *controllers;
遵循协议 则相对应的代理方法必写
#pragma mark - JXCategoryListContentViewDelegate
- (UIView *)listView{return self.view;
}
#pragma mark - JXCategoryListContainerViewDelegate
- (id<JXCategoryListContentViewDelegate>)listContainerView:(JXCategoryListContainerView *)listContainerView initListForIndex:(NSInteger)index{__kindof UIViewController *vc = self.controllers[index];return vc;
}- (NSInteger)numberOfListsInlistContainerView:(JXCategoryListContainerView *)listContainerView {return self.controllers.count;
}
#pragma mark - JXCategoryViewDelegate
// 点击选中或者滚动选中都会调用该方法。适用于只关心选中事件,不关心具体是点击还是滚动选中的。
- (void)categoryView:(JXCategoryBaseView *)categoryView didSelectedItemAtIndex:(NSInteger)index { //侧滑手势处理self.navigationController.interactivePopGestureRecognizer.enabled = (index == 0);
}- (BOOL)categoryCollectionView:(JXCategoryCollectionView *)collectionView gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {if (collectionView.contentOffset.x <= 0) {if ([otherGestureRecognizer.delegate isKindOfClass:NSClassFromString(@"_FDFullscreenPopGestureRecognizerDelegate")]) {return YES;}}return NO;
}
若需要对指示器特殊处理 则对categoryview 自行写view
#pragma mark - 懒加载
- (JXCategoryTitleView *)categoryView{if (!_categoryView) {_categoryView = [[JXCategoryTitleView alloc] init];_categoryView.backgroundColor = [UIColor colorWithHexString:@"#F3F3F3"];_categoryView.layer.cornerRadius = 16;_categoryView.clipsToBounds = YES;_categoryView.delegate = self;_categoryView.titles = @[@"今天",@"7天",@"30天"];_categoryView.defaultSelectedIndex = 0;_categoryView.titleSelectedColor = [UIColor colorWithHexString:@"#161619"];_categoryView.titleColor = [UIColor colorWithHexString:@"#A3A3A3"];_categoryView.titleFont = [UIFont systemFontOfSize:12 weight:UIFontWeightMedium];JXCategoryIndicatorBackgroundView *backgroundView = [[JXCategoryIndicatorBackgroundView alloc] init];backgroundView.size = CGSizeMake(52, 28);backgroundView.indicatorColor = UIColor.whiteColor;backgroundView.indicatorHeight = 28;backgroundView.indicatorWidth = 52;backgroundView.indicatorCornerRadius = 14;_categoryView.indicators = @[backgroundView];_categoryView.listContainer = self.listContainerView;}return _categoryView;
}
其他代码
- (void)viewDidLoad{[super viewDidLoad];[self setupViews];
}- (void)setupViews{[self.view addSubview:self.categoryView];[self.categoryView mas_makeConstraints:^(MASConstraintMaker *make) {make.height.mas_equalTo(32);make.width.mas_equalTo(190);make.centerX.equalTo(self.view);make.top.equalTo(self.view).offset(23);}];[self.view addSubview:self.listContainerView];[self.listContainerView mas_makeConstraints:^(MASConstraintMaker *make) {make.left.right.mas_equalTo(self.view);make.top.mas_equalTo(self.categoryView.mas_bottom).offset(28);make.bottom.mas_equalTo(self.view.mas_bottom);}];
}- (NSArray<__kindof UIViewController *> *)controllers{return@[self.ADayVC,self.AWeakVC,self.AMonthVC,];
}
- (JXCategoryListContainerView *)listContainerView
{if (!_listContainerView) {_listContainerView = [[JXCategoryListContainerView alloc] initWithType:JXCategoryListContainerType_ScrollView delegate:self];}return _listContainerView;
}- (LMContributionListToadayVC *)ADayVC{if (!_ADayVC) {_ADayVC = [[LMContributionListToadayVC alloc] init];_ADayVC.uid = self.uid;_ADayVC.liveid = self.liveid;_ADayVC.isAnchor = self.isAnchor;}return _ADayVC;
}- (LMContributionListAWeakVC *)AWeakVC{if (!_AWeakVC) {_AWeakVC = [[LMContributionListAWeakVC alloc] init];_AWeakVC.uid = self.uid;_AWeakVC.liveid = self.liveid;_AWeakVC.isAnchor = self.isAnchor;}return _AWeakVC;
}- (LMContributionListAMonthVC *)AMonthVC{if (!_AMonthVC) {_AMonthVC = [[LMContributionListAMonthVC alloc] init];_AMonthVC.uid = self.uid;_AMonthVC.liveid = self.liveid;_AMonthVC.isAnchor = self.isAnchor;}return _AMonthVC;
}