scrollView实现轮播

scrollView实现轮播

  • 假设现在我们有5张图片需要实现轮播效果,我实现轮播的方法是在第一张图片前插入最后一张,在最后一张后添加第一张,当滚动到添加后的最后一张时,contentoffset.x = screen_w,也就是到了插入前第一张;同理针对插入前的一张。
  • 代码:
1./**
2. * @brief 创建轮播,在tableView创建之后和数据加载后
3. */

4.-(void)createCarousel{
5. UIView *carouselBack = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, CAROUSEL_HEIGHT)];
6.
7. NSInteger numberOfAd = self.topStories.count - 2;
8.
9. _carousel = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, CAROUSEL_HEIGHT)];
10. _carousel.delegate = self;
11. _carousel.showsHorizontalScrollIndicator = NO;
12. _carousel.showsVerticalScrollIndicator = NO;
13. _carousel.bounces = NO;
14. _carousel.pagingEnabled = YES;
15. _carousel.contentSize = CGSizeMake(self.topStories.count * SCREEN_W, CAROUSEL_HEIGHT);
16.
17. for (int i = 0; i < self.topStories.count; i++) {
18. JKTopStory *topStory = self.topStories[i];
19. UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_W * i, 0, SCREEN_W, CAROUSEL_HEIGHT)];
20. NSURL *url = [NSURL URLWithString:topStory.image];
21. [imageView setImageWithURL:url];
22. [_carousel addSubview:imageView];
23. }
24. _carousel.contentOffset = CGPointMake(SCREEN_W, 0);
25.
26.
27. [carouselBack addSubview:_carousel];
28.
29. _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W * 0.2, 10)];
30. _pageControl.numberOfPages = numberOfAd;
31. _pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
32. _pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
33. _pageControl.center = CGPointMake(SCREEN_W * 0.5, CAROUSEL_HEIGHT - 20);
34. _pageControl.tintColor = [UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:1];
35. [carouselBack addSubview:_pageControl];
36.
37. self.storyTableView.tableHeaderView = carouselBack;
38.
39. _timer = [NSTimer scheduledTimerWithTimeInterval:5.0f target:self selector:@selector(doScroll) userInfo:nil repeats:YES];
40.}
41.#pragma mark - scrollView的代理方法
42.-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
43. if (scrollView == _carousel) {
44. if (scrollView.contentOffset.x == SCREEN_W * (self.topStories.count -1)) {
45. scrollView.contentOffset = CGPointMake(SCREEN_W, 0);
46. }else if (scrollView.contentOffset.x == 0){
47. scrollView.contentOffset = (CGPoint){SCREEN_W * (self.topStories.count - 2),0};
48. }
49. }
50.}
51.-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
52. if (scrollView == _carousel) {
53. CGPoint offset = scrollView.contentOffset;
54. _pageControl.currentPage = offset.x / SCREEN_W - 1;
55. }
56.}
57.// 关于drag方法,可以避免手动拖动后与timer的执行时间相撞,从而连续滚动两张图片
58.-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
59. [_timer invalidate];
60.}
61.-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
62. _timer = nil;
63. _timer = [NSTimer scheduledTimerWithTimeInterval:5.0f target:self selector:@selector(doScroll) userInfo:nil repeats:YES];
64.}
65.
66.#pragma mark - 定时器回调
67.-(void)doScroll{
68. __block CGPoint offset = _carousel.contentOffset;
69. offset.x += SCREEN_W;
70. [UIView animateWithDuration:1.0f animations:^{
71. _carousel.contentOffset = offset;
72. } completion:^(BOOL finished) {
73. if (offset.x == SCREEN_W * (self.topStories.count - 1)) {
74. offset = (CGPoint){SCREEN_W,offset.y};
75. _carousel.contentOffset = offset;
76. }
77. _pageControl.currentPage = offset.x / SCREEN_W - 1;
78. }];
79.}
  • 演示:
    Alt text
 
posted @ 2016-02-29 10:44  Emerys  阅读(452)  评论(0编辑  收藏  举报