1.先介紹下UIScrollView的常見屬性
@property(nonatomic) CGPoint contentOffset; // 記錄UIScrollView滾動的位置 @property(nonatomic) CGSize contentSize; // 內容尺寸(能滾動的范圍) @property(nonatomic) UIEdgeInsets contentInset; // 額外增加的滾動區域(在上下左右4個邊緣) @property(nonatomic,assign) id<UIScrollViewDelegate> delegate; // 代理對象 @property(nonatomic) BOOL bounces; // 是否有彈簧效果 @property(nonatomic) BOOL showsHorizontalScrollIndicator; // 是否顯示水平滾動條 @property(nonatomic) BOOL showsVerticalScrollIndicator; // 是否顯示垂直滾動條
2.分頁瀏覽的實現
2.1.把需要顯示的圖片設置進UIScrollView
1 CGFloat w = self.view.frame.size.width; 2 CGFloat h = self.view.frame.size.height; 3 for (int i = 0; i< kCount; i++) { 4 UIImageView *imageView = [[UIImageView alloc] init]; 5 6 // 1.設置frame 7 imageView.frame = CGRectMake(i * w, 0, w, h); 8 9 // 2.設置圖片 10 NSString *imgName = [NSString stringWithFormat:@"0%d.jpg", i + 1]; 11 imageView.image = [UIImage imageNamed:imgName]; 12 13 [_scrollView addSubview:imageView];
2.2.設置UIScrollView的相關屬性
屬性在文章的開頭有介紹
// height == 0 代表 禁止垂直方向滾動 _scrollView.contentSize = CGSizeMake(kCount * w, 0); _scrollView.showsHorizontalScrollIndicator = NO; _scrollView.pagingEnabled = YES; _scrollView.delegate = self;
2.3.設置UIPageControl的相關屬性,計算頁碼
1 UIPageControl *pageControl = [[UIPageControl alloc] init]; 2 pageControl.center = CGPointMake(w * 0.5, h - 20); 3 pageControl.bounds = CGRectMake(0, 0, 150, 50); 4 pageControl.numberOfPages = kCount; // 一共顯示多少個圓點(多少頁) 5 // 設置非選中頁的圓點顏色 6 pageControl.pageIndicatorTintColor = [UIColor redColor]; 7 // 設置選中頁的圓點顏色 8 pageControl.currentPageIndicatorTintColor = [UIColor blueColor]; 9 10 // 禁止默認的點擊功能 11 pageControl.enabled = NO; 12 13 [self.view addSubview:pageControl]; 14 _pageControl = pageControl;
2.4.滾動時切換頁碼
#pragma mark - UIScrollView的代理方法 #pragma mark 當scrollView正在滾動的時候調用 - (void)scrollViewDidScroll:(UIScrollView *)scrollView { int page = scrollView.contentOffset.x / scrollView.frame.size.width; // NSLog(@"%d", page); // 設置頁碼 _pageControl.currentPage = page; }
作者: 清澈Saup
出處: http://www.cnblogs.com/qingche/
本文版權歸作者和博客園共有,歡迎轉載,但必須保留此段聲明,且在文章頁面明顯位置給出原文連接。