iOS滾動檢視UIScrollView使用方法
//.h檔案 @property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
//.m檔案
//初始化ScrollView,設定位置
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
self.scrollView.backgroundColor = [UIColor greenColor];
//設定顯示內容的大小,這裡表示可以下滑十倍原高度
self.scrollView.contentSize = CGSizeMake(320, 460*10);
//設定當滾動到邊緣繼續滾時是否像橡皮經一樣彈回
self.scrollView.bounces = YES;
//設定滾動條指示器的型別,預設是白邊界上的黑色滾動條
self.scrollView.indicatorStyle = UIScrollViewIndicatorStyleDefault;//還有UIScrollViewIndicatorStyleBlack、UIScrollViewIndicatorStyleWhite
//設定是否只允許橫向或縱向(YES)滾動,預設允許雙向
// self.scrollView.directionalLockEnabled = YES;
//設定是否採用分頁的方式
// self.scrollView.pagingEnabled = YES;
//設定是否允許滾動
// self.scrollView.scrollEnabled = NO;
//設定是否可以縮放
self.scrollView.maximumZoomScale = 2.0;//最多放大到兩倍
self.scrollView.minimumZoomScale = 0.5;
[self.view addSubview:self.scrollView]; //建立兩個label顯示在ScrollerView中,第二個要滾動才能看見 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 200, 320, 40)]; label.backgroundColor = [UIColor yellowColor]; label.text = @"Study UIScrollView"; [self.scrollView addSubview:label]; UILabel *secondLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 800, 320, 40)]; secondLabel.backgroundColor = [UIColor yellowColor]; secondLabel.text = @"Next Label"; [self.scrollView addSubview:secondLabel];
//委託方法 #pragma mark - UIScrollViewDelegate //返回縮放時所使用的UIView物件
- (UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView{ return scrollView; }
//開始縮放時呼叫
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view{
}
//結束縮放時呼叫,告知縮放比例
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale{
}
//已經縮放時呼叫
- (void)scrollViewDidZoom:(UIScrollView *)scrollView{
}
//確定是否可以滾動到頂部
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView{ return YES; }
//滾動到頂部時呼叫
- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView{
}
//已經滾動時呼叫
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
}
//開始進行拖動時呼叫
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
}
//擡起手指停止拖動時呼叫,布林值確定滾動到最後位置時是否需要減速
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
}
//如果上面的方法決定需要減速繼續滾動,則呼叫該方法,可以讀取contentOffset屬性,判斷使用者擡手位置(不是最終停止位置)
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
}
//減速完畢停止滾動時呼叫,這裡的讀取contentOffset屬性就是最終停止位置
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
}