UIScrollView是全部滑動視圖的基類spa
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];事件
scroll.background = [UIColor redColor];it
scroll.contentSize = CGSizeMake(2000, 2000); //設置UIScrollView的內容區域大小, 當UIScrollView的內容大小大於UIScrollView的Frame的大小時,UIScrollView能夠滑動.io
scroll.contentOffset = CGPointMake(200, 0); //設置UIScrollView的內容偏移量.event
scroll.showHorizontalScrollIndicator = NO; //關閉水平的滑動指示條class
scroll.showVerticalScrollIndicator = NO; //關閉豎直的滑動指示條scroll
scroll.scrollEnabled = NO; //禁止UIScrollView滑動方法
scroll.pagingEnabled = YES; //容許UIScrollView整頁滑動im
scroll.directionalLockEnabled = YES; //設置UIScrollView的單向滑動touch
scroll.bounces = NO; //禁止UIScrollView的邊界反彈效果.
scroll.alwaysBounceHorizontal = YES; //即便內容的size大小小於ScrollView的frame的大小, 依然具備邊界反彈效果.
scroll.alwaysBounceVertical = YES;
scroll.scrolsToTop = NO; //禁止點擊狀態條滑動到頂部.
scroll.maximumZoomScale = 4.0; //設置UIScrollView的縮放最大最小值
scroll.minimumZoomScale = 0.5;
拓展
UIScrollView:
視圖的滾動過程,實際上是在不斷修改原點座標。當手指觸摸後,ScrollView會暫時攔截觸摸事件,使用一個計時器。假如在計時器到點後沒有發生手指移動事件,那麼ScrollView發送tracking events到被點擊的subView;如果在計時器到點後發生了移動事件,那麼ScrollView取消tracking本身促發滾動。
一、屬性delaysContentTouches,布爾類型,默認值爲YES。值爲YES時,UIScrollView會在接收到手勢時延遲150ms來判斷該手勢是否可以出發UIScrollView的滑動事件;值爲NO時,UIScrollView會立馬將接收到的手勢分發到子視圖上。
二、方法- (BOOL)touchesShouldCancelInContentView:(UIView *)view,此方法的重載是幫助咱們完美解決問題的重點,決定手勢是否取消傳遞到view上,拖動ScrollView時觸發。返回NO時,拖動手勢將留在ScrollView上,返回YES則將手勢傳到view上。(若view是UIControl,則默認返回YES) 可這樣重載:
- (BOOL)touchesShouldCancelInContentView:(UIView *)view {
if ([view isKindOfClass:[UIButton class]]) {
return YES;
}
return [super touchesShouldCancelInContentView:view];
}