// // OrderHomeViewController.m // IBOSSmini // // Created by guan hong hou on 2017/5/16. // Copyright © 2017年 elongtian. All rights reserved. // #import "OrderHomeViewController.h" #import"ShopCartViewController.h" @interface OrderHomeViewController () @end @implementation OrderHomeViewController static int navcount = 2; //导航数 #pragma mark - 公共函数 /** viewDidLoad函数 */ - (void)viewDidLoad { [super viewDidLoad]; self.navigationItem.title=@"手机开单"; [self initUI]; [self setMainSrollView]; [self sliderWithTag:self.currentIndex+1]; UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; [button setBackgroundImage:[UIImage imageNamed:@"icon_back.png"] forState:UIControlStateNormal]; [button addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside]; button.frame = CGRectMake(0, 0, 15, 18); UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithCustomView:button]; self.navigationItem.leftBarButtonItem = menuButton; UIButton* btnCart = [UIButton buttonWithType:UIButtonTypeCustom]; UIImage *image = [UIImage imageNamed:@"gocart"]; [btnCart setBackgroundImage:image forState:UIControlStateNormal]; [btnCart addTarget:self action:@selector(goCart) forControlEvents:UIControlEventTouchUpInside]; btnCart.frame = CGRectMake(0, 0,image.size.width,image.size.height); UIBarButtonItem* _menuButton2 = [[UIBarButtonItem alloc] initWithCustomView:btnCart]; self.navigationItem.rightBarButtonItem = _menuButton2; } /** 修改:2017-9-25 适配机型 安全区视图发生变化 */ -(void)viewSafeAreaInsetsDidChange{ _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.searchVC viewSafeAreaInsetsDidChange]; [self.historyOrderVC viewSafeAreaInsetsDidChange]; [super viewSafeAreaInsetsDidChange]; } /** didReceiveMemoryWarning函数 */ - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /** 产品搜索vc @return <#return value description#> */ - (ProductSearchViewController *)searchVC{ if (_searchVC==nil) { _searchVC = [[ProductSearchViewController alloc] init]; _searchVC.cNav=self.navigationController; } return _searchVC; } /** 历史订单vc @return <#return value description#> */ - (HistoryOrderViewController *)historyOrderVC{ if (_historyOrderVC==nil) { _historyOrderVC = [[HistoryOrderViewController alloc] init]; _historyOrderVC.cNav=self.navigationController; } return _historyOrderVC; } /** * 返回函数 */ -(void)goBack { [self.navigationController popViewControllerAnimated:YES]; } /* 去购物车 */ -(void)goCart{ self.hidesBottomBarWhenPushed=YES; ShopCartViewController *detailVC=[[ShopCartViewController alloc] init]; [self.navigationController pushViewController:detailVC animated:YES]; } /* 初始化UI */ -(void)initUI{ UIView *navView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_Width, 40)]; [self.view addSubview:navView]; [self.view setBackgroundColor:[UIColor whiteColor]]; _productSearchButton = [UIButton buttonWithType:UIButtonTypeCustom]; [_productSearchButton setTitleColor:[UIColor redColor] forState:UIControlStateSelected]; [_productSearchButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; _productSearchButton.frame = CGRectMake(0, 0, Screen_Width/navcount, navView.frame.size.height); _productSearchButton.titleLabel.font = [UIFont systemFontOfSize:ButtonFontOfSize]; [_productSearchButton addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventTouchUpInside]; [_productSearchButton setTitle:@"商品查询" forState:UIControlStateNormal]; _productSearchButton.tag = 1; _productSearchButton.selected = YES; [navView addSubview:_productSearchButton]; _historyOrderButton= [UIButton buttonWithType:UIButtonTypeCustom]; _historyOrderButton.frame = CGRectMake(CGRectGetMaxX(_productSearchButton.frame),0, Screen_Width/navcount, navView.frame.size.height); [_historyOrderButton setTitleColor:[UIColor redColor] forState:UIControlStateSelected]; [_historyOrderButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; _historyOrderButton.titleLabel.font = [UIFont systemFontOfSize:ButtonFontOfSize]; [_historyOrderButton addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventTouchUpInside]; [_historyOrderButton setTitle:@"历史订单" forState:UIControlStateNormal]; _historyOrderButton.titleLabel.font = [UIFont systemFontOfSize:ButtonFontOfSize]; _historyOrderButton.tag = 2; [navView addSubview: _historyOrderButton]; _sliderLabel = [[UILabel alloc]initWithFrame:CGRectMake(_productSearchButton.frame.origin.x, 40-2, Screen_Width/navcount, 2)]; _sliderLabel.backgroundColor = [UIColor redColor]; [navView addSubview:_sliderLabel]; } /** 按钮点击事件 @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; _productSearchButton.selected = NO; _historyOrderButton.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 _productSearchButton; } else if (tag==2){ return _historyOrderButton; } else{ return nil; } } /** 初始化srollView */ - (void)setMainSrollView{ _mainScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 40, Screen_Width, self.view.frame.size.height-40)]; _mainScrollView.delegate = self; _mainScrollView.backgroundColor = [UIColor whiteColor]; _mainScrollView.pagingEnabled = YES; _mainScrollView.showsHorizontalScrollIndicator = NO; _mainScrollView.showsVerticalScrollIndicator = NO; [self.view addSubview:_mainScrollView]; NSArray *views = @[self.searchVC.view,self.historyOrderVC.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)]; [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]; } /** 滑动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]; } /** 滑动tag @param tag <#tag description#> */ - (void)sliderWithTag:(NSInteger)tag{ self.currentIndex = tag; _productSearchButton.selected = NO; _historyOrderButton.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); } @end