滚动视图

@interface ViewController (){

    NSArray * imgArr;

    UIScrollView * scrollV;

    UIPageControl * page;

    NSTimer * timer;

    int k;

}

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(change) userInfo:nil repeats:YES];


    imgArr = @[@"1",@"2",@"3",@"4"];



    //新特性界面

    //滚动视图

    scrollV = [[UIScrollView alloc]initWithFrame:self.view.frame];

    //

    scrollV.backgroundColor=[UIColor redColor];

    //设置滚动范围

    scrollV.contentSize=CGSizeMake(self.view.frame.size.width * 4, self.view.frame.size.height);

    //设置分页

    scrollV.pagingEnabled=YES;

    //隐藏水平滚动条

    scrollV.showsHorizontalScrollIndicator=NO;

    //取消弹簧效果

    scrollV.bounces=NO;


    //设置滚动视图的代理

    scrollV.delegate=self;


    //初始化图片框

    for (int i = 0; i<4; i++)

    {

        //创建了四个图片框

        UIImageView * imgV = [[UIImageView alloc]initWithFrame:CGRectMake(i * self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height)];

        //设置图片

        imgV.image=[UIImage imageNamed:imgArr[i]];

        //设置图片与用户交互

        imgV.userInteractionEnabled=YES;


        if (i==3) {

            //初始化按钮

            UIButton * btn = [[UIButton alloc]initWithFrame:CGRectMake((self.view.frame.size.width-100)/2, 600, 100, 44)];

            //添加文字

            [btn setTitle:@"立即体验" forState:UIControlStateNormal];

            //设置边框

            btn.layer.borderWidth = 1;

            //设置边框颜色

            btn.layer.borderColor = [UIColor cyanColor].CGColor;

            //设置文字颜色

            [btn setTitleColor:[UIColor cyanColor] forState:UIControlStateNormal];

            //添加点击事件

            [btn addTarget:self action:@selector(shijian) forControlEvents:UIControlEventTouchUpInside];

            //将按钮添加到图片

            [imgV addSubview:btn];


        }


        [scrollV addSubview:imgV];


    }


    //设置分页控制点

    page = [[UIPageControl alloc]initWithFrame:CGRectMake((self.view.frame.size.width-100)/2, 640, 100, 30)];

    //分页点的个数

    page.numberOfPages=4;

    //设置页码的颜色

    page.pageIndicatorTintColor=[UIColor blackColor];

    //选中点的颜色

    page.currentPageIndicatorTintColor=[UIColor redColor];

    //添加到主视图

    [self.view addSubview:scrollV];

    [self.view addSubview:page];







}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView

{

    //偏移量

    page.currentPage =  scrollV.contentOffset.x/self.view.frame.size.width;


}

-(void)change

{

    k = scrollV.contentOffset.x/self.view.frame.size.width;

    //设置当前 点的 位置

    page.currentPage=k;

    //每隔三秒 + 1

    k++;

    // set设置偏移量

    scrollV.contentOffset = CGPointMake(k * self.view.frame.size.width, 0);

    if (k>=3) {

        [timer invalidate];

    }


}

你可能感兴趣的:(滚动视图)