UI基礎視圖----UIView總結,ui視圖----uiview
UIView是UIKit架構裡面最基礎的視圖類,是UIResponder的子類,是UIApplication和UIViewController的兄弟類,是UIWindow,UILabel,UIImageView,UIScrollView,UIControl等的父類,是UIButton,UITextField的父父類(它們是UIControl的子類),是UITableView,UICollectionView的父父類(它們是UIScrollView的子類)。在UIKit架構中掌握各個視圖之間的繼承關係是很重要的,比如知道了UILabel的父類是UIView,那麼UIView的很多屬性和方法UILabel繼承過去就可以直接用。同樣的,UIView繼承於UIResponder,瞭解UIResponder的屬性和方法同樣是有必要的,會在UIResponder的總結中寫出來。
因為UIView是很多常用視圖控制項的父類,它的屬性和方法可以被直接繼承到子類。所以,瞭解UIView的常用屬性和方法就顯得尤為重要,這裡做一個簡單的總結,一些細節異同點注意點後續會繼續完善。
根據官方文檔,UIView根據不同的用途可以分為7大常用模組(當然不止這些),分別是基礎配置,幾何學配置,層級關係,渲染,動畫,手勢,約束。
0:準備工作
首先添加兩個UIView的屬性
1 @property (nonatomic, strong) UIView * view1;2 @property (nonatomic, strong) UIView * view2;
1:基礎配置
基礎配置主要包括執行個體化,使用者互動,tag值。
注意:
1:使用者互動:UIView的使用者互動是預設開啟的,它的子類中UILabel和UIimageView的使用者互動預設是關閉的(因為這兩個偏重展示文字或者圖片),所以其他繼承於UIView的子類,使用者互動預設是開啟的,但是上面兩個是關閉的,如果需要使用互動,需要把它們的使用者互動開啟(如在圖片上添加手勢)。
2:tag值:一般來說,我們自己建立的tag值要寫的大一點並且有規律一些,因為小的tag值可能和系統定義的衝突,有規律方便使用。
1 /** 2 * 1:基礎配置 3 */ 4 - (void)basisConfig 5 { 6 //1:常用的兩種執行個體化方式 7 self.view1 = [[UIView alloc] init]; 8 self.view2 = [[UIView alloc] initWithFrame:CGRectMake(100, 300, 200, 100)]; 9 //2:基礎屬性10 self.view1.userInteractionEnabled = YES;//使用者互動,預設是開啟的11 self.view2.userInteractionEnabled = NO;12 //3:tag值13 self.view1.tag = 1001;14 self.view2.tag = 1002;15 }
2:幾何學配置
幾何學主要包括frame,bounds,center,transform四種
注意:
1:frame,bounds,center的區別和關係:frame是相對於父視圖來說的,包括座標和尺寸。bounds是相對於本身來說的,座標是(0,0),center就是視圖的中心點座標。
2:transform兩種寫法的區別:make那種寫法是相對於初始視圖的,就是不管後面做什麼樣的變換,這個變換都是相對於初始視圖進行變換的,所以有可能出現後面的變換把前面的覆蓋掉。另一種寫法就是相對於上一種變換的,所以可以實現連續變換。
1 /** 2 * 2:幾何學配置 3 */ 4 - (void)geometryConfig 5 { 6 //1:frame 7 self.view1.frame = CGRectMake(100, 100, 200, 100); 8 //2:bounds 9 self.view1.bounds = CGRectMake(0, 0, 200, 100);10 self.view2.bounds = CGRectMake(0, 0, 200, 100);11 //3:center12 self.view1.center = CGPointMake(self.view.center.x, self.view.center.y);13 self.view2.center = self.view.center;//和上面效果相同14 //4:transform15 self.view1.transform = CGAffineTransformMakeRotation(0.1);//旋轉16 self.view2.transform = CGAffineTransformRotate(self.view2.transform, 0.7);//旋轉17 self.view1.transform = CGAffineTransformMakeTranslation(50, 50);//平移18 self.view2.transform = CGAffineTransformTranslate(self.view2.transform, -100, 100);//平移19 self.view1.transform = CGAffineTransformMakeScale(0.5, 0.5);//比例縮放20 self.view2.transform = CGAffineTransformScale(self.view2.transform, 0.5, 0.5);//比例縮放 21 }
3:層級關係
層級關係主要包括視圖之間各種層級關係以及對視圖之間層級關係的操作。方法很靈活,幾乎可以實現任何層視圖的切換,添加,刪除,插入,放到最上面,放到最下面,交換等。
1 /** 2 * 3:層級關係 3 */ 4 - (void)hierarchyConfig 5 { 6 //1:添加到父視圖 7 [self.view addSubview:self.view1];//視圖來的時候父視圖把你帶來 8 [self.view addSubview:self.view2]; 9 //父視圖管理子視圖的思想,其實父視圖上面應該有個子視圖數組,來管理在它上面所有的子視圖10 UIView * view3 = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 50, 50)];11 view3.backgroundColor = [UIColor orangeColor];12 //2:三種插入方式13 [self.view insertSubview:view3 aboveSubview:self.view1];14 [self.view insertSubview:view3 belowSubview:self.view2];15 [self.view insertSubview:view3 atIndex:0];16 //3:bring send17 [self.view bringSubviewToFront:self.view1];18 [self.view sendSubviewToBack:self.view2];//父視圖來操作,把它放到兄弟視圖的最下面19 //4:交換20 [self.view exchangeSubviewAtIndex:1 withSubviewAtIndex:0];//按順序交換視圖21 //5:移除22 [view3 removeFromSuperview];//走的時候自己走,來的時候父視圖把你帶來23 //6:兩個屬性24 NSLog(@"----%@----%@",self.view.subviews, self.view1.superview);25 26 }
4:渲染
渲染主要是常用的背景顏色,切邊界,透明度,顯示隱藏
注意:
切邊界:切邊界是父視圖做的事情,父視圖來切子視圖,這也體現了父視圖管理子視圖的核心思想。
隱藏屬性:實際開發中,隱藏屬性可以協助我們通過捷徑修改一些展示效果,很多時候不需要刪除,隱藏就行了。
1 /** 2 * 4:渲染 3 */ 4 - (void)renderingConfig 5 { 6 //1:背景顏色 7 self.view1.backgroundColor = [UIColor orangeColor]; 8 //2:切邊界 9 UIView * view4 = [[UIView alloc] initWithFrame:CGRectMake(10, 0, 50, 200)];10 view4.backgroundColor = [UIColor blueColor];11 [self.view1 addSubview:view4];12 self.view1.clipsToBounds = YES;//父視圖做的事情,讓子視圖切到自己的邊界13 //3:透明度14 view4.alpha = 0.3;//View內建,不用Image15 //4:顯示隱藏16 view4.hidden = YES;//視圖的隱藏屬性17 18 }
5:動畫
UIView中封裝了一些動畫屬性,可以讓我們在不實用Core Animation的情況下就能實現很多動畫效果。實現方式也是多種多樣,iOS7以後甚至封裝了主要畫面格動畫,可見,在開發中,如果對動畫效果沒有很高的要求,UIView內建的動畫效果是足夠用的。這裡簡單的實現一下用block方式實現的基礎動畫。
注意:
動畫改變的是什嗎?是視圖的屬性,比如frame,backgroundColor,alpha等,而且動畫效果一般都是類方法,直接用UIView調用,把需要改變的屬性末狀態寫在block塊中即可。
1 /** 2 * 5:動畫 3 */ 4 - (void)animationBlockConfig 5 { 6 //第一種 7 [UIView animateWithDuration:5 animations:^{ 8 self.view1.alpha = 0.1; 9 }];10 //第二種11 [UIView animateWithDuration:5 animations:^{12 self.view2.alpha = 0.1;13 } completion:^(BOOL finished) {14 self.view2.alpha = 1.0;15 }];16 //第三種17 [UIView animateWithDuration:5 delay:6 options:UIViewAnimationOptionBeginFromCurrentState animations:^{18 self.view2.frame = CGRectMake(0, 0, 100, 100);19 20 } completion:nil];21 22 }
6:手勢
手勢主要包括添加手勢和移除手勢。
1 /** 2 * 6:手勢 3 */ 4 - (void)gestureRecognizersConfig 5 { 6 UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] init]; 7 //添加手勢 8 [self.view1 addGestureRecognizer:tap]; 9 //移除手勢10 [self.view1 removeGestureRecognizer:tap];11 12 }
7:約束
約束在開發中也是很重要的,添加約束的方式也有很多種,通過系統封裝好的類,xib拖線,第三方庫,VFL語言等等很多方式,這裡就是View本身添加約束和刪除約束兩個基本方法
1 /** 2 * 7:約束 3 */ 4 - (void)constraintConfig 5 { 6 NSLayoutConstraint * constraint = [[NSLayoutConstraint alloc] init]; 7 //添加約束 8 [self.view1 addConstraint:constraint]; 9 //添加約束組10 [self.view1 addConstraints:@[constraint]];11 //刪除約束12 [self.view1 removeConstraint:constraint];13 //刪除約束組14 [self.view1 removeConstraints:@[constraint]];15 16 }