滚动视图自动滚+小圆点

如果您在阅读我的文章时有疑问 , 请点击这里

#import "ViewController.h"
#define WIDTH [[UIScreen mainScreen] bounds].size.width

#define HEIGHT [[UIScreen mainScreen] bounds].size.height
@interface ViewController ()
@property (nonatomic, strong)NSTimer *timer; //定时器
@property (nonatomic, retain)NSMutableArray *arr;  //放图片的数组
@property (nonatomic, retain)UIView *headerView;  //tableView的表头
@property (nonatomic, retain)UIImageView *image;  //图片
@property (nonatomic, retain)UIScrollView *scrollView;
@property (nonatomic, retain)UIPageControl *pageC;  //页码

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self headImage];
}
- (void)headImage{
    //图片数组
    self.arr = [NSMutableArray arrayWithObjects:@"8.jpg",@"1.jpg", @"2.jpg", @"3.jpg", @"4.jpg", @"5.jpg", @"6.jpg", @"7.jpg", @"8.jpg", @"1.jpg", nil];
    
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 200 * HEIGHT/667)];
    self.scrollView.backgroundColor = [UIColor clearColor];
    //设置滚动量
    self.scrollView.contentSize = CGSizeMake(WIDTH * self.arr.count, 0);
    //设置偏移量
    self.scrollView.contentOffset = CGPointMake(WIDTH, 0);
    //设置按页滚动
    self.scrollView.pagingEnabled = YES;
    //设置是否显示水平滑动条
    self.scrollView.showsHorizontalScrollIndicator = NO;
    //设置是否边界反弹
    self.scrollView.bounces = NO;
    //把scrollView添加到tableView的表头的视图上
    [self.view addSubview:self.scrollView];
    
    //循环图片添加到UIImageView上
    for (int i = 0 ;  i  < self.arr.count; i++) {
        NSString *name = [self.arr objectAtIndex:i];
        UIImage *img = [UIImage imageNamed:name];
        self.image = [[UIImageView alloc]init];
        self.image.frame = CGRectMake(i * WIDTH, 0, WIDTH, 200 * HEIGHT/667);
        self.image.image = img;
        [self.scrollView addSubview:self.image];
        
    }
    self.scrollView.delegate = self;
    //设置页面
    self.pageC = [[UIPageControl alloc]initWithFrame:CGRectMake(100 * WIDTH/375, 120 * HEIGHT/667, 200* WIDTH/375, 60*HEIGHT/667)];
    self.pageC.backgroundColor = [UIColor clearColor];
    //把页码添加到头视图上
    [self.view addSubview:self.pageC];
    //设置页码数
    self.pageC.numberOfPages = self.arr.count;
    //设置选中页码的颜色
    self.pageC.currentPageIndicatorTintColor = [UIColor whiteColor];
    //设置未选中的页码颜色
    self.pageC.pageIndicatorTintColor = [UIColor grayColor];
    //设置当前选中页
    self.pageC.currentPage = 0;
    //核心方法
    [self.pageC addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
    //自定义一个定时器方法
    [self addTimer];
    
}

//点击页码,换页的方法
- (void)pageAction:(UIPageControl *)page{
    [self.scrollView setContentOffset:CGPointMake(WIDTH * (self.pageC.currentPage + 1), 0) animated:YES];
}
//减速停止时触发
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    //当图片在第一张时
    if (scrollView.contentOffset.x == 0) {
        // 用户滑动到1号位置,此时必须跳转到倒二的位置
        [scrollView setContentOffset:CGPointMake(WIDTH * (self.arr.count), 0) animated:NO];
    }
    //当图片在倒数第一张时
    else if (scrollView.contentOffset.x == scrollView.contentSize.width - WIDTH) {
        //把倒数第一张位置换成第二张图片
        [scrollView setContentOffset:CGPointMake(WIDTH, 0) animated:NO];
        
    }
    //滚动换页码
    self.pageC.currentPage = scrollView.contentOffset.x / WIDTH - 1;
}
//开启定时器
- (void)addTimer{
    //设置定时器
    self.timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(change:) userInfo:nil repeats:YES];
    
}
//关闭定时器
- (void)removeTimer{
    if (_timer) {
        if ([self.timer isValid]) {
            [self.timer invalidate];
            _timer=nil;
        }
    }
}
//开始拖拽的时候调用
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    //关闭定时器(注意,定时器一旦被关闭,无法在开启)
    [self removeTimer];
}
//拖拽结束后
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    //开启定时器
    [self addTimer];
}
//定时器执行方法
- (void)change:(NSTimer *)time{
    if (self.pageC.currentPage == self.pageC.numberOfPages - 1) {
        self.pageC.currentPage = 0;
    } else if (self.pageC.currentPage < self.pageC.numberOfPages - 1) {
        self.pageC.currentPage++;
    }
    [self.scrollView setContentOffset:CGPointMake((self.pageC.currentPage + 1) * WIDTH, 0) animated:NO];
}


@end

你可能感兴趣的:(滚动视图自动滚+小圆点)