IOS開發之UIView總結
阿新 • • 發佈:2019-02-17
如果想呼叫某個類的某個方法可以寫成這樣,這個方法來自NSObject類
- performSelector:
- performSelector:withObject:
- performSelector:withObject:withObject:
實際呼叫
- [self performSelector:@selector(displayViews) withObject:nil afterDelay:1.0f];
有三個方法分別是
- //父檢視
- [self.view superview]
-
//所有子檢視
- [self.view subviews]
- //自身的window
- self.view.window
迴圈一個檢視下面所有檢視的方法
- NSArray *allSubviews(UIView *aView)
- {
- NSArray *results = [aView subviews];
- for (UIView *eachView in [aView subviews])
- {
- NSArray *riz = allSubviews(eachView);
-
if (riz) {
- results = [results arrayByAddingObjectsFromArray:riz];
- }
- }
- return results;
- }
迴圈返回一個APPLICATION裡面所有的VIEW
- // Return all views throughout the application
- NSArray *allApplicationViews()
- {
-
NSArray *results = [[UIApplication sharedApplication] windows];
- for (UIWindow *window in [[UIApplication sharedApplication] windows])
- {
- NSArray *riz = allSubviews(window);
- if (riz) results = [results arrayByAddingObjectsFromArray: riz];
- }
- return results;
- }
找出所有的父檢視
- // Return an array of parent views from the window down to the view
- NSArray *pathToView(UIView *aView)
- {
- NSMutableArray *array = [NSMutableArray arrayWithObject:aView];
- UIView *view = aView;
- UIWindow *window = aView.window;
- while (view != window)
- {
- view = [view superview];
- [array insertObject:view atIndex:0];
- }
- return array;
- }
UIView提供了大量管理檢視的方法
- //加一個檢視到一個視圖裡面
- addSubview:
- //將一個檢視移到前面
- bringSubviewToFront:
- //將一個檢視推送到背後
- sendSubviewToBack:
- //把檢視移除
- removeFromSuperview
- //插入檢視 並指定索引
- insertSubview:atIndex:
- //插入檢視在某個檢視之上
- insertSubview:aboveSubview:
- //插入檢視在某個檢視之下
- insertSubview:belowSubview:
- //交換兩個位置索引的檢視
- exchangeSubviewAtIndex:withSubviewAtIndex:
檢視回撥
- //當加入檢視完成後呼叫
- (void)didAddSubview:(UIView *)subview
- //當檢視移動完成後呼叫
- (void)didMoveToSuperview
- //當檢視移動到新的WINDOW後呼叫
- (void)didMoveToWindow
- //在刪除檢視之後呼叫
- (void)willRemoveSubview:(UIView *)subview
- //當移動檢視之前呼叫
- (void)didMoveToSuperview:(UIView *)subview
- //當檢視移動到WINDOW之前呼叫
- (void)didMoveToWindow
給UIView設定標記和檢索檢視
- myview.tag = 1001;
- [self.view viewWithTag:1001];
- (UILable *)[self.view.window viewWithTag:1001];
檢視的幾何特徵
- //框架
- struct CGPoint {
- CGFloat x;
- CGFloat y;
- };
- typedef struct CGPoint CGPoint;
- /* Sizes. */
- struct CGSize {
- CGFloat width;
- CGFloat height;
- };
- typedef struct CGSize CGSize;
- struct CGRect {
- CGPoint origin;
- CGSize size;
- };
- typedef struct CGRect CGRect;
- CGRect rect = CGRectMake(0,0,320,480);
- UIView *view = [[UIView allow]initWithFrame:rect];
- //將String轉成CGPoint 如 @”{3.0,2.5}” {x,y}
- CGPoint CGPointFromString (
- NSString *string
- );
- //將String轉成CGRect @”{{3,2},{4,5}}” {{x,y},{w, h}}
- CGRect CGRectFromString (
- NSString *string
- );
- //將String轉成CGSize @”{3.0,2.5}” {w, h}
- CGSize CGSizeFromString (
- NSString *string
- );
- //CGPoint轉成NSString
- NSString * NSStringFromCGPoint (
- CGPoint point
- );
- //CGRect轉成NSString
- NSString * NSStringFromCGRect (
- CGRect rect
- );
- //CGSize轉成NSString
- NSString * NSStringFromCGSize (
- CGSize size
- );
- //對一個CGRect進行修改 以這個的中心來修改 正數表示更小(縮小) 負數表示更大(放大)
- CGRect CGRectInset (
- CGRe