美文网首页
循环滑动

循环滑动

作者: 金牛忘忧 | 来源:发表于2016-07-02 08:58 被阅读0次

    //

    //ViewController.m

    //循环滑动

    #import"ViewController.h"

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

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

    @interfaceViewController()

    {

    UIScrollView* scrollView;

    UIPageControl* pageCtr;

    }

    @end

    @implementationViewController

    - (void)viewDidLoad {

    [superviewDidLoad];

    //1:创建视图(scrollView,pageCtr)

    [selfcreateScrollViewPageCtr];

    }

    - (void)createScrollViewPageCtr{

    //1:创建scrollView

    scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0, kScreenWidth, kScreenHeight)];

    //最重要的属性滑动的范围

    scrollView.contentSize = CGSizeMake(kScreenWidth*8, kScreenHeight);

    //循环添加imageView

    for(inti =0; i <6; i++)

    {

    //获取图片的名字

    NSString *imageName = [NSString stringWithFormat:@"%i.jpg",i+1];

    NSLog(@"%@",imageName);

    //创建imageView

    UIImageView * iamgeView = [[UIImageView alloc] initWithFrame:CGRectMake((i+1)*kScreenWidth,0, kScreenWidth, kScreenHeight)];

    iamgeView.image = [UIImage imageNamed:imageName];

    [scrollView addSubview:iamgeView];

    }

    [self.view addSubview:scrollView];

    //添加到第1张前面

    UIImageView *imageView1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"6.jpg"]];

    imageView1.frame = CGRectMake(0,0, kScreenWidth, kScreenHeight);

    [scrollView addSubview:imageView1];

    //添加第5后面的第一张

    UIImageView *imageView5 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.jpg"]];

    imageView5.frame = CGRectMake(kScreenWidth*7,0, kScreenWidth, kScreenHeight);

    [scrollView addSubview:imageView5];

    //--------------------------------------

    scrollView.contentOffset = CGPointMake(kScreenWidth,0);

    //--------------------------------------

    //属性分页

    scrollView.pagingEnabled =YES;

    //设置代理对象

    scrollView.delegate =self;

    //2:创建pageCtr

    pageCtr = [[UIPageControl alloc] initWithFrame:CGRectMake(0, kScreenHeight/2, kScreenWidth,30)];

    //属性

    pageCtr.numberOfPages =6;

    pageCtr.pageIndicatorTintColor = [UIColor grayColor];

    pageCtr.currentPageIndicatorTintColor= [UIColor whiteColor];

    //pageCtr.currentPage = 0;

    //添加值改变的方法

    [pageCtr addTarget:selfaction:@selector(pageCtrAction) forControlEvents:UIControlEventValueChanged];

    //[pageCtr updateCurrentPageDisplay];

    [self.view addSubview:pageCtr];

    }

    - (void)pageCtrAction{

    //获取当前的pageCtr.currentPage当前页数

    CGPoint point = CGPointMake(pageCtr.currentPage * kScreenWidth,0);

    scrollView.contentOffset = point;

    }

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

    if(scrollView.contentOffset.x ==2240) {

    scrollView.contentOffset = CGPointMake(kScreenWidth,0);

    pageCtr.currentPage =0;

    }

    if(scrollView.contentOffset.x==0) {

    scrollView.contentOffset = CGPointMake(kScreenWidth*6,0);

    pageCtr.currentPage =6;

    }

    pageCtr.currentPage = scrollView.contentOffset.x / kScreenWidth-1;

    NSLog(@"pageCtr.currentPage=%li",pageCtr.currentPage);

    //NSLog(@"scrollView.contentOffset.x:%f",);

    }

    @end

    相关文章

      网友评论

          本文标题:循环滑动

          本文链接:https://www.haomeiwen.com/subject/rzrqjttx.html