UIScrollView現實循環滾動

 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
相關文章
相關標籤/搜索