标签:
1 #import "RootViewController.h"
2
3 #define width [UIScreen mainScreen].bounds.size.width
4 #define heigthY 150
5 #define scrollTime 1
6
7 @interface RootViewController ()<UIScrollViewDelegate>
8 {
9 UIScrollView *_scrollView;
10 NSMutableArray *imageArray;
11 UIPageControl *pageControl;
12 }
13 @end
14
15 @implementation RootViewController
16
17 - (void)dealloc
18 {
19 imageArray = nil;
20 [super dealloc];
21 }
22
23 - (void)loadView
24 {
25 [super loadView];
26 _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, width, heigthY)];
27 _scrollView.pagingEnabled = YES;
28 _scrollView.delegate = self;
29 // 开始时选中第二个图片 图片的布局[3-1-2-3-1]
30 _scrollView.contentOffset = CGPointMake(width, 0);
31 // 隐藏水平滚动条
32 _scrollView.showsHorizontalScrollIndicator = NO;
33
34 pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(width - 100,heigthY - 50, 100, 50)];
35 // 设置pageControl不支持用户操作
36 pageControl.userInteractionEnabled = NO;
37 pageControl.currentPageIndicatorTintColor = [UIColor redColor];
38 pageControl.pageIndicatorTintColor = [UIColor greenColor];
39 [self.view addSubview:_scrollView];
40 [self.view addSubview:pageControl];
41 [_scrollView release];
42 [pageControl release];
43
44 }
45
46 - (void)viewDidLoad {
47 [super viewDidLoad];
48 imageArray = [[NSMutableArray alloc] init];
49 NSArray *tempArray = @[@"1-3.jpg",@"1-1.jpg",@"1-2.jpg",@"1-3.jpg",@"1-1.jpg"];
50 [imageArray addObjectsFromArray:tempArray];
51 // 根据imageArray的数量设置_scrollView的内容大小
52 _scrollView.contentSize = CGSizeMake(width * imageArray.count, heigthY);
53 pageControl.numberOfPages = imageArray.count - 2;
54 // 给_scrollView添加图片
55 [self addImagesWithScrollView];
56
57 // 添加定时器
58 NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:scrollTime target:self selector:@selector(automateScroll) userInfo:nil repeats:YES];
59 }
60
61 // 每隔scrollTime秒就滚动一次
62 - (void)automateScroll
63 {
64 // 因为开始时出现的图片是imageArray中的第二张图片,后面求余后还加1,使得contentOffset为2*width
65 static int contentOffsetX = 1;
66 // 为了偏移量始终在(1 至 imageArray.count-2)*width中,((contentOffsetX % (imageArray.count - 2))求余得到的数值在 0~imageArray.count-3中, + 1后让数值保持在 1 至 imageArray.count-2 中
67 _scrollView.contentOffset = CGPointMake(((contentOffsetX % (imageArray.count - 2)) + 1) *width, 0);
68 contentOffsetX ++;
69 NSLog(@"==%d",contentOffsetX);
70 }
71 /**
72 * 给scrollView添加图片
73 */
74 - (void)addImagesWithScrollView
75 {
76 for (int i = 0; i < imageArray.count; i++) {
77 UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageArray[i]]];
78 imageView.frame = CGRectMake(i * width, 0, width, heigthY);
79 [_scrollView addSubview:imageView];
80 [imageView release];
81 }
82 }
83
84 //#pragma mark - UIScrollViewDelegate的方法
85 - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
86 {
87 int imageIndex = scrollView.contentOffset.x / width;
88 if (imageIndex == 0) {
89 // 滚到第一张图片时,就跳转到倒数第二张图片
90 [_scrollView scrollRectToVisible:CGRectMake((imageArray.count - 2)*width, 0, width, heigthY) animated:NO];
91 }else if (imageIndex == imageArray.count - 1){
92 // 滚动到最后一张图片时,就跳转到第二张图片
93 [_scrollView scrollRectToVisible:CGRectMake(width, 0, width, heigthY) animated:NO];
94 }
95 }
96
97 /**
98 * 设置pageControl的当前页
99 */
100 - (void)scrollViewDidScroll:(UIScrollView *)scrollView
101 {
102 // 加0.5是为了用户体验好些,滑动过程中哪张图片占优就显示占优图片对应的下标
103 int imageIndex = scrollView.contentOffset.x / width + 0.5;
104 if (imageIndex == 0) {
105 // 设置相应的下标(使之减1后与pageControl的下标相对应)
106 imageIndex = (int)imageArray.count - 1;
107 }else if (imageIndex == imageArray.count - 1){
108 // 设置相应的下标(使之减1后与pageControl的下标相对应)
109 imageIndex = 1;
110 }
111 pageControl.currentPage = imageIndex - 1;
112 }
113
114 @end
标签:
原文地址:http://www.cnblogs.com/lantu1989/p/4628879.html