无线轮播视图

#import "ViewController.h"

#define WIDTH self.view.frame.size.width

#define HEIGHT self.view.frame.size.height

@interface ViewController ()

@property(nonatomic,strong) UIScrollView * scrollV;

@property(nonatomic,strong) UIPageControl * pageControl;

@property(nonatomic,strong) NSTimer * timer;

@property(nonatomic,strong) NSArray * imageArr;

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];


    self.imageArr=@[@"IMG_0003",@"IMG_0225",@"IMG_1057",@"IMG_1058",@"IMG_1060"];


    _scrollV = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 380)];

    _scrollV.delegate =self;

    _scrollV.bounces=NO;

    [self.view addSubview:_scrollV];

    _scrollV.contentSize = CGSizeMake(WIDTH * _imageArr.count, HEIGHT);

    _scrollV.pagingEnabled = YES;

    //添加图片

    for(inti =0; i<_imageArr.count; i++) {

        //创建图片对象

        UIImageView* imageView = [[UIImageViewalloc]initWithFrame:CGRectMake(i*WIDTH,0,WIDTH,380)];

        imageView.image= [UIImageimageNamed:self.imageArr[i]];

        [_scrollVaddSubview:imageView];

    }

    _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, 350, WIDTH,20)];

    //_pageControl.backgroundColor = [UIColor redColor];

    _pageControl.alpha = 0.5;

    //设置圆点的个数

    _pageControl.numberOfPages = _imageArr.count;

    //添加事件

    [_pageControl addTarget:self action:@selector(valueChanage:) forControlEvents:UIControlEventValueChanged];

    [self.view addSubview:_pageControl];

    //创建事件定时器

    [self startTimer];

}

//pageControl 点击事件,点击PageCt 改变滚动视图的偏移量

-(void)valueChanage:(UIPageControl*)pc

{

    NSIntegercurrentPage = pc.currentPage;

    //设置scrollV的偏移量

    [_scrollV setContentOffset:CGPointMake(currentPage * WIDTH,0) animated:YES];

}

//滚动视图停止滚动的时候

-(void)scrollViewDidScroll:(UIScrollView*)scrollView

{

    NSLog(@"滚动的时候会调用");

    _pageControl.currentPage = scrollView.contentOffset.x/WIDTH + 0.5;

}

//定时器方法,定时滚动图片

-(void)ChangeImage{


    if (self.pageControl.currentPage==_imageArr.count-1) {

        self.pageControl.currentPage=0;

        [self.scrollV setContentOffset:CGPointMake(0, 0)];

    }

    [_scrollV setContentOffset:CGPointMake((self.pageControl.currentPage+1)*WIDTH, 0) animated:YES];

}

//停止定时器

- (void)stopTimer

{

    [self.timer invalidate];

    self.timer=nil;

}

//创建定时器

- (void)startTimer {


    _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(ChangeImage)userInfo:nil repeats:YES];


    // 调整timer 的优先级

    NSRunLoop*mainLoop = [NSRunLoopmainRunLoop];

    [mainLoopaddTimer:_timer forMode:NSRunLoopCommonModes];

}

/**

 手指开始拖动的时候, 就让计时器停止

 */

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

    [self stopTimer];

}

/**

 手指离开屏幕的时候, 就让计时器开始工作

 */

- (void)scrollViewWillEndDragging:(UIScrollView*)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inoutCGPoint*)targetContentOffset {

    [self startTimer];

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end

你可能感兴趣的:(无线轮播视图)