JXCategoryView的使用

在这里插入图片描述

实现效果

在这里插入图片描述

JXCategoryTitleView 使用示例

//遵守协议
@interface ViewController () <JXCategoryViewDelegate,JXCategoryListContainerViewDelegate>
@property (nonatomic ,retain) JXCategoryTitleView *categoryView;
@property (nonatomic ,retain) JXCategoryListContainerView *listContainerView;
@property (weak, nonatomic) IBOutlet UIView *topView;
@property (weak, nonatomic) IBOutlet UIView *midView;
@end
	//初始化 JXCategoryTitleView:
    self.categoryView = [[JXCategoryTitleView alloc] initWithFrame:CGRectMake(0, 0, self.topView.bounds.size.width, 50)];
    //代理
    self.categoryView.delegate = self;
    //配置属性
    self.categoryView.titles = @[@"螃蟹", @"麻辣小龙虾", @"苹果"];
    //title的颜色是否渐变过渡 默认NO
    self.categoryView.titleColorGradientEnabled = YES;
    //下划线
    JXCategoryIndicatorLineView *lineView = [[JXCategoryIndicatorLineView alloc] init];
    lineView.indicatorColor = [UIColor redColor];
    lineView.indicatorWidth = JXCategoryViewAutomaticDimension;
    //配置titleView的下划线
    self.categoryView.indicators = @[lineView];
    [self.topView addSubview:self.categoryView];
  • 代理方法
//实现 JXCategoryViewDelegate 代理(可选)
// 点击选中或者滚动选中都会调用该方法。适用于只关心选中事件,不关心具体是点击还是滚动选中的。
 - (void)categoryView:(JXCategoryBaseView *)categoryView didSelectedItemAtIndex:(NSInteger)index;
// 点击选中的情况才会调用该方法
 - (void)categoryView:(JXCategoryBaseView *)categoryView didClickSelectedItemAtIndex:(NSInteger)index;
// 滚动选中的情况才会调用该方法
 - (void)categoryView:(JXCategoryBaseView *)categoryView didScrollSelectedItemAtIndex:(NSInteger)index;
// 正在滚动中的回调
 - (void)categoryView:(JXCategoryBaseView *)categoryView scrollingFromLeftIndex:(NSInteger)leftIndex toRightIndex:(NSInteger)rightIndex ratio:(CGFloat)ratio;

JXCategoryListContainerView封装类使用示例

JXCategoryListContainerView 是对列表视图高度封装的类,具有以下优点:

  • 相对于直接使用 UIScrollView 自定义,封装度高、代码集中、使用简单;
  • 列表懒加载:当显示某个列表的时候,才进行列表初始化。而不是一次性加载全部列表,性能更优;
  • 支持列表的 willAppear、didAppear、willDisappear、didDisappear 生命周期方法调用;
	//初始化JXCategoryListContainerView并关联到categoryView
    //设置下面的
    self.listContainerView = [[JXCategoryListContainerView alloc] initWithType:JXCategoryListContainerType_ScrollView delegate:self];
    //记得配置Frame
    self.listContainerView.frame = CGRectMake(0, 0, self.midView.frame.size.width, self.midView.frame.size.height);
    [self.midView addSubview:self.listContainerView];
    // 关联到categoryView
    self.categoryView.listContainer = self.listContainerView;
  • 实现 JXCategoryListContainerViewDelegate 代理方法:
// 返回列表的数量
 - (NSInteger)numberOfListsInlistContainerView:(JXCategoryListContainerView *)listContainerView {
    
    
    return self.titles.count;
}
// 根据下标 index 返回对应遵守并实现 `JXCategoryListContentViewDelegate` 协议的列表实例
 - (id<JXCategoryListContentViewDelegate>)listContainerView:(JXCategoryListContainerView *)listContainerView initListForIndex:(NSInteger)index {
    
    
 	//这里需要返回一个遵守JXCategoryListContentViewDelegate的对象 例如自己写一个JJVC
    JJViewController *vc = [[JJViewController alloc]init];
    return vc;
}

JXCategoryListContentViewDelegate

  • 自己创建一个遵守 JXCategoryListContentViewDelegate 的类, 不管是继承 UIView 还是 UIViewController 都可以,提高使用灵活性,更便于现有的业务接入。
// 返回列表视图
// 如果列表是 VC,就返回 VC.view
// 如果列表是 View,就返回 View 自己
- (UIView *)listView {
    
    
    return self.view;
}

参考资料

GitHub原作者链接

猜你喜欢

转载自blog.csdn.net/weixin_46926959/article/details/121636382