UIScrollView簡介
-
什么是UIScrollView
- 移動設(shè)備的屏幕大小是極其有限的,因此直接展示在用戶眼前的內(nèi)容也相當(dāng)有限
- 當(dāng)展示的內(nèi)容較多,超出一個屏幕時,用戶可通過滾動手勢來查看屏幕以外的內(nèi)容
- 普通的UIView不具備滾動功能,不適合顯示過多的內(nèi)容
- UIScrollView是一個能夠滾動的視圖控件,可以用來展示大量的內(nèi)容,并且可以通過滾動查看所有的內(nèi)容
- 超出UIScrollView邊框的內(nèi)容會被自動隱藏
- 用戶可以用過手勢拖動來查看超出邊框并被隱藏的內(nèi)容
-
scrollView使用
- 將需要展示的內(nèi)容添加到UIScrollView中
- 設(shè)置UIScrollView的contentSize屬性,告訴UIScrollView所有內(nèi)容的尺寸,也就是告訴它滾動的范圍(能滾多遠(yuǎn),滾到哪里是盡頭)
-
UIScrollView無法滾動的可能原因
- 沒有設(shè)置contentSize
- scrollEnabled=NO
- 沒有接收到觸摸事件:userInteractionEnabled=NO
UIScrollView常見屬性
@property(nonatomic)CGPointcontentOffset;
//這個屬性用來表示UIScrollView滾動的位置(其實就是內(nèi)容左上角與scrollView左上角的間距值)
@property(nonatomic)CGSizecontentSize;
//這個屬性用來表示UIScrollView內(nèi)容的尺寸,滾動范圍(能滾多遠(yuǎn))
@property(nonatomic)UIEdgeInsetscontentInset;
//這個屬性能夠在UIScrollView的4周增加額外的滾動區(qū)域,一般用來避免scrollView的內(nèi)容被其他控件擋住
@property(nonatomic)BOOLbounces;
//設(shè)置UIScrollView是否需要彈簧效果
@property(nonatomic,getter=isScrollEnabled)BOOLscrollEnabled;
//設(shè)置UIScrollView是否能滾動
@property(nonatomic)BOOLshowsHorizontalScrollIndicator;
//是否顯示水平滾動條
@property(nonatomic)BOOLshowsVerticalScrollIndicator;
//是否顯示垂直滾動條
![Uploading 屏幕快照 2016-06-14 上午11.20.06_417453.png . . .]
UIScrollView代理
- 很多時候,我們想在UIScrollView正在滾動 或 滾動到某個位置或者 停止?jié)L動 時做一些特定的操作
- 要想完成上述功能,前提條件就是能夠監(jiān)聽到UIScrollView的整個滾動過程
- 當(dāng)UIScrollView發(fā)生一系列的滾動操作時,會自動通知它的代理(delegate)對象,給它的代理發(fā)送相應(yīng)的消息,讓代理得知它的滾動情況
- 也就是說,要想監(jiān)聽UIScrollView的滾動過程,就必須先給UIScrollView設(shè)置一個代理對象,然后通過代理得知UIScrollView的滾動過程
UIScrollView和delegate的通信
UIScrollView和delegate的通信
- UIScrollView將delegate需要實現(xiàn)的方法都定義在了UIScrollViewDelegate協(xié)議中,因此要想成為UIScrollView的delegate,必須遵守UIScrollViewDelegate協(xié)議,然后實現(xiàn)協(xié)議中相應(yīng)的方法,就可以監(jiān)聽UIScrollView的滾動過程了
//遵守協(xié)議
self.scrollView.delegate= self;
下面就附上一個用sb實現(xiàn)的小demo。頁面效果如下
頁面效果
中間的拖可以上下左右拖動,而點擊最上最下等四個按鈕圖片可以到相應(yīng)的位置。
第一步:添加控件
添加控件
第二步:添加scrollview屬性,關(guān)聯(lián)相關(guān)方法
@property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
//最左
- (IBAction)left:(id)sender
{
}
//最上
- (IBAction)top:(id)sender
{
}
//最下
- (IBAction)bottom:(id)sender
{
}
//最右
- (IBAction)right:(id)sender
{
}
第三步:初始化內(nèi)容,添加圖片
- (void)viewDidLoad {
[super viewDidLoad];
UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"ace"]];
[self.scrollView addSubview:imageView];
self.scrollView.backgroundColor = [UIColor lightGrayColor];
//設(shè)置內(nèi)容大小
self.scrollView.contentSize = imageView.image.size;
}
第四步:實現(xiàn)點擊方法
- (IBAction)left:(id)sender
{
//偏移量,記錄UIScrollView 滾動的位置,有多少內(nèi)容已經(jīng)超出左上角
[UIView animateWithDuration:0.5 animations:^{
self.scrollView.contentOffset = CGPointMake(0 , self.scrollView.contentOffset.y);
} completion:^(BOOL finished) {
NSLog(@"執(zhí)行完畢最左");
}];
//打印偏移量
NSLog(@"---%@",NSStringFromCGPoint(self.scrollView.contentOffset));
}
- (IBAction)top:(id)sender
{
CGPoint offset = CGPointMake(self.scrollView.contentOffset.x , 0);
[self.scrollView setContentOffset:offset animated:YES];
}
- (IBAction)bottom:(id)sender
{
CGFloat offsetY = self.scrollView.contentSize.height - self.scrollView.frame.size.height;
self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x , offsetY);
}
- (IBAction)right:(id)sender
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
CGFloat offsetX = self.scrollView.contentSize.width - self.scrollView.frame.size.width;
self.scrollView.contentOffset = CGPointMake(offsetX , self.scrollView.contentOffset.y);
[UIView commitAnimations];
}
這里貼上我的代碼鏈接scrollView的Github鏈接.
這里的內(nèi)容很多借鑒了小碼哥視頻中的,有什么問題可以直接問我,當(dāng)然想學(xué)習(xí)的我這邊也有些資源可以發(fā)給你,大家共同進(jìn)步。