
Untitled.gif
#import "ViewController.h"
#define ZXSCREEN_WIDTH [[UIScreen mainScreen] bounds].size.width
#define ZXSCREEN_HEIGHT [[UIScreen mainScreen] bounds].size.height
#define TopCycleScrollviewHeight (ZXSCREEN_WIDTH*(165.0/375.0))
@interface ViewController ()<UIScrollViewDelegate,UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UIView *topSelectBtnBgView;
@property (nonatomic, strong) UIView *topMarkLine;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) NSMutableArray *navItemsArr;
@property (nonatomic, strong) UIButton *navSelectItem;
@property (nonatomic, assign) BOOL isClickNavItem;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.isClickNavItem = NO;
[self.view addSubview:self.tableView];
[self.view addSubview:self.topSelectBtnBgView];
[self.view sendSubviewToBack:self.topSelectBtnBgView];
self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
}
-(UIView *)topSelectBtnBgView {
if (!_topSelectBtnBgView) {
_topSelectBtnBgView = [UIView new];
_topSelectBtnBgView.alpha = 0;
_topSelectBtnBgView.backgroundColor = [UIColor blueColor];
self.navItemsArr = [NSMutableArray arrayWithCapacity:0];
NSArray *titlearr = @[@"商品",@"相关推荐",@"详细信息",@"规格参数"];
[titlearr enumerateObjectsUsingBlock:^(NSString *string, NSUInteger idx, BOOL * _Nonnull stop) {
UIButton *btn = [UIButton new];
[self.navItemsArr addObject:btn];
btn.tag = 2*idx+1;
btn.backgroundColor = [UIColor purpleColor];
btn.titleLabel.font = [UIFont systemFontOfSize:14];
[btn setTitle:string forState:UIControlStateNormal];
[btn addTarget:self action:@selector(onChangeItemBtnClick:) forControlEvents:UIControlEventTouchUpInside];
if (idx == 0) {
self.navSelectItem = btn;
[btn setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
[self.topSelectBtnBgView addSubview:self.topMarkLine];
}else {
[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
}
btn.frame = CGRectMake(idx*ZXSCREEN_WIDTH/4.0, 90, ZXSCREEN_WIDTH/4.0, 30);
[self.topSelectBtnBgView addSubview:btn];
}];
}
return _topSelectBtnBgView;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell classForCoder])];
cell.textLabel.text = @(indexPath.row).stringValue;
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0) {
return 10;
}
if (section == 1) {
return 10;
}
return 20;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 4;
}
- (UITableView *)tableView {
if (!_tableView) {
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 164, ZXSCREEN_WIDTH, ZXSCREEN_HEIGHT - 164) style:UITableViewStyleGrouped];
_tableView.delegate = self;
_tableView.dataSource = self;
[_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])];
}
return _tableView;
}
-(UIView *)topMarkLine {
if (!_topMarkLine) {
_topMarkLine = [UIView new];
_topMarkLine.frame = CGRectMake((ZXSCREEN_WIDTH/4.0)/2.0-(21)/2.0, 64 + 30, 21, 2);
_topMarkLine.backgroundColor = [UIColor purpleColor];
}
return _topMarkLine;
}
- (void)click {
NSLog(@"ddd");
}
-(void)onChangeItemBtnClick:(UIButton *)sender {
self.isClickNavItem = YES;
[self.navSelectItem setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[sender setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
self.navSelectItem = sender;
[UIView animateWithDuration:0.2 animations:^{
self.topMarkLine.frame = CGRectMake(sender.tag*ZXSCREEN_WIDTH/8.0-(21)/2.0, 64 + 30, 21, 2);
}];
NSInteger section = (sender.tag-1)/2;
if (section == 0) {
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:(sender.tag-1)/2] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}else {
self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:(sender.tag-1)/2] atScrollPosition:UITableViewScrollPositionTop animated:YES];
self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
}
}
#pragma mark - UIScrollViewDelegate
-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
self.isClickNavItem = NO;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
self.navigationController.navigationBar.barTintColor = [UIColor redColor];
CGFloat alpha = MIN(1, scrollView.contentOffset.y/TopCycleScrollviewHeight);
NSLog(@"%d - %f",__LINE__,alpha);
if (alpha >= 0 && alpha <= 1) {
self.topSelectBtnBgView.alpha = alpha;
self.navigationController.navigationBar.alpha = alpha;
}
if (alpha < 0) {
self.topSelectBtnBgView.alpha = 0;
self.navigationController.navigationBar.alpha = 0;
}
self.topMarkLine.frame = CGRectMake(self.navSelectItem.tag*ZXSCREEN_WIDTH/8.0-(21)/2.0, 64 + 30, 21, 2);
if (!self.isClickNavItem) {
if (scrollView.contentOffset.y >= self.tableView.contentSize.height-(ZXSCREEN_HEIGHT-64)) {
[self.navSelectItem setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
self.navSelectItem = self.navItemsArr[3];
[self.navSelectItem setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
}else {
CGPoint offsetPoint = CGPointMake(0, scrollView.contentOffset.y+98);
NSIndexPath *indexpath = [self.tableView indexPathForRowAtPoint:offsetPoint];
[self.navSelectItem setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
self.navSelectItem = self.navItemsArr[indexpath.section];
[self.navSelectItem setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
}
[UIView animateWithDuration:0.2 animations:^{
self.topMarkLine.frame = CGRectMake(self.navSelectItem.tag*ZXSCREEN_WIDTH/8.0-(21)/2.0, 64 + 30, 21, 2);
}];
}
}
@end
网友评论