代码改变世界

UIScrollView现实循环滚动

2015-07-07 15:43  甘雨路  阅读(179)  评论(0编辑  收藏  举报
 1 #import "RootViewController.h"
 2 
 3 #define width [UIScreen mainScreen].bounds.size.width
 4 #define heigthY 150
 5 
 6 @interface RootViewController ()<UIScrollViewDelegate>
 7 {
 8     UIScrollView *_scrollView;
 9     NSMutableArray *imageArray;
10     UIPageControl *pageControl;
11 }
12 @end
13 
14 @implementation RootViewController
15 
16 - (void)dealloc
17 {
18     imageArray = nil;
19     [super dealloc];
20 }
21 
22 - (void)loadView
23 {
24     [super loadView];
25     _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, width, heigthY)];
26     _scrollView.pagingEnabled = YES;
27     _scrollView.delegate = self;
28     // 开始时选中第二个图片  图片的布局[3-1-2-3-1]
29     _scrollView.contentOffset = CGPointMake(width, 0);
30     // 隐藏水平滚动条
31     _scrollView.showsHorizontalScrollIndicator = NO;
32     
33     pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(width - 100,heigthY - 50, 100, 50)];
34     // 设置pageControl不支持用户操作
35     pageControl.userInteractionEnabled = NO;
36     pageControl.currentPageIndicatorTintColor = [UIColor redColor];
37     pageControl.pageIndicatorTintColor = [UIColor greenColor];
38     [self.view addSubview:_scrollView];
39     [self.view addSubview:pageControl];
40     [_scrollView release];
41     [pageControl release];
42     
43 }
44 
45 - (void)viewDidLoad {
46     [super viewDidLoad];
47     imageArray = [[NSMutableArray alloc] init];
48     NSArray *tempArray = @[@"1-3.jpg",@"1-1.jpg",@"1-2.jpg",@"1-3.jpg",@"1-1.jpg"];
49     [imageArray addObjectsFromArray:tempArray];
50     // 根据imageArray的数量设置_scrollView的内容大小
51     _scrollView.contentSize = CGSizeMake(width * imageArray.count, heigthY);
52     pageControl.numberOfPages = imageArray.count - 2;
53     // 给_scrollView添加图片
54     [self addImagesWithScrollView];
55 }
56 
57 /**
58  *  给scrollView添加图片
59  */
60 - (void)addImagesWithScrollView
61 {
62     for (int i = 0; i < imageArray.count; i++) {
63         UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageArray[i]]];
64         imageView.frame = CGRectMake(i * width, 0, width, heigthY);
65         [_scrollView addSubview:imageView];
66         [imageView release];
67     }
68 }
69 
70 #pragma mark - UIScrollViewDelegate的方法
71 - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
72 {
73     int imageIndex = scrollView.contentOffset.x / width;
74     if (imageIndex == 0) {
75         // 滚到第一张图片时,就跳转到倒数第二张图片
76         [_scrollView scrollRectToVisible:CGRectMake((imageArray.count - 2)*width, 0, width, heigthY) animated:NO];
77     }else if (imageIndex == imageArray.count - 1){
78         // 滚动到最后一张图片时,就跳转到第二张图片
79         [_scrollView scrollRectToVisible:CGRectMake(width, 0, width, heigthY) animated:NO];
80     }
81 }
82 
83 /**
84  *  设置pageControl的当前页
85  */
86 - (void)scrollViewDidScroll:(UIScrollView *)scrollView
87 {
88     // 加0.5是为了用户体验好些,滑动过程中哪张图片占优就显示占优图片对应的下标
89     int imageIndex = scrollView.contentOffset.x / width + 0.5;
90     if (imageIndex == 0) {
91         // 设置相应的下标(使之减1后与pageControl的下标相对应)
92         imageIndex = imageArray.count - 1;
93     }else if (imageIndex == imageArray.count - 1){
94         // 设置相应的下标(使之减1后与pageControl的下标相对应)
95         imageIndex = 1;
96     }
97     pageControl.currentPage = imageIndex - 1;
98 }
99 @end