@property(nonatomic,strong)UIScrollView *scrollView;
@property(nonatomic,strong)UIPageControl *pageControl;
@property(nonatomic,strong)NSTimer *timer;
@property(nonatomic,assign)int timeIndex;
- (void)viewDidLoad {
[super viewDidLoad];
[
_timeIndex=0;
_timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(scrollChange) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop]addTimer:_timer forMode:NSDefaultRunLoopMode];
_scrollView = [UIScrollView new];
_scrollView.showsVerticalScrollIndicator = NO;
_scrollView.delegate = self;
_scrollView.pagingEnabled = YES;
_scrollView.frame=CGRectMake(0, 64, self.view.bounds.size.width, 136);
self.automaticallyAdjustsScrollViewInsets=NO;
_scrollView.showsHorizontalScrollIndicator = NO;
CGFloat imageW = self.scrollView.frame.size.width;
CGFloat imageH = self.scrollView.frame.size.height;
CGFloat imageInitialY = 0;
CGFloat imageInitialX = 0;
NSArray *arr=[NSArray arrayWithObjects:@"1",@"2",@"3",@"4", nil];
/*添加按钮
for (int i=0; i<4; i++) {
UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:[UIImage imageNamed:[arr objectAtIndex:i]] forState:UIControlStateNormal];
button.tag=i;
CGFloat imageX = i*imageW + imageInitialX;
button.frame=CGRectMake(imageX, imageInitialY, imageW, imageH);
[button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[_scrollView addSubview:button];
}
*/
CGFloat contentw=4*imageW;
self.scrollView.contentSize=CGSizeMake(contentw, 0);
self.scrollView.pagingEnabled = YES;
[self.view addSubview:self.scrollView];
_pageControl = [UIPageControl new];
_pageControl.frame=CGRectMake(0, 200,WEIDTH , 5);
_pageControl.accessibilityNavigationStyle =
UIAccessibilityNavigationStyleAutomatic;
_pageControl.numberOfPages=4;
_pageControl.pageIndicatorTintColor = [UIColor redColor];// 设置非选中页的圆点颜色
_pageControl.currentPageIndicatorTintColor = [UIColor blueColor]; // 设置选中页的圆点颜色
[self.view addSubview:self.pageControl];
}
-(void)scrollChange{
NSLog(@"123");
_timeIndex++;
if (_timeIndex == 4) {
_timeIndex = 0;
_scrollView.contentOffset = CGPointMake(WEIDTH * _timeIndex, 0);
}
else {
_scrollView.contentOffset = CGPointMake(_timeIndex *WEIDTH,0);
}
}
//手动滑动时候,定时器关闭
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
NSLog(@"1");
[_timer invalidate];
_timer = nil;
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
_timeIndex = scrollView.contentOffset.x/WEIDTH ;
NSLog(@"_timeIndex = %d",_timeIndex);
_timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(scrollChange) userInfo:nil repeats:YES];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
int page = scrollView.contentOffset.x / scrollView.frame.size.width;
_pageControl.currentPage = page;
}