UIScrollView

常見屬性:
@property (nonatomic) CGPoint contentOffset;// 表示UIScrollView的滾動位置 
@property (nonatomic) CGSize contentSize;// 表示UIScrollView內(nèi)容的尺寸,即滾動范圍
@property (nonatomic) UIEdgeInsets contentInset;// 能夠在UIScrollView四周增加額外的邊框
self.scrollView.contentOffset = CGPointMake(CGFloat x, CGFloat y);
self.scrollView.contentSize = CGSizeMake(CGFloat width, CGFloat height);
self.scrollView.contentInset = UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right);
其他屬性:
@property (nonatomic) BOOL bounces;// 設置是否需要彈簧效果
@property (nonatomic, getter = isDcrollEnabled) BOOL scrollEnabled;// 設置是否能滾動
@property (nonatomic) BOOL showHorizontalScrollIndicator;// 是否顯示水平滾動條
@property (nonatomic) BOOL showVerticalScrollIndicator;// 是否顯示垂直滾動條

UIScrollView代理(delegate)

當用戶開始拖拽,調(diào)用delegate的scrollViewWillBeginDragging:方法;
當用戶正在滾動,調(diào)用delegate的scrollViewDidScroll:方法;
當用戶停止拖拽,調(diào)用delegate的scrollViewDidEndDragging:willDecelerate:方法;
當用戶使用捏合手勢時調(diào)用delegate的viewForZoomingInScrollView:方法,其返回的控件就是將要縮放的控件;
當用戶正在捏合,調(diào)用delegate的scrollViewDidZoom:方法。

成為UIScrollView的delegate條件:

UIScrollView將delegate需要實現(xiàn)的方法都定義在了UIScrollViewDelegate協(xié)議中,因此想要成為UIScrollView的delegate,必需遵守UIScrollViewDelegate協(xié)議。

實現(xiàn)代理的方法:
通過代碼:self.scrollView.delegate = self; 通過storyboard右擊scrollView,從delegate拖拽到ViewController
讓控制器實現(xiàn)代理協(xié)議:@interface ViewController () <UIScrollViewDelegate>
實現(xiàn)協(xié)議中定義的方法。


UIScrollView的縮放

設置最大/最小縮放值:
self.scrollView.maximumZoomScale = 2.0; // 最大放大值
self.scrollView.minimumZoomScale = 0.5; //  最小縮小值
調(diào)用delegate的方法:
viewForZoomingInScrollView:

UIScrollView的分頁

// 根據(jù)scrollView的寬度分頁
self.scrollView.pagingEnabled = YES;
UIPageControl
self.pageControl.numberOfPages = ;// 設置pageControl總頁數(shù)
self.pageControl.currentPage = ;// 設置當前頁小圓點0,1,2,...

然后通過UIScrollView的代理方法監(jiān)聽滾動位置情況,利用scrollView.contentOffset.x的值,從而改變pageControl.currentPage的值來小圓點的顯示。

注意:UIPageControl支持用戶點擊哪邊向哪邊切換

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容