1. 程式人生 > >IOS開發之UIView總結

IOS開發之UIView總結

如果想呼叫某個類的某個方法可以寫成這樣,這個方法來自NSObject類

  1. performSelector:  
  2. performSelector:withObject:  
  3. performSelector:withObject:withObject:  

 實際呼叫

  1. [self performSelector:@selector(displayViews) withObject:nil afterDelay:1.0f];  

  有三個方法分別是

  1. //父檢視   
  2. [self.view superview]  
  3. //所有子檢視
      
  4.  [self.view subviews]  
  5. //自身的window  
  6.  self.view.window  

迴圈一個檢視下面所有檢視的方法

  1. NSArray *allSubviews(UIView *aView)  
  2. {  
  3.     NSArray *results = [aView subviews];  
  4.     for (UIView *eachView in [aView subviews])  
  5.     {  
  6.         NSArray *riz = allSubviews(eachView);  
  7.         if (riz) {  
  8.             results = [results arrayByAddingObjectsFromArray:riz];  
  9.         }  
  10.     }  
  11.     return results;  
  12. }  

迴圈返回一個APPLICATION裡面所有的VIEW

  1. // Return all views throughout the application  
  2. NSArray *allApplicationViews()  
  3. {  
  4.     NSArray *results = [[UIApplication sharedApplication] windows];  
  5.     for (UIWindow *window in [[UIApplication sharedApplication] windows])  
  6.     {  
  7.         NSArray *riz = allSubviews(window);  
  8.         if (riz) results = [results arrayByAddingObjectsFromArray: riz];  
  9.     }  
  10.     return results;  
  11. }  

 找出所有的父檢視

  1. // Return an array of parent views from the window down to the view  
  2. NSArray *pathToView(UIView *aView)  
  3. {  
  4.     NSMutableArray *array = [NSMutableArray arrayWithObject:aView];  
  5.     UIView *view = aView;  
  6.     UIWindow *window = aView.window;  
  7.     while (view != window)  
  8.     {  
  9.         view = [view superview];  
  10.         [array insertObject:view atIndex:0];  
  11.     }  
  12.     return array;  
  13. }  

UIView提供了大量管理檢視的方法

  1. //加一個檢視到一個視圖裡面  
  2. addSubview:  
  3. //將一個檢視移到前面  
  4. bringSubviewToFront:  
  5. //將一個檢視推送到背後  
  6. sendSubviewToBack:  
  7. //把檢視移除  
  8. removeFromSuperview  
  9. //插入檢視 並指定索引  
  10. insertSubview:atIndex:  
  11. //插入檢視在某個檢視之上  
  12. insertSubview:aboveSubview:  
  13. //插入檢視在某個檢視之下  
  14. insertSubview:belowSubview:  
  15. //交換兩個位置索引的檢視  
  16. exchangeSubviewAtIndex:withSubviewAtIndex:  

檢視回撥

  1. //當加入檢視完成後呼叫  
  2. (void)didAddSubview:(UIView *)subview  
  3. //當檢視移動完成後呼叫  
  4. (void)didMoveToSuperview  
  5. //當檢視移動到新的WINDOW後呼叫  
  6. (void)didMoveToWindow  
  7. //在刪除檢視之後呼叫  
  8. (void)willRemoveSubview:(UIView *)subview  
  9. //當移動檢視之前呼叫  
  10. (void)didMoveToSuperview:(UIView *)subview  
  11. //當檢視移動到WINDOW之前呼叫  
  12. (void)didMoveToWindow  

 給UIView設定標記和檢索檢視

  1. myview.tag = 1001;  
  2. [self.view viewWithTag:1001];  
  3. (UILable *)[self.view.window viewWithTag:1001];  

檢視的幾何特徵

  1. //框架  
  2. struct CGPoint {  
  3.   CGFloat x;  
  4.   CGFloat y;  
  5. };  
  6. typedef struct CGPoint CGPoint;  
  7. /* Sizes. */  
  8. struct CGSize {  
  9.   CGFloat width;  
  10.   CGFloat height;  
  11. };  
  12. typedef struct CGSize CGSize;  
  13. struct CGRect {  
  14.   CGPoint origin;  
  15.   CGSize size;  
  16. };  
  17. typedef struct CGRect CGRect;  
  18. CGRect rect = CGRectMake(0,0,320,480);  
  19. UIView *view = [[UIView allow]initWithFrame:rect];  
  20. //將String轉成CGPoint 如 @”{3.0,2.5}”    {x,y}  
  21. CGPoint CGPointFromString (  
  22.    NSString *string  
  23. );  
  24. //將String轉成CGRect  @”{{3,2},{4,5}}”  {{x,y},{w, h}}  
  25. CGRect CGRectFromString (  
  26.    NSString *string  
  27. );  
  28. //將String轉成CGSize @”{3.0,2.5}” {w, h}  
  29. CGSize CGSizeFromString (  
  30.    NSString *string  
  31. );  
  32. //CGPoint轉成NSString  
  33. NSString * NSStringFromCGPoint (  
  34.    CGPoint point  
  35. );  
  36. //CGRect轉成NSString  
  37. NSString * NSStringFromCGRect (  
  38.    CGRect rect  
  39. );  
  40. //CGSize轉成NSString  
  41. NSString * NSStringFromCGSize (  
  42.    CGSize size  
  43. );  
  44. //對一個CGRect進行修改 以這個的中心來修改 正數表示更小(縮小) 負數表示更大(放大)  
  45. CGRect CGRectInset (  
  46.    CGRe