UIKit
框架裏面的幾個視圖非常重要,UIScrollView
,UITableView
,UICollectionView
都是很常用。因爲功能強大,所以很多朋友都會弄混淆一些概念,那麼這篇文章就先從UIScrollView
着手,全面剖析裏面的各種屬性和方法,這樣用起來就會得心應手。
第一、UIScrollView的基本概念
是一個可以選擇滑動的視圖,用於顯示更多的內容,可以通過手勢放大或者縮小顯示更多的內容。
有兩個子類一個是UITableView,另一個是UITextView
第二、基本屬性:
scrollView=[[[UIScrollView alloc] initWithFrame:CGRectMake(0,0, 320,250)] autorelease];
scrollView.backgroundColor=[UIColor redColor];
//設置內容的大小
scrollView.contentSize=CGSizeMake(320*4,250);
//當超出邊界時表示是否可以反彈
scrollView.bounces=YES;
//是否分頁
scrollView.pagingEnabled=YES;
//是否滾動
scrollView.scrollEnabled=YES;
//是否顯示邊界
scrollView.showsHorizontalScrollIndicator=YES;
//設置indicator的風格
scrollView.indicatorStyle=UIScrollViewIndicatorStyleWhite;
//提示用戶
[scrollView flashScrollIndicators];
//設置內容的邊緣
scrollView.contentInset=UIEdgeInsetsMake(0,50, 50,0);
[self.viewaddSubview:scrollView];
UILabel *label=[[[UILabelalloc] initWithFrame:CGRectMake(320,210, 320,40)] autorelease];
label.backgroundColor=[UIColoryellowColor];
label.text=@"學習scroolView";
[scrollViewaddSubview:label];
//調轉到指定的offset
[scrollView setContentOffset:CGPointMake(320,0)];
利用UIPageController和UIScrollView實現滾動圖片
- 1 - (void)viewDidLoad
- {
- [super viewDidLoad];
- UIScrollView *ScrollView=[[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 180)] autorelease];
- ScrollView.delegate=self;
- ScrollView.scrollsToTop=YES;
- ScrollView.contentSize=CGSizeMake(320*5, 180);
- ScrollView.backgroundColor=[UIColor clearColor];
- ScrollView.pagingEnabled=YES;
- ScrollView.showsHorizontalScrollIndicator=NO;
- self.tableView.tableHeaderView=ScrollView;
- for (int index=0; index<5; index++) {
- UIImageView *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(index*320, 0, 320, 180)];
- imageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"image%d.jpg",index+1]];
- [ScrollView addSubview:imageView];
- [imageView release];
- }
- UIPageControl *pageControl=[[[UIPageControl alloc] initWithFrame:CGRectMake(0, 150, 320, 30)] autorelease];
- [self.tableView addSubview:pageControl];
- pageControl.numberOfPages=5;
- pageControl.tag=1001;
- }
- 1pragma mark - Table view data source
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- {
- return 1;
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- return 10;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- static NSString *CellIdentifier = @"Cell";
- UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
- if (cell==nil) {
- cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
- }
- cell.textLabel.text=[NSString stringWithFormat:@"row: %d",indexPath.row];
- return cell;
- }
- 1
- #pragma mark -UIScrollViewDelegate
- - (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
- {
- if ([scrollView isMemberOfClass:[UITableView class]]) {
- NSLog(@"HELLO");
- }else
- {
- int current=scrollView.contentOffset.x/320;
- NSLog(@"scrollview :%f",scrollView.contentOffset.x);
- UIPageControl *pageControl=(UIPageControl*)[self.view viewWithTag:1001];
- pageControl.currentPage=current;
- }
- }
contenSize
顧名思義,表示內容的寬和高,從圖上也能清晰的看到,就是表示一張圖的寬和高。
contentInset
這個就是在contentSize
外面加的白色的部分,圖上看的清清楚楚。
contentOffset
這個屬性是是個CGPoint
類型,表示現在視圖上看到的那個點,距離圖片真實原點的位置。
因爲contentOffset
的那個點不是從contentInset
的原點開始的,所以如果滑動到如圖上所示的位置,於是就有了負值。
在滾動的過程中,實際上就是contentOffset
的值在不斷變化,當手指觸摸後,UIScrollView
會暫時攔截觸摸事件,使用一個計時器。假如在計時器到點後沒有發生手指移動事件,那麼UIScrollView
發送
tracking events 到被點擊的 subview 上面。如果在計時器到點前發生了移動事件,那麼UIScrollView
取消
tracking 然後自己發生滾動。
可以重載子類
- (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view;
決定自己是否接受 touch 事件
- (BOOL)touchesShouldCancelInContentView:(UIView *)view;
開始發送 tracking messages 給 subview 的時候調用這個方法,決定是否發送 tracking messages 消息到 subview。
返回NO
-> 發送,表示不取消
返回YES
-> 不發送,表示會取消
@property(nonatomic,readonly,getter=isTracking) BOOL tracking;
當 touch 後還沒有拖動的時候值是YES
,否則NO
@property(nonatomic,readonly,getter=isZoomBouncing) BOOL zoomBouncing;
當內容放大到最大或者最小的時候值是YES
,否則NO
@property(nonatomic,readonly,getter=isZooming) BOOL zooming;
當正在縮放的時候值是YES
,否則NO
@property(nonatomic,readonly,getter=isDecelerating) BOOL decelerating;
當滾動後,手指放開但是還在繼續滾動中。這個時候是YES
,其他時候是NO
@property(nonatomic) CGFloat decelerationRate
設置手指放開後的減速率
@property(nonatomic) CGFloat maximumZoomScale;
表示放大的最大倍數
@property(nonatomic) CGFloat minimumZoomScale;
表示縮小的最小倍數
@property(nonatomic,getter=isPagingEnabled) BOOL pagingEnabled;
當值爲YES
的時候,就會產生翻頁那種效果
@property(nonatomic,getter=isScrollEnabled) BOOL scrollEnabled;
決定是否可以滾動
@property(nonatomic) BOOL delaysContentTouches;
當值爲YES
的時候,用戶一旦觸碰,然後再一定時間內沒有移動,UIScrollView
會發送
tracking events,然後用戶移動手指足夠長度觸發滾動事件,這個時候,UIScrollView
發送了-(void)touchesCancelled:(NSSet
*)touches withEvent:(UIEvent *)event
到 subview,然後UIScrollView
開始滾動。假如值爲NO
,UIScrollView
發送
tracking events 後,就算用戶移動手指,UIScrollView
也不會滾動。
@property(nonatomic) BOOL showsHorizontalScrollIndicator;
滾動時是否顯示水平滾動條
@property(nonatomic) BOOL showsVerticalScrollIndicator;
滾動時是否顯示垂直滾動條
@property(nonatomic) BOOL bounces;
默認是YES
,就是滾動超過邊界會有反彈回來的效果,如果設置爲NO
,那麼滾動到邊界就會立刻停止
@property(nonatomic) BOOL bouncesZoom;
這個效果反映在縮放上面,如果縮放超過最大縮放,就會有反彈效果,加入設置爲NO
,則達到最大或者最小的時候立刻停止
@property(nonatomic,getter=isDirectionalLockEnabled) BOOL directionalLockEnabled;
默認是NO
,可以在垂直和水平方向同時運動。當值爲YES
的時候,加入一開始是垂直或者水平運動,那麼接下來會鎖定另外一個方向的滾動。加入一開始是對角方向滾動,則不會禁止某個方向
</pre><br />@property(<span class="hljs-keyword" style="box-sizing: border-box; color: rgb(133, 153, 0);">nonatomic</span>) UIScrollViewIndicatorStyle indicatorStyle;
滾動條的樣式,基本只是設置顏色
@property(nonatomic) UIEdgeInsets scrollIndicatorInsets;
設置滾動條的位置
#pragma mark UIScrollViewDelegate
//只要滾動了就會觸發
- (void)scrollViewDidScroll:(UIScrollView *)scrollView;
{
// NSLog(@" scrollViewDidScroll");
NSLog(@"ContentOffset x is %f,yis %f",scrollView.contentOffset.x,scrollView.contentOffset.y);
}
//開始拖拽視圖
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView;
{
NSLog(@"scrollViewWillBeginDragging");
}
//完成拖拽
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate;
{
NSLog(@"scrollViewDidEndDragging");
}
//將開始降速時
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView;
{
NSLog(@"scrollViewWillBeginDecelerating");
}
//減速停止了時執行,手觸摸時執行
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;
{
NSLog(@"scrollViewDidEndDecelerating");
}
//滾動動畫停止時執行,代碼改變時出發,也就是setContentOffset改變時
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView;
{
NSLog(@"scrollViewDidEndScrollingAnimation");
}
//設置放大縮小的視圖,要是uiscrollview的subview
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView;
{
NSLog(@"viewForZoomingInScrollView");
return viewA;
}
//完成放大縮小時調用
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale;
{
viewA.frame=CGRectMake(50,0,100,400);
NSLog(@"scale between minimum and maximum. called after any 'bounce' animations");
}// scale between minimum and maximum. called after any 'bounce' animations
//如果你不是完全滾動到滾軸視圖的頂部,你可以輕點狀態欄,那個可視的滾軸視圖會一直滾動到頂部,那是默認行爲,你可以通過該方法返回NO來關閉它
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView;
{
NSLog(@"scrollViewShouldScrollToTop");
returnYES;
}
- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView;
{
NSLog(@"scrollViewDidScrollToTop");
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end