一、使用步驟:
1.添加子組件到scrollview //必要步驟
2.設置clipsToBounds來確定超出范圍是否被剪裁 (默認yes)
self.scrolltest.clipsToBounds=YES;
3.設置滾動范圍 。 //必要步驟
(這里的滾動范圍是設置的contentsize寬高減去scrollview的寬高,所以當contentsize寬高小於等於scrollview的寬高的時候,是不可滾動的)
scrollview.contentSize=CGSizeMake(
二、scrollview不可滾動的原因
1.沒有正確設置scrollview.contentSize
2..scrollEnabled屬性的設置(默認為yes) self.scrolltest.scrollEnabled=NO;
3.userInteractionEnabled屬性來設置是否可以和用戶交互 self.scrolltest.userInteractionEnabled=NO;
scrollEnabled和userInteractionEnabled的區別:
scrollEnabled只能決定是否能滾動;
userInteractionEnabled 影響滾動 。 事件 已經scrollview子組件的所有事件
三、scrollview基本屬性
1.是否有彈簧效果
self.scrollview.bounces=No; (默認是yes)
self.scrollview.alwaysBounceHorizontal //水平
self.scrollview.alwaysBounceVertical //垂直
ps:可不可以滾動和有沒有彈簧效果是不想關的
2.是否顯示滾動條
showsVerticalScrollIndicator; //垂直滾動條
showsHorizontalScrollIndicator; //水平滾動條
3. scrollview的子控件 scrollview.subviews
ps:scrollview的子組件中默認有兩個滾動條組件 。
4.設置內容的偏移量
scrollview.contentoffset=cgpoinntmake(x,y)
5.cgpoint 屬性的用法
cgpoint offset=self.scrollview.contentoffset; //第一步取出屬性
offset.x=12; //設置屬性
offset.y=13;
self.scrollview.contentoffset=offset; //賦值屬性
6.設置內邊距
self.scrollview.contentInset=UIEdgeInsetsMake(10, 20, 30, 40);
四、scrollview代理 (其方法不是必須實現的)
步驟:
1、遵守協議
2.設置代理
3.方法實現
(1) scrollview滾動過程中,自動調用的方法;(慣性滾動也會調用)
-(void)scrollViewDidScroll:(UIScrollView *)scrollView;
(2) scrollview 將要滾動的時候調用
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView;
(3)scrollview將要停止滾動時,調用
-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
(4)scrollview 已經停止滾動時,調用
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate;
(5)scrollview滾動減速完畢后,調用 。 (ps:並不是每次拖拽都會有減速情況;所以如果要判斷scrollview是否停止滾動,可以用scrollViewDidEndDecelerating,scrollViewDidEndDragging一起使用來判斷)
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;
(6)設置放大於縮小
1.設置scrollview的代理
2.實現如下方法
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return self.imageview;
}
3.設置scrollview放大縮小的最大最小值
scroll.maximumZoomScale=2.0;
scroll.minimumZoomScale=0.5;
(7)正在所發過程中調用的方法
-(void)scrollViewDidZoom:(UIScrollview *)scorllview;
五、scrollview . 分頁功能
scrollview.pagingEnabled=YES; //默認是no
