#import "ViewController.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface ViewController ()<UIScrollViewDelegate>
@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++) {
? ? ? ? //創(chuàng)建圖片對象
? ? ? ? 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;
? ? //設置圓點的個數(shù)
? ? _pageControl.numberOfPages = _imageArr.count;
? ? //添加事件
? ? [_pageControl addTarget:self action:@selector(valueChanage:) forControlEvents:UIControlEventValueChanged];
? ? [self.view addSubview:_pageControl];
? ? //創(chuàng)建事件定時器
? ? [self startTimer];
}
//pageControl 點擊事件奥额,點擊PageCt 改變滾動視圖的偏移量
-(void)valueChanage:(UIPageControl*)pc
{
? ? NSIntegercurrentPage = pc.currentPage;
? ? //設置scrollV的偏移量
? ? [_scrollV setContentOffset:CGPointMake(currentPage * WIDTH,0) animated:YES];
}
//滾動視圖停止?jié)L動的時候
-(void)scrollViewDidScroll:(UIScrollView*)scrollView
{
? ? NSLog(@"滾動的時候會調(diào)用");
? ? _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;
}
//創(chuàng)建定時器
- (void)startTimer {
? ? _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(ChangeImage)userInfo:nil repeats:YES];
? ? // 調(diào)整timer 的優(yōu)先級
? ? 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