ScrollView实现自动轮播

import "ViewController.h"

@interface ViewController ()

{
NSTimer *_timer;
BOOL isAdd;
}

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) NSMutableArray *advsList;
@property (nonatomic, strong) UIPageControl *pagePoint;

@end

/**

  • 屏幕宽度
    */

define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)

/**

  • 屏幕高度
    */

define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)

@implementation ViewController

  • (void)viewDidLoad {
    [super viewDidLoad];

    [self addView];
    [self showAdvs];
    [self addNSTimer];

}

pragma mark -创建图片数组

  • (NSMutableArray *)advsList
    {
    if(_advsList == nil){
    _advsList = [NSMutableArray array];
    }
    return _advsList;
    }

pragma mark -添加控件

  • (void)addView{

    //添加图片
    for (int i = 1; i < 5; i++) {
    [self.advsList addObject:[NSString stringWithFormat:@"%d",i]];
    }
    self.view.backgroundColor = [UIColor whiteColor];

    //添加滑动视图
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 150)];
    //设置代理这个必须的
    self.scrollView.delegate = self;
    //设置总大小也就是里面容纳的大小
    self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width * self.advsList.count,self.scrollView.frame.size.height);
    [self.scrollView setBackgroundColor:[UIColor blackColor]];
    //里面的子视图跟随父视图改变大小
    [self.scrollView setAutoresizesSubviews:YES];
    //设置分页形式,这个必须设置
    [self.scrollView setPagingEnabled:YES];
    //隐藏横竖滑动块
    [self.scrollView setShowsVerticalScrollIndicator:NO];
    [self.scrollView setShowsHorizontalScrollIndicator:NO];
    [self.view addSubview:self.scrollView];
    //场景UIPageControl显示控件,并修改小点颜色
    self.pagePoint = [[UIPageControl alloc]init];
    self.pagePoint.currentPageIndicatorTintColor = [UIColor blackColor];
    self.pagePoint.pageIndicatorTintColor = [UIColor grayColor];
    self.pagePoint.frame = CGRectMake(self.view.frame.size.width - self.pagePoint.bounds.size.width-self.advsList.count*10, self.scrollView.frame.origin.y+135, self.pagePoint.bounds.size.width, self.pagePoint.bounds.size.height);
    [self.pagePoint setNumberOfPages:[self.advsList count]];
    [self.view addSubview:self.pagePoint];

}

pragma mark -展示广告位,初始化

  • (void)showAdvs{

    isAdd = true;
    for (UIView *view in [self.scrollView subviews]){
    [view removeFromSuperview];
    }
    for(int i = 0; i < [self.advsList count]; i ++){
    UIButton *thumbView = [[UIButton alloc] init];
    [thumbView addTarget:self
    action:@selector(myDidSelectAdvAtIndex:)
    forControlEvents:UIControlEventTouchUpInside];
    thumbView.tag = i;
    thumbView.frame = CGRectMake(self.view.frame.size.width * i, 0, self.view.frame.size.width, 150);
    [thumbView setBackgroundImage:[UIImage imageNamed:self.advsList[i]] forState:UIControlStateNormal];
    thumbView.adjustsImageWhenHighlighted = NO;
    [self.scrollView addSubview:thumbView];
    }
    }

pragma mark -添加定时器

  • (void)addNSTimer
    {
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
    //添加到runloop中
    [[NSRunLoop mainRunLoop]addTimer:timer forMode:NSRunLoopCommonModes];
    _timer = timer;
    }

pragma mark -删除定时器

  • (void)removeNSTimer
    {
    [ _timer invalidate];
    _timer =nil;
    }

pragma mark -定时器下一页

  • (void)nextPage{
    NSInteger num = self.pagePoint.currentPage;
    if(isAdd){
    num++;
    if(num == self.advsList.count -1){
    isAdd = false;
    }
    }else{
    num--;
    if(num == 0){
    isAdd = true;
    }
    }
    [self scrollToIndex:num];
    }

pragma mark -滑动的距离

  • (void)scrollToIndex:(NSInteger)index
    {

    CGRect frame = self.scrollView.frame;
    frame.origin.x = frame.size.width * index;
    frame.origin.y = 0;
    [self.scrollView scrollRectToVisible:frame animated:YES];
    }

pragma mark -滑动完成时计算滑动到第几页

  • (void)scrollViewDidScroll:(UIScrollView *)scrollView
    {

    CGFloat pageWidth = scrollView.frame.size.width;
    int page = floor((scrollView.contentOffset.x - pageWidth / 2) /pageWidth) +1;
    [self.pagePoint setCurrentPage:page];
    }

pragma mark -当用户开始拖拽的时候就调用移除计时器

  • (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
    {
    [self removeNSTimer];
    }

pragma mark -当用户停止拖拽的时候调用添加定时器

  • (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
    {
    [self addNSTimer];
    }

pragma mark -点击广告

  • (void)myDidSelectAdvAtIndex:(id) index
    {
    UIButton *thumbView = (UIButton *)index;
    NSLog(@"你点击了第个%ld广告",thumbView.tag);
    }

  • (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }

@end

你可能感兴趣的:(ScrollView实现自动轮播)