UIKit 框架之

UIKit框架裏面的幾個視圖非常重要,UIScrollViewUITableViewUICollectionView都是很常用。因爲功能強大,所以很多朋友都會弄混淆一些概念,那麼這篇文章就先從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)];

利用UIPageControllerUIScrollView實現滾動圖片


  1. 1 - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.     UIScrollView *ScrollView=[[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 180)] autorelease];  
  5.     ScrollView.delegate=self;  
  6.     ScrollView.scrollsToTop=YES;  
  7.     ScrollView.contentSize=CGSizeMake(320*5, 180);  
  8.     ScrollView.backgroundColor=[UIColor clearColor];  
  9.     ScrollView.pagingEnabled=YES;  
  10.     ScrollView.showsHorizontalScrollIndicator=NO;  
  11.     self.tableView.tableHeaderView=ScrollView;  
  12.       
  13.     for (int index=0; index<5; index++) {  
  14.         UIImageView *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(index*320, 0, 320, 180)];  
  15.        imageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"image%d.jpg",index+1]];  
  16.         
  17.         [ScrollView addSubview:imageView];  
  18.         [imageView release];  
  19.     }  
  20.       
  21.     UIPageControl *pageControl=[[[UIPageControl alloc] initWithFrame:CGRectMake(0, 150, 320, 30)] autorelease];  
  22.     [self.tableView addSubview:pageControl];  
  23.     pageControl.numberOfPages=5;  
  24.     pageControl.tag=1001;  
  25.   
  26. }  
  27.   
  28. 1pragma mark - Table view data source  
  29.   
  30. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView  
  31. {  
  32.     return 1;  
  33. }  
  34.   
  35. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  
  36. {  
  37.     return 10;  
  38. }  
  39.   
  40. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  41. {  
  42.     static NSString *CellIdentifier = @"Cell";  
  43.   
  44.     UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];  
  45.     if (cell==nil) {  
  46.         cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];  
  47.     }  
  48.     cell.textLabel.text=[NSString stringWithFormat:@"row: %d",indexPath.row];  
  49.       
  50.     return cell;  
  51. }  
  52. 1
  53. #pragma mark -UIScrollViewDelegate  
  54. - (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView  
  55. {  
  56.     if ([scrollView isMemberOfClass:[UITableView class]]) {  
  57.         NSLog(@"HELLO");  
  58.     }else  
  59.     {  
  60.         int current=scrollView.contentOffset.x/320;  
  61.         NSLog(@"scrollview :%f",scrollView.contentOffset.x);  
  62.         UIPageControl *pageControl=(UIPageControl*)[self.view viewWithTag:1001];  
  63.         pageControl.currentPage=current;  
  64.     }  
  65. }  


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(nonatomicCGFloat maximumZoomScale;

表示放大的最大倍數

@property(nonatomicCGFloat minimumZoomScale;

表示縮小的最小倍數

@property(nonatomic,getter=isPagingEnabled) BOOL pagingEnabled;

當值爲YES的時候,就會產生翻頁那種效果

@property(nonatomic,getter=isScrollEnabled) BOOL scrollEnabled;

決定是否可以滾動

@property(nonatomicBOOL delaysContentTouches;

當值爲YES的時候,用戶一旦觸碰,然後再一定時間內沒有移動,UIScrollView會發送 tracking events,然後用戶移動手指足夠長度觸發滾動事件,這個時候,UIScrollView發送了-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event到 subview,然後UIScrollView開始滾動。假如值爲NOUIScrollView發送 tracking events 後,就算用戶移動手指,UIScrollView也不會滾動。

@property(nonatomic)  BOOL showsHorizontalScrollIndicator;

滾動時是否顯示水平滾動條

@property(nonatomic)  BOOL showsVerticalScrollIndicator;

滾動時是否顯示垂直滾動條

@property(nonatomic)  BOOL bounces;

默認是YES,就是滾動超過邊界會有反彈回來的效果,如果設置爲NO,那麼滾動到邊界就會立刻停止

@property(nonatomicBOOL 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




發佈了26 篇原創文章 · 獲贊 7 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章