control

//屏幕宽度的宏

#define SCR_W [UIScreen mainScreen].bounds.size.width

//高度的宏

#define SCR_H [UIScreen mainScreen].bounds.size.height

//判断是不是X的宏

#define IS_IPHONE_X  (SCR_W==375&& SCR_H==812)

//状态栏高度的宏(三木运算符)/

#define STATUS_BAR_HEIGHT  (IS_IPHONE_X ?44:20)

@implementationContainerViewController

#pragma mark

#pragma mark 懒加载三个VC


-(NearbyViewController *)nearbyVC{

    if(nearbyVC==nil) {

        nearbyVC = [[NearbyViewController alloc]init];

        nearbyVC.navigationController = self.navigationController;

    }

    return nearbyVC;

}

-(SquareViewController *)sqareVC{

    if(sqareVC==nil) {

        sqareVC = [[SquareViewController alloc]init];

    }

    return sqareVC;

}



-(RecommendViewController *)recommendVC{

    if (recommendVC==nil) {

        recommendVC = [[RecommendViewController alloc]init];

        recommendVC.navigationController = self.navigationController;

    }

    return recommendVC;

}

#pragma mark 

#pragma mark 初始化三个UIButton和一个滑动的silderLabel,三个btn放到一个UIView(navView)上面。

-(void)initUI{

    navView = [[UIView alloc]initWithFrame:CGRectMake(0, 40, SCR_W, 40)];


    nearbyBtn = [UIButton buttonWithType:UIButtonTypeCustom];

    [nearbyBtn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];

    [nearbyBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

    nearbyBtn.frame=CGRectMake(0,40,SCR_W/4,navView.frame.size.height);

    nearbyBtn.titleLabel.font = [UIFont boldSystemFontOfSize:19];

    [nearbyBtn addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventTouchUpInside];

    [nearbyBtn setTitle:@"行业详情" forState:UIControlStateNormal];

    nearbyBtn.tag = 1;

    nearbyBtn.selected = YES;

    [navView addSubview:nearbyBtn];

  squareBtn = [UIButton buttonWithType:UIButtonTypeCustom];

    squareBtn.frame = CGRectMake(nearbyBtn.frame.origin.x+nearbyBtn.frame.size.width+40, nearbyBtn.frame.origin.y, SCR_W/4, navView.frame.size.height);

    [squareBtn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];

    [squareBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

    squareBtn.titleLabel.font = [UIFont boldSystemFontOfSize:16];

    [squareBtn addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventTouchUpInside];

    [squareBtn setTitle:@"行业指数" forState:UIControlStateNormal];

    squareBtn.tag = 2;

    [navView addSubview:squareBtn];



    recommendBtn = [UIButton buttonWithType:UIButtonTypeCustom];

    recommendBtn.frame = CGRectMake(squareBtn.frame.origin.x+squareBtn.frame.size.width+40, squareBtn.frame.origin.y, SCR_W/4, navView.frame.size.height);

    [recommendBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

    [recommendBtn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];

    recommendBtn.titleLabel.font = [UIFont boldSystemFontOfSize:16];

    [recommendBtn addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventTouchUpInside];

    [recommendBtn setTitle:@"牛枰草地" forState:UIControlStateNormal];

    recommendBtn.tag = 3;

    [navView addSubview:recommendBtn];


    sliderLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 81, SCR_W/4, 2)];

    sliderLabel.backgroundColor = [UIColor redColor];

    [navView addSubview:sliderLabel];


    self.navigationItem.titleView = navView;


}

- (void)viewDidLoad {

    self.view.backgroundColor = [UIColor whiteColor];

    [super viewDidLoad];

    [selfinitUI];

    [self setMainSrollView];

    //设置默认

    [self sliderWithTag:self.currentIndex+1];

    [self uinavControllerTab];

    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:@selector(clickw)];

    self.navigationController.navigationBar.tintColor = [UIColor lightGrayColor];


}


-(void)clickw{


}

-(void)uinavControllerTab{


    CGRect mainViewBounds = self.navigationController.view.bounds;

    UISearchBar*customSearchBar = [[UISearchBaralloc]initWithFrame:CGRectMake(CGRectGetWidth(mainViewBounds)/2-((CGRectGetWidth(mainViewBounds)-40)/2),CGRectGetMinY(mainViewBounds)+STATUS_BAR_HEIGHT,CGRectGetWidth(mainViewBounds)-120,40)];

    customSearchBar.delegate=self;

    customSearchBar.placeholder=@"搜索";

    customSearchBar.layer.cornerRadius=14.0f;

    customSearchBar.showsCancelButton=NO;

    customSearchBar.backgroundColor=[UIColorwhiteColor];

    customSearchBar.searchBarStyle=UISearchBarStyleMinimal;

    [self.navigationController.viewaddSubview: customSearchBar];


}

