#import <UIKit/UIKit.h>
#import "JXCategoryBaseView.h"
#define WindowsSize [UIScreen mainScreen].bounds.size
@interface ViewController : UIViewController
@property (nonatomic, strong) JXCategoryBaseView *categoryView;
@property (nonatomic, strong) UIScrollView *scrollView;
- (Class)preferredCategoryViewClass;
- (NSUInteger)preferredListViewCount;
- (CGFloat)preferredCategoryViewHeight;
@end
@interface ViewController ()<JXCategoryViewDelegate>
@property (nonatomic, strong) NSArray *titles
@property (nonatomic, strong) JXCategoryTitleView *myCategoryView
@end
@implementation ViewController
- (void)viewDidLoad {
_titles = @[@"螃蟹", @"麻辣小龙虾", @"苹果", @"营养胡萝卜", @"葡萄", @"美味西瓜", @"香蕉", @"香甜菠萝", @"鸡肉", @"鱼", @"海星"]
[super viewDidLoad]
self.myCategoryView.titles = self.titles
self.view.backgroundColor = [UIColor whiteColor]
self.edgesForExtendedLayout = UIRectEdgeNone
CGFloat naviHeight = 64
if (@available(iOS 11.0, *)) {
if (WindowsSize.height == 812) {
naviHeight = [UIApplication sharedApplication].keyWindow.safeAreaInsets.top + 44
}
}
NSUInteger count = [self preferredListViewCount]
CGFloat categoryViewHeight = [self preferredCategoryViewHeight]
CGFloat width = WindowsSize.width
CGFloat height = WindowsSize.height - naviHeight - categoryViewHeight
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, categoryViewHeight, width, height)]
self.scrollView.pagingEnabled = YES
self.scrollView.contentSize = CGSizeMake(width*count, height)
[self.view addSubview:self.scrollView]
for (int i = 0
VerticalViewController *listVC = [[VerticalViewController alloc] init]
[self addChildViewController:listVC]
listVC.view.frame = CGRectMake(i*width, 0, width, height)
[self.scrollView addSubview:listVC.view]
}
self.categoryView.frame = CGRectMake(0, 0, WindowsSize.width, categoryViewHeight)
self.categoryView.delegate = self
self.categoryView.contentScrollView = self.scrollView
[self.view addSubview:self.categoryView]
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated]
self.navigationController.interactivePopGestureRecognizer.enabled = (self.categoryView.selectedIndex == 0)
}
- (JXCategoryTitleView *)myCategoryView {
return (JXCategoryTitleView *)self.categoryView
}
- (NSUInteger)preferredListViewCount {
return self.titles.count
}
- (Class)preferredCategoryViewClass {
return [JXCategoryTitleView class]
}
- (JXCategoryBaseView *)categoryView {
if (_categoryView == nil) {
_categoryView = [[[self preferredCategoryViewClass] alloc] init]
}
return _categoryView
}
- (CGFloat)preferredCategoryViewHeight {
return 50
}
- (void)categoryView:(JXCategoryBaseView *)categoryView didSelectedItemAtIndex:(NSInteger)index {
[self.scrollView setContentOffset:CGPointMake(self.scrollView.bounds.size.width*index, 0) animated:YES]
//侧滑手势处理
self.navigationController.interactivePopGestureRecognizer.enabled = (index == 0)
}
@end
