@property (strong, nonatomic) UIScrollView *scrollView;
@property (strong, nonatomic)UIPageControl *pageControl;
//定时器
@property(strong,nonatomic)NSTimer * timer;
- (void)viewDidLoad {
[super viewDidLoad];
_timeIndex=0;
_timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(scrollChange) userInfo:nil repeats:YES];
}
//定时器执行方法
-(void)scrollChange
{
_timeIndex++;
if (_timeIndex == 5) {
// NSLog(@"_timeIndex = %ld",_timeIndex);
_timeIndex = 1;
_scrollView.contentOffset = CGPointMake(WIDTH * _timeIndex, 0);
}else if(_timeIndex == 6)
{
_timeIndex = 3;
_scrollView.contentOffset = CGPointMake(WIDTH * _timeIndex, 0);
}
else {
_scrollView.contentOffset = CGPointMake(_timeIndex *WIDTH,0);
}
}
//将开始拖拽时使定时器停止
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
NSLog(@"1");
[_timer invalidate];
_timer = nil;
}
//拖拽结束时再开启定时器
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
NSLog(@"@@");
_timeIndex = scrollView.contentOffset.x/WIDTH ;
NSLog(@"_timeIndex = %ld",_timeIndex);
_timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(scrollChange) userInfo:nil repeats:YES];
}
//scrollview发生变动时调用此方法,多次调用
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//防止同一个view上其他的scrollview同时调用此方法
if (scrollView.tag ==200) {
UIPageControl * pc = (UIPageControl *)[self.view viewWithTag:10];
// _timeIndex++;
int currentPage =scrollView.contentOffset.x/WIDTH;
// NSLog(@"currentPage = %d",currentPage);
if (currentPage > 5) {
_scrollView.contentOffset = CGPointMake(WIDTH * 2, 0);
} else if (currentPage<1) {
_scrollView.contentOffset = CGPointMake(4*WIDTH,0);
pc.currentPage = 4;
}
if (currentPage >0 && currentPage < 5) {
pc.currentPage = currentPage-1;
}else if (currentPage == 5)
{
pc.currentPage = 0;
}else if (currentPage == 6)
{
pc.currentPage = 1;
}else if(currentPage == 0)
{
pc.currentPage = 3;
}
}
}