简单实现滑动和点击翻页 button与scrollView结合 iOS

来源:互联网 发布:windows rt怎么换系统 编辑:程序博客网 时间:2024/06/07 02:36


//

//  ViewController.m

//  第一天的

//

//  Created by beevle on 16/1/11.

//  Copyright © 2016 beevle. All rights reserved.

//


#import "ViewController.h"






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

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




@interface ViewController ()<UIScrollViewDelegate,UITableViewDataSource,UITableViewDelegate>


@end


@implementation ViewController

{

    UIButton * _lastBtn;

    UIScrollView * _scrollView;

    UITableView * _tableView;

}

- (void)viewDidLoad {

    

    

    [superviewDidLoad];

    [selfcreateNAV];

    [self createScrollView];

    

    


}

//导航


- (void)createNAV{

    UIView * topview = [[UIViewalloc]initWithFrame:CGRectMake(0,0, 375,64)];

    topview.userInteractionEnabled =YES;

    topview.backgroundColor = [UIColorgrayColor];

    NSArray * nameArr =@[@"",@"",@""];

    for (int i =0; i<3; i++) {

        UIButton * button = [UIButtonbuttonWithType:UIButtonTypeCustom];

        button.frame =CGRectMake(100+i*50,20, 30,30);

        [button setTitle:nameArr[i] forState:UIControlStateNormal];

        [button setTitleColor:[UIColorblackColor] forState:(UIControlStateSelected)];

        button.tag =100 + i;

        if (button.tag ==100) {

            button.selected =YES;

            _lastBtn = button;

        }

        [topview addSubview:button];

        [button addTarget:selfaction:@selector(buttonClick:)forControlEvents:UIControlEventTouchUpInside];

        

    }

   

    [self.viewaddSubview:topview];

}

//点击事件

-(void)buttonClick:(UIButton *)btn{

    

    if (btn!=_lastBtn) {

        

        btn.selected =YES;

        _lastBtn.selected =NO;

        _lastBtn =btn;

        

        [UIViewanimateWithDuration:0.5animations:^{

           

            _scrollView.contentOffset =CGPointMake((btn.tag-100)*WW_width,0);


            

        }];

        

    }

}


//主要效果

-(void)createScrollView {

    _scrollView = [[UIScrollViewalloc]initWithFrame:CGRectMake(0,64, WW_width,WW_height)];

    _scrollView.autoresizesSubviews=NO;

    _scrollView.backgroundColor = [UIColorwhiteColor];

    

    

    _scrollView.contentSize =CGSizeMake([UIScreenmainScreen].bounds.size.width*3,WW_height-64) ;

    _scrollView.pagingEnabled =YES;

    _scrollView.delegate =self;


    

    for (int i =0 ; i<3; i++) {

        UIView * view = [[UIViewalloc]initWithFrame:CGRectMake(i*WW_width,0, WW_width,WW_height-64)];

        view.tag =200 + i ;

        if (view.tag==200) {

            view.backgroundColor = [UIColorgreenColor];

         

        }elseif (view.tag ==201){

            view.backgroundColor = [UIColoryellowColor];

            

        }else{

            view.backgroundColor = [UIColorcyanColor];

            

        }

        

        [_scrollViewaddSubview:view];

    }

   

    [self.viewaddSubview:_scrollView];

}


//scrollview 的协议方法

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

    

    float sub =_scrollView.contentOffset.x/WW_width;

    

    UIButton * btn = (UIButton * )[self.viewviewWithTag:100+sub];

    

    if (_lastBtn != btn) {

    btn.selected =YES;

    _lastBtn.selected =NO;

    _lastBtn =btn;

    

    }

    

}


@end


1 0
原创粉丝点击