美文网首页
滚动视图+分页控制器+定时器

滚动视图+分页控制器+定时器

作者: yi叶知秋 | 来源:发表于2016-06-29 01:12 被阅读93次
    #import "ViewController.h"
    @interface ViewController ()<UIScrollViewDelegate>
    {
        UIScrollView *_scrollView;
        UIPageControl *_pageControl;
        NSTimer *_timer;
        int _speed;
    }
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        //创建
        [self createScrollView];
        //创建pagecontrol
        UIPageControl *pageControl = [self pageControl];
        [self.view addSubview:pageControl];
        _speed = 1;
        _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(ontimer) userInfo:nil repeats:YES];
    }
    
    #pragma mark --- createScrollview
    -(void)createScrollView
    {
        _scrollView = [[UIScrollView alloc]  initWithFrame:CGRectMake(0, 20, 320, 200)];
        //设置滚动视图内容的大小
        _scrollView.contentSize = CGSizeMake(320*7, 200);
        for (int a = 0; a< 7; a++)
        {
            UIImageView *imageview = [[UIImageView alloc] initWithFrame:CGRectMake(320*a, 0, 320, 200)];
            NSString *imagestr = [NSString stringWithFormat:@"%d",a+1];
            imageview.image = [UIImage imageNamed:imagestr];
            [_scrollView addSubview:imageview];
        }
    
        //设置是否隐藏水平指示条或者竖直指示条
        //Indicator:指示器
        //Vertical:垂直方向
        //    _scrollView.showsVerticalScrollIndicator = NO;
        //    _scrollView.showsHorizontalScrollIndicator = NO;
        //设置指示条的样式
        //    _scrollView.indicatorStyle = UIScrollViewIndicatorStyleBlack;
        //设置scrollView内容的偏移量
        //x: x方向上的偏移量 正值:往左发生偏移 负值:往右偏移
        //y:y方向上的偏移量 正值:往上偏移 负值:往下偏移
        //scrollView.contentOffset = CGPointMake(0, -100);
       
        //设置是否开启回弹的效果
        _scrollView.bounces = NO;
        //设置scrollView是否分页显示
        _scrollView.pagingEnabled = YES;
        _scrollView.delegate = self;
        [self.view addSubview:_scrollView];
    }
    
    #pragma mark --- creatpageControl
    //页面指示器
    -(UIPageControl *)pageControl
    {
        _pageControl= [[UIPageControl alloc] initWithFrame:CGRectMake(100, 200, 130, 20)];
        //设置当前选中圆点颜色
        _pageControl.currentPageIndicatorTintColor = [UIColor yellowColor];
        //设置未选中的圆点颜色
        _pageControl.pageIndicatorTintColor = [UIColor blueColor];
        //设置pageControl的圆点个数
        _pageControl .numberOfPages = 7;
        [_pageControl addTarget:self action:@selector(pageMove) forControlEvents:UIControlEventValueChanged];
        return _pageControl;
    }
    
    #pragma mark --- pageCOntrol的帮定方法
    -(void)pageMove
    {
        //随着currentpage的值不断地改变,我们要及时的修改图片的偏移量
        [_scrollView setContentOffset:CGPointMake(_pageControl.currentPage*320, 0) animated:YES];
    }
    
    #pragma mark----timer
    -(void)ontimer
    {
        //currentPage属性设置小圆点的位置
        if (_pageControl.currentPage == 6)
        {
            _speed = -1;
        }
        if (_pageControl.currentPage == 0)
        {
            _speed = 1;
        }
        _pageControl.currentPage = _pageControl.currentPage + _speed;
        //改变偏移量带有动画的方法
        [_scrollView setContentOffset:CGPointMake(_pageControl.currentPage*320, 0) animated:YES];
    }
    
    #pragma mark --- UIScrollViewDelegate
    //将要开始拖拽
    -(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
    {
        [_timer invalidate];
        _timer = nil;
    }
    
    //拖拽结束后减速结束
    - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
    {
        //手动滑动图片会修改滚动试图偏移量,偏移量改变,让currentPage的值也跟着改变;
        _pageControl.currentPage = _scrollView.contentOffset.x/320;
        _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(ontimer) userInfo:nil repeats:YES];
    }
    @end

    相关文章

      网友评论

          本文标题:滚动视图+分页控制器+定时器

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