- 假设现在我们有5张图片需要实现轮播效果,我实现轮播的方法是在第一张图片前插入最后一张,在最后一张后添加第一张,当滚动到添加后的最后一张时,contentoffset.x = screen_w,也就是到了插入前第一张;同理针对插入前的一张。
- 代码:
-(void)createCarousel{
UIView *carouselBack = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, CAROUSEL_HEIGHT)];
NSInteger numberOfAd = self.topStories.count - 2;
_carousel = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, CAROUSEL_HEIGHT)];
_carousel.delegate = self;
_carousel.showsHorizontalScrollIndicator = NO;
_carousel.showsVerticalScrollIndicator = NO;
_carousel.bounces = NO;
_carousel.pagingEnabled = YES;
_carousel.contentSize = CGSizeMake(self.topStories.count * SCREEN_W, CAROUSEL_HEIGHT);
for (int i = 0; i < self.topStories.count; i++) {
JKTopStory *topStory = self.topStories[i];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_W * i, 0, SCREEN_W, CAROUSEL_HEIGHT)];
NSURL *url = [NSURL URLWithString:topStory.image];
[imageView setImageWithURL:url];
[_carousel addSubview:imageView];
}
_carousel.contentOffset = CGPointMake(SCREEN_W, 0);
[carouselBack addSubview:_carousel];
_pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W * 0.2, 10)];
_pageControl.numberOfPages = numberOfAd;
_pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
_pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
_pageControl.center = CGPointMake(SCREEN_W * 0.5, CAROUSEL_HEIGHT - 20);
_pageControl.tintColor = [UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:1];
[carouselBack addSubview:_pageControl];
self.storyTableView.tableHeaderView = carouselBack;
_timer = [NSTimer scheduledTimerWithTimeInterval:5.0f target:self selector:@selector(doScroll) userInfo:nil repeats:YES];
}
#pragma mark - scrollView的代理方法
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
if (scrollView == _carousel) {
if (scrollView.contentOffset.x == SCREEN_W * (self.topStories.count -1)) {
scrollView.contentOffset = CGPointMake(SCREEN_W, 0);
}else if (scrollView.contentOffset.x == 0){
scrollView.contentOffset = (CGPoint){SCREEN_W * (self.topStories.count - 2),0};
}
}
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
if (scrollView == _carousel) {
CGPoint offset = scrollView.contentOffset;
_pageControl.currentPage = offset.x / SCREEN_W - 1;
}
}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
[_timer invalidate];
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
_timer = nil;
_timer = [NSTimer scheduledTimerWithTimeInterval:5.0f target:self selector:@selector(doScroll) userInfo:nil repeats:YES];
}
#pragma mark - 定时器回调
-(void)doScroll{
__block CGPoint offset = _carousel.contentOffset;
offset.x += SCREEN_W;
[UIView animateWithDuration:1.0f animations:^{
_carousel.contentOffset = offset;
} completion:^(BOOL finished) {
if (offset.x == SCREEN_W * (self.topStories.count - 1)) {
offset = (CGPoint){SCREEN_W,offset.y};
_carousel.contentOffset = offset;
}
_pageControl.currentPage = offset.x / SCREEN_W - 1;
}];
}
- 演示: