// // InstallReceiptHomeViewController.m // IBOSS // // Created by guan hong hou on 2017/7/10. // Copyright © 2017年 沈阳东科云信软件有限公司. All rights reserved. // // 功能描述:安装回执主页视图控制器 #import "InstallReceiptHomeViewController.h" @interface InstallReceiptHomeViewController () @end @implementation InstallReceiptHomeViewController //导航数 static int navcount = 2; #pragma mark - 公共函数 /** 视图加载函数 */ - (void)viewDidLoad { [super viewDidLoad]; self.navigationItem.title = @"安装回执一览"; UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; [button setImage:[UIImage imageNamed:@"icon_back.png"] forState:UIControlStateNormal]; [button addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside]; button.frame = CGRectMake(0, 0,45,22); UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithCustomView:button]; self.navigationItem.leftBarButtonItem = menuButton; [self initUI]; [self setMainSrollView]; [self sliderWithTag:self.currentIndex+1]; } /** 安全区视图发生变化 */ -(void)viewSafeAreaInsetsDidChange{ self.view.backgroundColor = [UIColor whiteColor]; _mainScrollView.frame = CGRectMake(0, 40, Screen_Width,self.view.safeAreaLayoutGuide.layoutFrame.size.height-40); for (NSInteger i = 0; i < _mainScrollView.subviews.count; i++) { _mainScrollView.subviews[i].frame = CGRectMake(Screen_Width * i, 0, Screen_Width,CGRectGetHeight(_mainScrollView.frame)); } [self.installUnreceiptVC viewSafeAreaInsetsDidChange]; [self.installReceiptVC viewSafeAreaInsetsDidChange]; [super viewSafeAreaInsetsDidChange]; } /** 初始化安装未回执控制器 @return <#return value description#> */ - (InstallUnreceiptVC *)installUnreceiptVC{ if (_installUnreceiptVC == nil) { _installUnreceiptVC = [[InstallUnreceiptVC alloc] init]; _installUnreceiptVC.cNav = self.navigationController; } return _installUnreceiptVC; } /** 初始化安装回执控制器 @return <#return value description#> */ - (InstallReceiptedVC *)installReceiptVC{ if (_installReceiptVC == nil) { _installReceiptVC = [[InstallReceiptedVC alloc] init]; _installReceiptVC.cNav = self.navigationController; } return _installReceiptVC; } #pragma mark - 委托函数 /** 滑动label位置 @param scrollView <#scrollView description#> */ - (void)scrollViewDidScroll:(UIScrollView *)scrollView{ //实时计算当前位置,实现和titleView上的按钮的联动 CGFloat contentOffSetX = scrollView.contentOffset.x; CGFloat x = contentOffSetX / navcount; CGRect frame = _sliderLabel.frame; frame.origin.x = x; _sliderLabel.frame = frame; } /** 滑动完成 @param scrollView <#scrollView description#> */ - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{ CGFloat contentOffSetX = scrollView.contentOffset.x; int index = contentOffSetX / Screen_Width; [self sliderWithTag:index + 1]; } #pragma mark - 私有函数 /* 初始化UI */ - (void)initUI{ UIView *navView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_Width, 40)]; [self.view addSubview:navView]; [self.view setBackgroundColor:[UIColor whiteColor]]; _installUnreceiptButton = [UIButton buttonWithType:UIButtonTypeCustom]; [_installUnreceiptButton setTitleColor:[UIColor redColor] forState:UIControlStateSelected]; [_installUnreceiptButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; _installUnreceiptButton.frame = CGRectMake(0, 0, Screen_Width / navcount, navView.frame.size.height); _installUnreceiptButton.titleLabel.font = [UIFont systemFontOfSize:ButtonFontOfSize]; [_installUnreceiptButton addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventTouchUpInside]; [_installUnreceiptButton setTitle:@"未回执" forState:UIControlStateNormal]; _installUnreceiptButton.tag = 1; _installUnreceiptButton.selected = YES; [navView addSubview:_installUnreceiptButton]; _installReceiptedButton= [UIButton buttonWithType:UIButtonTypeCustom]; _installReceiptedButton.frame = CGRectMake(CGRectGetMaxX(_installUnreceiptButton.frame),0, Screen_Width / navcount, navView.frame.size.height); [_installReceiptedButton setTitleColor:[UIColor redColor] forState:UIControlStateSelected]; [_installReceiptedButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; _installReceiptedButton.titleLabel.font = [UIFont systemFontOfSize:ButtonFontOfSize]; [_installReceiptedButton addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventTouchUpInside]; [_installReceiptedButton setTitle:@"已回执" forState:UIControlStateNormal]; _installReceiptedButton.titleLabel.font = [UIFont systemFontOfSize:ButtonFontOfSize]; _installReceiptedButton.tag = 2; [navView addSubview: _installReceiptedButton]; _sliderLabel = [[UILabel alloc]initWithFrame:CGRectMake(_installUnreceiptButton.frame.origin.x, 40 - 2, Screen_Width / navcount, 2)]; UIView *line = [[UIView alloc] initWithFrame:CGRectMake(((Screen_Width/navcount)-50)/2, 0, 50, 2)]; line.backgroundColor = [UIColor redColor]; [_sliderLabel addSubview:line]; [navView addSubview:_sliderLabel]; } /** 滑动tag @param tag <#tag description#> */ - (void)sliderWithTag:(NSInteger)tag{ self.currentIndex = tag; _installUnreceiptButton.selected = NO; _installReceiptedButton.selected = NO; UIButton *sender = [self buttonWithTag:tag]; sender.selected = YES; //动画 _sliderLabel.frame = CGRectMake(sender.frame.origin.x, _sliderLabel.frame.origin.y, _sliderLabel.frame.size.width, _sliderLabel.frame.size.height); } /** 按钮点击事件 @param sender <#sender description#> */ - (void)sliderAction:(UIButton *)sender{ if (self.currentIndex == sender.tag) { return; } [self.view endEditing:YES]; [self sliderAnimationWithTag:sender.tag]; [UIView animateWithDuration:0.3 animations:^{ _mainScrollView.contentOffset = CGPointMake(Screen_Width*(sender.tag - 1), 0); } completion:^(BOOL finished) { }]; } /** 滑动tag @param tag <#tag description#> */ - (void)sliderAnimationWithTag:(NSInteger)tag{ self.currentIndex = tag; _installReceiptedButton.selected = NO; _installUnreceiptButton.selected = NO; UIButton *sender = [self buttonWithTag:tag]; sender.selected = YES; //动画 [UIView animateWithDuration:0.3 animations:^{ _sliderLabel.frame = CGRectMake(sender.frame.origin.x, _sliderLabel.frame.origin.y, _sliderLabel.frame.size.width, _sliderLabel.frame.size.height); } completion:^(BOOL finished) { }]; } /* 获得导航按钮 */ - (UIButton *)buttonWithTag:(NSInteger )tag{ if (tag == 1) { return _installUnreceiptButton; } else if (tag == 2){ return _installReceiptedButton; } else{ return nil; } } /** 初始化srollView */ - (void)setMainSrollView{ _mainScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 40, Screen_Width, self.view.frame.size.height)]; _mainScrollView.delegate = self; _mainScrollView.backgroundColor = [UIColor whiteColor]; _mainScrollView.pagingEnabled = YES; _mainScrollView.showsHorizontalScrollIndicator = NO; _mainScrollView.showsVerticalScrollIndicator = NO; [self.view addSubview:_mainScrollView]; NSArray *views = @[self.installUnreceiptVC.view,self.installReceiptVC.view]; for (NSInteger i = 0; i < views.count; i++) { //把三个vc的view依次贴到mainScrollView上面 UIView *pageView = [[UIView alloc]initWithFrame:CGRectMake(Screen_Width * i, 0, _mainScrollView.frame.size.width, _mainScrollView.frame.size.height - 100)]; [pageView addSubview:views[i]]; [_mainScrollView addSubview:pageView]; } _mainScrollView.contentSize = CGSizeMake(Screen_Width*(views.count), 0); //滚动到_currentIndex对应的tab [_mainScrollView setContentOffset:CGPointMake((_mainScrollView.frame.size.width) * _currentIndex, 0) animated:YES]; } /** 返回到父视图 */ - (void)goBack{ [self.navigationController popViewControllerAnimated:YES]; } @end