一、UIScrollview基本概念ui
UIScrollView是一個用於選擇滑動的視圖,能夠顯示更多的視圖。並且能夠經過手勢放大、縮小要顯示的內容spa
UIScrollView包含兩個子類:UITableView、UITextView. 它們均可以實現滑動視圖的全部動做代理
二、UIScrollView經常使用屬性code
//能夠滾動的大小,默認是0 沒有滾動效果 contentSize //當touch後尚未拖到時值位YES 不然爲NO tracking //正在縮放時爲YES 不然爲NO zooming //滾動後,手指放開還在滾動中爲YES 不然爲NO decelerating //設置手指放開後的減速速率 decelerationRate //浮點數 能放最大的倍數 maximumZoomScale //浮點數 能放最小的倍數 minimumZoomScale //當值爲YES 會自動滾動UIScrollView的邊界 默認是NO pagingEnabled //決定是否能夠滾動 scrollEnabled //滾動時是否顯示水平滾動條 showsHorizontalScrollIndicator //滾動時是否顯示垂直滾動條 showsVerticalScrollIndicator //默認是YES 滾動超過邊界後會有反彈回來的效果 若爲NO 滾動超過邊界後會當即中止 bounces //與bounces同樣的效果 區別在:效果是顯示在縮放上 bouncesZoom //默認是NO 能夠在水平和垂直方向同時運動 若爲YES 假如一開始是水平或垂直方法滾動 則會鎖定另一個方向的滾動,若一開始是對角方向滾動 則不會禁止某個方向的滾動 directionalLockEnabled //設置滾動條的樣式 基本只設置顏色 默認 、黑、白 indicatorStyle //設置滾動條位置 scrollIndicatorInsets
//設置內容邊緣位置
contentInsets
//設置UIScrollView滾動到某一個位置或某個區域
- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated;
orm
- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated; 對象
三、UIScrollView經常使用代理方法blog
滾動時,滾動視圖軌跡狀態包括跟蹤、拖動、減速和縮放。繼承
contentOffSet屬性定義了滾動視圖左邊界左上方的可見點的偏移位置,具體參數以下:圖片
//UIScrollView已經滾動
- (void)scrollViewDidScroll:(UIScrollView *)scrollView;
//scrollView開始拖動
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView; //scrollView結束拖動 (IOS 5.0以後的方法)
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView
withVelocity:(CGPoint)velocity
targetContentOffset:(inout CGPoint *)targetContentOffset // scrollView結束拖動
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate; //scrollview開始減速
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView; //scrollView減速中止
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView; //返回一個放大或縮小的視圖
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView; //開始放大或縮小
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view
//視圖正在放大或縮小
- (void)scrollViewDidZoom:(UIScrollView *)scrollView
//縮放結束
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale
//是否支持滑動到底部
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView
//滑動到底部時調用改方法
- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView
四、UIScrollView與UIPageControl的結合使用ci
五、UIScrollView的捏合手勢
只須要實現一個委託方法就能夠是UIScrollView的子視圖經過捏合手勢實現縮放功能
1 //返回一個放大或縮小的視圖 2 - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView;
六、isMemberOfClass 與isKindOfClass的區別
二者均可以堅持一個對象是不是某個類的成員,區別在於:isKindOfClass 不只能夠肯定一個對象是不是某個類的成員,也能夠肯定某個對象是不是派生自該類的成員
而isMemberOfClass 作不到這一點
七、案例:滑動視圖實現分頁縮放,雙擊時能夠放大縮小圖片
1 //自定義ImageScrollView 繼承自 UIScrollView實現縮放功能,雙擊也能夠放大縮小 2 - (id)initWithFrame:(CGRect)frame 3 { 4 self = [super initWithFrame:frame]; 5 if (self) { 6 self.maximumZoomScale = 2.5; 7 self.minimumZoomScale = 1; 8 self.showsHorizontalScrollIndicator = NO; 9 self.showsVerticalScrollIndicator = NO; 10 11 self.delegate = self; 12 _imageView = [[UIImageView alloc] initWithFrame:self.bounds]; 13 [self addSubview:_imageView]; 14 15 UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(zoomInOrOut:)]; 16 doubleTap.numberOfTapsRequired = 2; 17 [self addGestureRecognizer:doubleTap]; 18 } 19 return self; 20 } 21 22 - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{ 23 return _imageView; 24 } 25 - (void)zoomInOrOut:(UITapGestureRecognizer *)tapGesture{ 26 if (self.zoomScale>=2.5) { 27 [self setZoomScale:2. animated:YES]; 28 }else { 29 CGPoint point = [tapGesture locationInView:self]; 30 [self zoomToRect:CGRectMake(point.x - 40, point.y-40, 80, 80) animated:YES]; 31 } 32 }
1 //MainViewController的主要代碼 2 - (void)viewDidLoad 3 { 4 [super viewDidLoad]; 5 6 _scrollView.contentSize = CGSizeMake(320 *4,460); 7 _scrollView.pagingEnabled = YES; 8 //將_scrollView的tag設置int_max 與ImageScrollView區分,不然下面經過當tag爲0時_scrollView得到根據tag得的時其本身 9 _scrollView.tag = INT_MAX; 10 for (int i=0; i<4; i++) { 11 ImageScrollView *imageScrollView = [[ImageScrollView alloc] initWithFrame:CGRectMake(0+i*320, 0, 320, 460)]; 12 NSString *imageName = [NSString stringWithFormat:@"%d.JPG",i+1]; 13 UIImage *image = [UIImage imageNamed:imageName]; 14 imageScrollView.imageView.image = image; 15 imageScrollView.tag = i; 16 [_scrollView addSubview:imageScrollView]; 17 } 18 19 UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 400, 320, 20)]; 20 pageControl.numberOfPages = 4; 21 pageControl.pageIndicatorTintColor = [UIColor redColor]; 22 pageControl.currentPageIndicatorTintColor = [UIColor purpleColor]; 23 pageControl.tag = 100; 24 [self.view addSubview:pageControl]; 25 } 26 27 int pre = 0; 28 - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{ 29 int currentIndex = scrollView.contentOffset.x / 320.0; 30 UIPageControl *pageControl = (UIPageControl *)[scrollView.superview viewWithTag:100]; 31 pageControl.currentPage = currentIndex; 32 ImageScrollView *imageScrollView = (ImageScrollView *)[scrollView viewWithTag:pre]; 33 if (imageScrollView.zoomScale > 1.0 && pre!=currentIndex) { 34 imageScrollView.zoomScale = 1.0; 35 } 36 pre = currentIndex; 37 }