#pragma mark

#pragma mark 初始化srollView

-(void)setMainSrollView{

    mainScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 40, SCR_W, self.view.frame.size.height)];

    mainScrollView.delegate = self;

    mainScrollView.backgroundColor = [UIColor greenColor];

    mainScrollView.pagingEnabled = YES;

    mainScrollView.showsHorizontalScrollIndicator = NO;

    mainScrollView.showsVerticalScrollIndicator = NO;

    [self.view addSubview:mainScrollView];


    NSArray *views = @[self.nearbyVC.view,self.sqareVC.view,self.recommendVC.view];

    for(NSIntegeri =0; i

        //把三个vc的view依次贴到mainScrollView上面

        UIView*pageView = [[UIViewalloc]initWithFrame:CGRectMake(SCR_W*i,0,mainScrollView.frame.size.width,mainScrollView.frame.size.height-100)];

        [pageViewaddSubview:views[i]];

        [mainScrollViewaddSubview:pageView];

    }

    mainScrollView.contentSize=CGSizeMake(SCR_W*(views.count),0);

    //滚动到_currentIndex对应的tab

    [mainScrollView setContentOffset:CGPointMake((mainScrollView.frame.size.width)*_currentIndex, 0) animated:YES];

}

-(UIButton*)buttonWithTag:(NSInteger)tag{

    if(tag==1) {

        returnnearbyBtn;

    }elseif(tag==2){

        returnsquareBtn;

    }elseif(tag==3){

        return recommendBtn;

    }else{

        returnnil;

    }

}

-(void)sliderAction:(UIButton*)sender{

    if(self.currentIndex==sender.tag) {

        return;

    }

    [self sliderAnimationWithTag:sender.tag];

    [UIView animateWithDuration:0.3 animations:^{

        mainScrollView.contentOffset=CGPointMake(SCR_W*(sender.tag-1),0);

    }completion:^(BOOLfinished) {


    }];

}

-(void)sliderAnimationWithTag:(NSInteger)tag{

    self.currentIndex = tag;

    nearbyBtn.selected = NO;

    squareBtn.selected = NO;

    recommendBtn.selected = NO;

    UIButton*sender = [selfbuttonWithTag:tag];

    sender.selected=YES;

    //动画

    [UIView animateWithDuration:0.525 animations:^{

        sliderLabel.frame = CGRectMake(sender.frame.origin.x+20, sliderLabel.frame.origin.y, sliderLabel.frame.size.width, sliderLabel.frame.size.height);


    }completion:^(BOOLfinished) {

        nearbyBtn.titleLabel.font = [UIFont boldSystemFontOfSize:16];

        squareBtn.titleLabel.font = [UIFont boldSystemFontOfSize:16];

        recommendBtn.titleLabel.font = [UIFont boldSystemFontOfSize:16];

        sender.titleLabel.font= [UIFontboldSystemFontOfSize:19];

    }];


}

-(void)sliderWithTag:(NSInteger)tag{

    self.currentIndex = tag;

    nearbyBtn.selected = NO;

    squareBtn.selected = NO;

    recommendBtn.selected = NO;

    UIButton*sender = [selfbuttonWithTag:tag];

    sender.selected=YES;

    //动画

        sliderLabel.frame = CGRectMake(sender.frame.origin.x, sliderLabel.frame.origin.y, sliderLabel.frame.size.width, sliderLabel.frame.size.height);

        nearbyBtn.titleLabel.font = [UIFont boldSystemFontOfSize:16];

        squareBtn.titleLabel.font = [UIFont boldSystemFontOfSize:16];

        recommendBtn.titleLabel.font = [UIFont boldSystemFontOfSize:16];

        sender.titleLabel.font= [UIFontboldSystemFontOfSize:19];

}

-(void)scrollViewDidScroll:(UIScrollView*)scrollView{

    //实时计算当前位置,实现和titleView上的按钮的联动

    CGFloatcontentOffSetX = scrollView.contentOffset.x;

    CGFloatX = contentOffSetX * (3*SCR_W/4)/SCR_W/3;

    CGRectframe =sliderLabel.frame;

    frame.origin.x= X;

    sliderLabel.frame= frame;

}

- (void)scrollViewDidEndDecelerating:(UIScrollView*)scrollView{

    CGFloatcontentOffSetX = scrollView.contentOffset.x;

        intindex_ = contentOffSetX/SCR_W;

    [selfsliderWithTag:index_+1];

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];


}

-(void)dealloc{

    NSLog(@"%s dealloc",object_getClassName(self));

}

你可能感兴趣的:(control)