iOS 官方文檔 UIView

一、初始化方法

1、- initWithFrame:

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(10,100, self.view.frame.size.width-20,300)];

二、配置View的視圖外觀

1、backgroundColor? ? 設置背景顏色

view.backgroundColor=[UIColor orangeColor];

(1) backgroundColor顧名思義就是背景顏色,就是整個UIView子類的充滿視圖的顏色;

(2) 默認父視圖設置的背景顏色,如果子視圖的背景顏色也是default那么默認透明的視圖都是父視圖的背景顏色;

(3) 背景顏色對于視圖上的帶有線條的內容是無法改變的,比如字體顏色;

2、hidden? ? 是否隱藏

該屬性為BOOL值,用來表示UIView是否隱藏,默認值是NO。? 當值設為YES時:

1、當前的UIView和subview都會被隱藏,而不管subview的hidden值為多少。

2、當前UIView會從響應者鏈中移除,而響應者鏈中的下一個會成為第一響應者 總之,同alpha為0時的顯示效果相同。

view.hidden=NO;

3、alpha? ? 透明度設置

液晶顯示器是由一個個的像素點組成的,每個像素點都可以顯示一個由RGBA顏色空間組成的一種色值。其中的A就表示透明度alpha,UIView中alpha是一個浮點值,取值范圍0~1.0,表示從完全透明到完全不透明。

當把alpha的值設置成0以后:? 1、當前的UIView和subview都會被隱藏,而不管subview的alpha值為多少。

2、當前UIView會從響應者鏈中移除,而響應者鏈中的下一個會成為第一響應者 alpha的默認值是1.0。

另外,更改alpha值時,默認是有動畫效果的,這是因為圖層在Cocoa中是由Core

Animation中CALayer表示的,該動畫效果是CALayer的隱含動畫。當然也有辦法禁用此動畫效果。

view.alpha=0.5;

4、opaque? ? 是否透明

圖層疊加時,設置疊加部分圖層顯示的透明度。

view.opaque=NO;

5、tintColor

[[UIBarButtonItem? appearance]? setTintColor:[UIColor? blueColor]];

(1) tintColor字面意思也是色彩,痕跡,相當于是一個描述一個視圖中的線條的顏色,這與痕跡的中文釋義不謀而合;

(2) tintColor是描述線條輪廓的一種顏色,該顏色默認具有傳遞性,默認狀態下最底部的視圖的tintcolor會一直往上面的視圖傳遞;

(3) 如果子視圖改變了tintcolor那么將會和父視圖的tintColor不一樣;傳遞鏈從此處斷開;

6、tintAdjustmentMode

view.tintAdjustmentMode=UIViewTintAdjustmentModeNormal;

7、clipsToBounds

作用:決定了子視圖的顯示范圍。具體的說,就是當取值為YES時,剪裁超出父視圖范圍的子視圖部分;當取值為NO時,不剪裁子視圖。默認值為NO。

view.clipsToBounds=YES;

8、clearsContextBeforeDrawing

view.clearsContextBeforeDrawing=YES;

9、maskView

10、+ layerClass

11、layer

三、配置與事件相關的行為

1、userInteractionEnabled

(1) 該屬性值為布爾類型,如屬性本身的名稱所釋,該屬性決定UIView是否接受并響應用戶的交互。

(2)

當值設置為NO后,UIView會忽略那些原本應該發生在其自身的諸如touch和keyboard等用戶事件,并將這些事件從消息隊列中移除出去。當值設置為YES后,這些用戶事件會正常的派發至UIView本身(前提事件確實發生在該view上),UIView會按照之前注冊的事件處理方法來響應這些事件。

(3)

在一次動畫執行流程中,動畫包含的所有UIView都會被臨時禁止用戶交互,而不管每個UIView本身userInteractionEnabled此時的屬性值是YES還是NO。但是在配置動畫時,通過添加UIViewAnimationOptionAllowUserInteraction選項可以禁止這種行為的發生,使UIView即使是在執行動畫期間依然能響應用戶事件。

2、multipleTouchEnabled

multipleTouchEnabled? 默認是NO,如果設置為YES則支持多點觸碰。

3、exclusiveTouch

(1) 默認是NO,如果設置為YES則當前UIView會獨占整個Touch事件。具體來說就是如果UIView設置了exclusiveTouch屬性為YES則當這個UIView成為第一響應者時,在手指離開屏幕前其他view不會響應任何touch事件。

(2) 作用舉例:UITableView的每個cell都需要使用exclusive,否則同時點擊多個cell會觸發每個視圖的事件響應。手勢識別會忽略此屬性。

四、配置矩形的邊界框

1、frame

描述當前視圖在其父視圖中的位置和大小

2、bounds

描述當前視圖在其自身坐標系統中的位置和大小

3、center

描述當前視圖的中心點在其父視圖中的位置

4、transform

UIView有個transform的屬性,通過設置該屬性,我們可以實現調整該view在其superView中的大小和位置,具體來說,Transform(變化矩陣)是一種3×3的矩陣,通過這個矩陣我們可以對一個坐標系統進行縮放,平移,旋轉以及這兩者的任意組著操作。而且矩陣的操作不具備交換律,即矩陣的操作的順序不同會導致不同的結果。

常用的三種實現選中的方式:

view.transform=CGAffineTransformScale(view.transform, 0.5, 0.5); // 實現的是放大和縮小 view.transform=CGAffineTransformRotate(view.transform, 0.2); //實現的是旋轉 view.transform=CGAffineTransformTranslate(view.transform, 20, 20); //實現的是平移

由此可以發現屏幕旋轉其實就是通過view的矩陣變化實現,當設備監測到旋轉的時候,會通知當前程序,當前程序再通知程序中的window,window會通知它的rootViewController的,rootViewController對其view的transform進行設置,最終完成旋轉。

五、管理View的層次結構

1、superview

接收者的父類。

2、subviews

接收者的子類。

3、window

UIWindow對象是所有UIView的根視圖,管理和協調的應用程序的顯示、分發事件給View。UIWindow類是UIView的子類,可以看作是特殊的UIView。一般應用程序只有一個UIWindow對象,即使有多個UIWindow對象,也只有一個UIWindow可以接受到用戶的觸屏事件。

4、- addSubview:

添加一個子視圖到接收者并讓它在最上面顯示出來。

5、- bringSubviewToFront:

將某個子視圖調整到最上邊。

-(void)bringSubviewToFront:(UIView*)view

6、- sendSubviewToBack:

將某個子視圖調整到最下邊。

-(void)sendSubviewToBack:(UIView*)view

7、- removeFromSuperview

將子視圖從父視圖中移除掉。

8、- insertSubview:atIndex:

在特定的位置,插入一個子視圖。

view:被插入的子視圖。

index:被插入的位置下標,位置下標從0開始;下標不能大于子視圖的總數。

-(void)insertSubview:(UIView*)view atIndex:(NSInteger)index

9、- insertSubview:aboveSubview:

插入一個View到指定View的上層。

-(void)insertSubview:(UIView*)view aboveSubview:(UIView*)siblingSubView

10、- insertSubview:belowSubview:

插入一個View到指定View的下層。

-(void)insertSubview:(UIView*)view belowSubview:(UIView*)siblingSubView

11、-exchangeSubviewAtIndex:withSubviewAtIndex:

根據下標交換兩個層的位置。

(void)exchangeSubviewAtIndex:(NSInteger)indexwithSubviewAtIndex:(NSInteger)index

12、- isDescendantOfView:

返回一個布爾值指出接收者是否是給定視圖的子視圖或者指向那個視圖.

- (BOOL)isDescendantOfView:(UIView*)view 參數 view 一個視圖用來測試子視圖在視圖層次中的關系 返回值 如果接收者是視圖的子視圖就返回YES,或者視圖就是接收者;否則就是NO

六、配置布局行為

1、autoresizingMask

在 UIView 中有一個autoresizingMask的屬性,它對應的是一個枚舉的值(如下),屬性的意思就是自動調整子控件與父控件中間的位置,寬高。

enum {? UIViewAutoresizingNone? ? ? ? ? ? ? ? =0,UIViewAutoresizingFlexibleLeftMargin? =1<<0,UIViewAutoresizingFlexibleWidth? ? ? ? =1<<1,UIViewAutoresizingFlexibleRightMargin? =1<<2,UIViewAutoresizingFlexibleTopMargin? ? =1<<3,UIViewAutoresizingFlexibleHeight? ? ? =1<<4,UIViewAutoresizingFlexibleBottomMargin =1<<5};

UIViewAutoresizingNone就是不自動調整。

UIViewAutoresizingFlexibleLeftMargin 自動調整與superView左邊的距離,保證與superView右邊的距離不變。

UIViewAutoresizingFlexibleRightMargin 自動調整與superView的右邊距離,保證與superView左邊的距離不變。

UIViewAutoresizingFlexibleTopMargin 自動調整與superView頂部的距離,保證與superView底部的距離不變。

UIViewAutoresizingFlexibleBottomMargin 自動調整與superView底部的距離,也就是說,與superView頂部的距離不變。

UIViewAutoresizingFlexibleWidth 自動調整自己的寬度,保證與superView左邊和右邊的距離不變。

UIViewAutoresizingFlexibleHeight 自動調整自己的高度,保證與superView頂部和底部的距離不變。

UIViewAutoresizingFlexibleLeftMargin? |UIViewAutoresizingFlexibleRightMargin 自動調整與superView左邊的距離,保證與左邊的距離和右邊的距離和原來距左邊和右邊的距離的比例不變。比如原來距離為20,30,調整后的距離應為68,102,即68/20=102/30。

其它的組合類似。

2、autoresizesSubviews

//高度自動伸縮,右邊間距自動伸縮self.autoresizesSubviews = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleRightMargin;

UIViewAutoresizingNone

這個常量如果被設置,視圖將不進行自動尺寸調整。

UIViewAutoresizingFlexibleHeight

這個常量如果被設置,視圖的高度將和父視圖的高度一起成比例變化。否則,視圖的高度將保持不變。

UIViewAutoresizingFlexibleWidth

這個常量如果被設置,視圖的寬度將和父視圖的寬度一起成比例變化。否則,視圖的寬度將保持不變。

UIViewAutoresizingFlexibleLeftMargin

這個常量如果被設置,視圖的左邊界將隨著父視圖寬度的變化而按比例進行調整。否則,視圖和其父視圖的左邊界的相對位置將保持不變。

UIViewAutoresizingFlexibleRightMargin

這個常量如果被設置,視圖的右邊界將隨著父視圖寬度的變化而按比例進行調整。否則,視圖和其父視圖的右邊界的相對位置將保持不變。

UIViewAutoresizingFlexibleBottomMargin

這個常量如果被設置,視圖的底邊界將隨著父視圖高度的變化而按比例進行調整。否則,視圖和其父視圖的底邊界的相對位置將保持不變。

UIViewAutoresizingFlexibleTopMargin

這個常量如果被設置,視圖的上邊界將隨著父視圖高度的變化而按比例進行調整。否則,視圖和其父視圖的上邊界的相對位置將保持不變。

3、contentMode

UIViewContentMode 都有哪些值:typedefNS_ENUM(NSInteger, UIViewContentMode) {? ? UIViewContentModeScaleToFill,? ? UIViewContentModeScaleAspectFit,// contents scaled to fit with fixed aspect. remainder is transparentUIViewContentModeScaleAspectFill,// contents scaled to fill with fixed aspect. some portion of content may be clipped.UIViewContentModeRedraw,// redraw on bounds change (calls -setNeedsDisplay)UIViewContentModeCenter,// contents remain same size. positioned adjusted.UIViewContentModeTop,? ? UIViewContentModeBottom,? ? UIViewContentModeLeft,? ? UIViewContentModeRight,? ? UIViewContentModeTopLeft,? ? UIViewContentModeTopRight,? ? UIViewContentModeBottomLeft,? ? UIViewContentModeBottomRight,};默認值是0,也就是:UIViewContentModeScaleToFill

一個個來理解下吧:

UIViewContentModeScaleToFill:表示完全填充在 frame 里。

UIViewContentModeScaleAspectFit:保持比例,都在 frame 內。

UIViewContentModeScaleAspectFill:保持比例,填滿但 frame 外也有。

UIViewContentModeRedraw:啥意思我還不懂。

其他的是相似的,好理解:

UIViewContentModeCenter:這個 image 的中心與 frame 的中心重合。

UIViewContentModeTop:這個 image 的上邊緣與 frame 的上邊緣重合。

UIViewContentModeBottom:這個 image 的下邊緣與 frame 的下邊緣重合。

UIViewContentModeLeft:這個 image 的左邊緣與 frame 的左邊緣重合。

UIViewContentModeRight:這個 image 的右邊緣與 frame 的右邊緣重合。

UIViewContentModeTopLeft:類似。

UIViewContentModeTopRight:類似。

UIViewContentModeBottomLeft:類似。

UIViewContentModeBottomRight:類似。

4、- sizeThatFits:

讓視圖計算最適合子視圖的大小,即能把全部子視圖顯示出來所需要的最小的size

5、- sizeToFit

根據子視圖的大小位置,調整視圖,使其恰好圍繞子視圖,也就是說自動適應子視圖的大小,只顯示子視圖

七、Laying out Subviews

1、- layoutSubviews

2、- setNeedsLayout

3、- layoutIfNeeded

4、+ requiresConstraintBasedLayout

5、translatesAutoresizingMaskIntoConstraints

八、Creating Constraints Using Layout Anchors

1、bottomAnchor

2、centerXAnchor

3、centerYAnchor

4、firstBaselineAnchor

5、heightAnchor

6、lastBaselineAnchor

7、leadingAnchor

8、leftAnchor

9、rightAnchor

10、topAnchor

11、trailingAnchor

12、widthAnchor

九、Managing the View’s Constraints

1、constraints

2、- addConstraint:

3、- addConstraints:

4、- removeConstraint:

5、- removeConstraints:

十、Working with Layout Guides

1、- addLayoutGuide:

2、layoutGuides

3、layoutMarginsGuide

4、readableContentGuide

5、- removeLayoutGuide:

十一、Measuring in Auto Layout

1、- systemLayoutSizeFittingSize:

2、- systemLayoutSizeFittingSize:withHorizontalFittingPriority:verticalFittingPriority:

3、- intrinsicContentSize

4、- invalidateIntrinsicContentSize

5、- contentCompressionResistancePriorityForAxis:

6、- setContentCompressionResistancePriority:forAxis:

7、- contentHuggingPriorityForAxis:

8、- setContentHuggingPriority:forAxis:

十二、Aligning Views in Auto Layout

1、- alignmentRectForFrame:

2、- frameForAlignmentRect:

3、- alignmentRectInsets

4、- viewForBaselineLayout

5、viewForFirstBaselineLayout

6、viewForLastBaselineLayout

十三、Triggering Auto Layout

1、- needsUpdateConstraints

2、- setNeedsUpdateConstraints

3、- updateConstraints

4、- updateConstraintsIfNeeded

十四、Debugging Auto Layout

1、- constraintsAffectingLayoutForAxis:

2、- hasAmbiguousLayout

3、- exerciseAmbiguityInLayout

十五、Managing the User Interface Direction

1、semanticContentAttribute

2、+ userInterfaceLayoutDirectionForSemanticContentAttribute:

十六、Configuring Content Margins

1、layoutMargins

2、preservesSuperviewLayoutMargins

3、- layoutMarginsDidChange

十七、Drawing and Updating the View

1、- drawRect:

(1) 方法原型

-(void)drawRect:(CGRect)rect

(2) drawRect在以下情況下會被調用:

a、如果在UIView初始化時沒有設置rect大小,將直接導致drawRect不被自動調用。drawRect

掉用是在Controller->loadView, Controller->viewDidLoad 兩方法之后掉用的.所以不用擔心在

控制器中,這些View的drawRect就開始畫了.這樣可以在控制器中設置一些值給View(如果這些View draw的時候需要用到某些變量

值).

b、該方法在調用sizeToFit后被調用,所以可以先調用sizeToFit計算出size。然后系統自動調用drawRect:方法。

c、通過設置contentMode屬性值為UIViewContentModeRedraw。那么將在每次設置或更改frame的時候自動調用drawRect:。

以上1,2推薦;而3,4不提倡

(3) drawRect方法使用注意點:

a、

若使用UIView繪圖,只能在drawRect:方法中獲取相應的contextRef并繪圖。如果在其他方法中獲取將獲取到一個invalidate

的ref并且不能用于畫圖。drawRect:方法不能手動顯示調用,必須通過調用setNeedsDisplay 或 者

setNeedsDisplayInRect,讓系統自動調該方法。

b、若使用calayer繪圖,只能在drawInContext: 中(類似魚drawRect)繪制,或者在delegate中的相應方法繪制。同樣也是調用setNeedDisplay等間接調用以上方法

c、若要實時畫圖,不能使用gestureRecognizer,只能使用touchbegan等方法來掉用setNeedsDisplay實時刷新屏幕

2、- setNeedsDisplay

3、- setNeedsDisplayInRect:

4、contentScaleFactor

5、- tintColorDidChange

十八、Formatting Printed View Content

1、- viewPrintFormatter

2、- drawRect:forViewPrintFormatter:

十九、Managing Gesture Recognizers

1、- addGestureRecognizer:

2、- removeGestureRecognizer:

3、gestureRecognizers

4、- gestureRecognizerShouldBegin:

二十、UIView動畫塊

在iOS4.0 以后蘋果推出了動畫塊,代替了之前iOS2.0推出的UIView基本動畫,蘋果更推薦開發者使用動畫塊。

1、+ animateWithDuration:delay:options:animations:completion:

(1) 方法原型

+ (void)animateWithDuration:(NSTimeInterval)duration? ? ? ? ? ? ? ? ? ? ? delay:(NSTimeInterval)delay? ? ? ? ? ? ? ? ? ? options:(UIViewAnimationOptions)options? ? ? ? ? ? ? ? animations:(void(^)(void))animations? ? ? ? ? ? ? ? completion:(void(^)(BOOLfinished))completion//這個動畫塊方法是最全面的,可以設置動畫延遲執行、動畫屬性、動畫速度、動畫專場、動畫開始執行操作、動畫結束執行操作。

(2) 事例代碼

- (void)animationBlock1{UIImageView*imageView = [[UIImageViewalloc] initWithFrame:CGRectMake(100,100,self.view.frame.size.width-200,200)];? ? imageView.backgroundColor= [UIColororangeColor];? ? [self.viewaddSubview:imageView];? ? [UIViewanimateWithDuration:1.0delay:0.0options:UIViewAnimationOptionLayoutSubviews? ? ? ? ? ? ? ? ? ? animations:^{? ? ? ? ? ? ? ? ? ? ? ? imageView.backgroundColor= [UIColorredColor];NSLog(@"動畫開始");? ? ? ? ? ? ? ? ? ? }? ? ? ? ? ? ? ? ? ? completion:^(BOOLfinished) {? ? ? ? ? ? ? ? ? ? ? ? imageView.alpha=0.5;NSLog(@"動畫結束");? ? ? ? ? ? ? ? ? ? }];}

(3) option 參數詳解 (可以同時選擇多個進行設置)

a、常規動畫屬性設置

UIViewAnimationOptionLayoutSubviews:動畫過程中保證子視圖跟隨運動。

UIViewAnimationOptionAllowUserInteraction:動畫過程中允許用戶交互。

UIViewAnimationOptionBeginFromCurrentState:所有視圖從當前狀態開始運行。

UIViewAnimationOptionRepeat:重復運行動畫。

UIViewAnimationOptionAutoreverse :動畫運行到結束點后仍然以動畫方式回到初始點。

UIViewAnimationOptionOverrideInheritedDuration:忽略嵌套動畫時間設置。

UIViewAnimationOptionOverrideInheritedCurve:忽略嵌套動畫速度設置。

UIViewAnimationOptionAllowAnimatedContent:動畫過程中重繪視圖(注意僅僅適用于轉場動畫)。

UIViewAnimationOptionShowHideTransitionViews:視圖切換時直接隱藏舊視圖、顯示新視圖,而不是將舊視圖從父視圖移除(僅僅適用于轉場動畫)

UIViewAnimationOptionOverrideInheritedOptions :不繼承父動畫設置或動畫類型。

b、動畫速度控制(可從其中選擇一個設置)

UIViewAnimationOptionCurveEaseInOut:動畫先緩慢,然后逐漸加速。

UIViewAnimationOptionCurveEaseIn :動畫逐漸變慢。

UIViewAnimationOptionCurveEaseOut:動畫逐漸加速。

UIViewAnimationOptionCurveLinear :動畫勻速執行,默認值。

c、轉場類型(僅適用于轉場動畫設置,可以從中選擇一個進行設置,基本動畫、關鍵幀動畫不需要設置)

UIViewAnimationOptionTransitionNone:沒有轉場動畫效果。

UIViewAnimationOptionTransitionFlipFromLeft :從左側翻轉效果。

UIViewAnimationOptionTransitionFlipFromRight:從右側翻轉效果。

UIViewAnimationOptionTransitionCurlUp:向后翻頁的動畫過渡效果。

UIViewAnimationOptionTransitionCurlDown :向前翻頁的動畫過渡效果。

UIViewAnimationOptionTransitionCrossDissolve:舊視圖溶解消失顯示下一個新視圖的效果。

UIViewAnimationOptionTransitionFlipFromTop :從上方翻轉效果。

UIViewAnimationOptionTransitionFlipFromBottom:從底部翻轉效果。

2、+ animateWithDuration:animations:completion:

(1) 方法原型

+ (void)animateWithDuration:(NSTimeInterval)duration? ? ? ? ? ? ? ? animations:(void(^)(void))animations? ? ? ? ? ? ? ? completion:(void(^)(BOOLfinished))completion//這個動畫塊方法比較全面,可以設置動畫開始執行操作、動畫結束執行操作。

(2) 事例代碼

- (void)animationBlock2{UIImageView*imageView = [[UIImageViewalloc] initWithFrame:CGRectMake(100,100,self.view.frame.size.width-200,200)];? ? imageView.backgroundColor= [UIColororangeColor];? ? [self.viewaddSubview:imageView];? ? [UIViewanimateWithDuration:1.0animations:^{? ? ? ? ? ? ? ? ? ? ? ? imageView.backgroundColor= [UIColorredColor];NSLog(@"動畫開始");? ? ? ? ? ? ? ? ? ? }? ? ? ? ? ? ? ? ? ? completion:^(BOOLfinished) {? ? ? ? ? ? ? ? ? ? ? ? imageView.alpha=0.5;NSLog(@"動畫結束");? ? ? ? ? ? ? ? ? ? }];}

3、+ animateWithDuration:animations:

(1) 方法原型

+ (void)animateWithDuration:(NSTimeInterval)duration? ? ? ? ? ? ? ? animations:(void(^)(void))animations//這個動畫塊方法很簡潔,實現普通的動畫。

(2) 事例代碼

- (void)animationBlock3{UIImageView*imageView = [[UIImageViewalloc] initWithFrame:CGRectMake(100,100,self.view.frame.size.width-200,200)];? ? imageView.backgroundColor= [UIColororangeColor];? ? [self.viewaddSubview:imageView];? ? [UIViewanimateWithDuration:3.0animations:^{? ? ? ? ? ? ? ? ? ? ? ? imageView.backgroundColor= [UIColorredColor];? ? ? ? ? ? ? ? ? ? }];}

4、+ transitionWithView:duration:options:animations:completion:

(1) 方法原型

+ (void)transitionWithView:(UIView*)view? ? ? ? ? ? ? ? ? duration:(NSTimeInterval)duration? ? ? ? ? ? ? ? ? options:(UIViewAnimationOptions)options? ? ? ? ? ? ? ? animations:(void(^)(void))animations? ? ? ? ? ? ? ? completion:(void(^)(BOOLfinished))completion//動畫專場過渡效果

(2) 事例代碼

- (void)viewDidLoad {? ? [superviewDidLoad];UIButton*btn = [[UIButtonalloc] initWithFrame:CGRectMake(100,100,100,100)];? ? [btn setBackgroundColor:[UIColorredColor]];? ? [self.viewaddSubview:btn];? ? [btn addTarget:selfaction:@selector(animationBlock4) forControlEvents:UIControlEventTouchUpInside];}- (void)animationBlock4{? ? [UIViewtransitionWithView:self.viewduration:1.0options:UIViewAnimationOptionTransitionFlipFromLeft|UIViewAnimationOptionCurveEaseIn? ? ? ? ? ? ? ? ? ? animations:^{NSLog(@"動畫開始");? ? ? ? ? ? ? ? ? ? }? ? ? ? ? ? ? ? ? ? completion:^(BOOLfinished) {NSLog(@"動畫結束");? ? ? ? ? ? ? ? ? ? }];}

5、+ transitionFromView:toView:duration:options:completion:

(1) 方法原型

+ (void)transitionFromView:(UIView*)fromView? ? ? ? ? ? ? ? ? ? toView:(UIView*)toView? ? ? ? ? ? ? ? ? duration:(NSTimeInterval)duration? ? ? ? ? ? ? ? ? options:(UIViewAnimationOptions)options? ? ? ? ? ? ? ? completion:(void(^)(BOOLfinished))completion//動畫專場過渡效果

(2)? 事例代碼

- (void)viewDidLoad {? ? [superviewDidLoad];UIButton*btn = [[UIButtonalloc] initWithFrame:CGRectMake(10,20,50,40)];? ? [btn setBackgroundColor:[UIColorredColor]];? ? [self.viewaddSubview:btn];? ? [btn addTarget:selfaction:@selector(animationBlock5) forControlEvents:UIControlEventTouchUpInside];}- (void)animationBlock5{UIView*view1 = [[UIViewalloc] initWithFrame:CGRectMake(100,100,100,100)];? ? view1.backgroundColor= [UIColorredColor];? ? [self.viewaddSubview:view1];UIView*view2 = [[UIViewalloc] initWithFrame:CGRectMake(100,100,100,100)];? ? view2.backgroundColor= [UIColororangeColor];? ? [self.viewaddSubview:view2];? ? [UIViewtransitionFromView:view1? ? ? ? ? ? ? ? ? ? ? ? toView:view2? ? ? ? ? ? ? ? ? ? ? duration:3.0options:UIViewAnimationOptionTransitionFlipFromLeft|UIViewAnimationOptionCurveEaseIn? ? ? ? ? ? ? ? ? ? completion:^(BOOLfinished) {? ? ? ? ? ? ? ? ? ? }];}

6、+ animateKeyframesWithDuration:delay:options:animations:completion:

(1) 方法原型

+ (void)animateKeyframesWithDuration:(NSTimeInterval)duration? ? ? ? ? ? ? ? ? ? ? ? ? ? ? delay:(NSTimeInterval)delay? ? ? ? ? ? ? ? ? ? ? ? ? ? options:(UIViewKeyframeAnimationOptions)options? ? ? ? ? ? ? ? ? ? ? ? ? animations:(void(^)(void))animations? ? ? ? ? ? ? ? ? ? ? ? ? completion:(void(^)(BOOLfinished))completion//關鍵幀動畫

(2) 事例代碼

//關鍵幀動畫- (void)animationBlock6{? ? [UIViewanimateKeyframesWithDuration:6.f? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? delay:0.0options:UIViewKeyframeAnimationOptionCalculationModeLinear? ? ? ? ? ? ? ? ? ? ? ? ? ? ? animations:^{? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [UIViewaddKeyframeWithRelativeStartTime:0.0// 相對于6秒所開始的時間(第0秒開始動畫)relativeDuration:1/3.0// 相對于6秒動畫的持續時間(動畫持續2秒)animations:^{self.view.backgroundColor= [UIColorredColor];? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }];? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [UIViewaddKeyframeWithRelativeStartTime:1/3.0// 相對于6秒所開始的時間(第2秒開始動畫)relativeDuration:1/3.0// 相對于6秒動畫的持續時間(動畫持續2秒)animations:^{self.view.backgroundColor= [UIColoryellowColor];? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }];? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [UIViewaddKeyframeWithRelativeStartTime:2/3.0// 相對于6秒所開始的時間(第4秒開始動畫)relativeDuration:1/3.0// 相對于6秒動畫的持續時間(動畫持續2秒)animations:^{self.view.backgroundColor= [UIColorgreenColor];? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }];? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }? ? ? ? ? ? ? ? ? ? ? ? ? ? ? completion:^(BOOLfinished) {? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [selfanimationBlock6];? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }];}

(3) 補充說明

關鍵幀動畫animateKeyframesWithDuration方法需要和addKeyframeWithRelativeStartTime:relativeDuration:animations方法一起使用,否則就跟普通動畫一樣。

7、+ addKeyframeWithRelativeStartTime:relativeDuration:animations:

(1) 方法原型

+ (void)addKeyframeWithRelativeStartTime:(double)frameStartTime? ? ? ? ? ? ? ? ? ? ? ? relativeDuration:(double)frameDuration? ? ? ? ? ? ? ? ? ? ? ? ? ? ? animations:(void(^)(void))animations//指定時間和關鍵幀動畫

8、+ performSystemAnimation:onViews:options:animations:completion:

(1) 方法原型

+ (void)performSystemAnimation:(UISystemAnimation)animation? ? ? ? ? ? ? ? ? ? ? onViews:(NSArray<__kindofUIView*> *)views? ? ? ? ? ? ? ? ? ? ? options:(UIViewAnimationOptions)options? ? ? ? ? ? ? ? ? ? animations:(void(^)(void))parallelAnimations? ? ? ? ? ? ? ? ? ? completion:(void(^)(BOOLfinished))completion//系統動畫

(2) 在一組視圖上執行指定的系統動畫,并可以并行自定義的動畫。其中parallelAnimations就是與系統動畫并行的自定義動畫。

9、+ animateWithDuration:delay:usingSpringWithDamping:initialSpringVelocity:options:animations:completion:

(1) 方法原型

+ (void)animateWithDuration:(NSTimeInterval)duration? ? ? ? ? ? ? ? ? ? ? delay:(NSTimeInterval)delay? ? usingSpringWithDamping:(CGFloat)dampingRatio? ? ? initialSpringVelocity:(CGFloat)velocity? ? ? ? ? ? ? ? ? ? options:(UIViewAnimationOptions)options? ? ? ? ? ? ? ? animations:(void(^)(void))animations? ? ? ? ? ? ? ? completion:(void(^)(BOOLfinished))completion//彈簧效果動畫

(2) 在原來的基礎上又增加了兩個參數,實現了像彈簧一樣的動畫;dampingRatio 這個參數是設置彈簧的阻尼比例;velocity 這個是設置彈簧的最初速度。

10、+ performWithoutAnimation:

(1) 方法原型

+ (void)performWithoutAnimation:(void(^)(void))actionsWithoutAnimation//禁用一個視圖動畫過渡。

(2) 視圖轉換時不執行動畫,參數為block對象,來執行你要執行的代碼。

二十一、UIView視圖動畫

1、+ beginAnimations:context:

[UIViewbeginAnimations:@"animaton"context:nil];//動畫開始

2、+ commitAnimations

[UIView commitAnimations];//執行動畫

3、+ setAnimationStartDate:

[UIViewsetAnimationStartDate:[NSDatedate]];//設置動畫開始的時間

4、+ setAnimationsEnabled:

[UIViewsetAnimationsEnabled:YES];//設置是否開啟動畫效果

5、+ setAnimationDelegate:

[UIViewsetAnimationDelegate:self];//設置動畫代理對象,當動畫開始或者結束時會發消息給代理對象

6、+ setAnimationWillStartSelector:

[UIViewsetAnimationDelegate:self];//設置動畫代理對象,當動畫開始或者結束時會發消息給代理對象- (void)animationWillStart{NSLog(@"動畫將要開始");}

7、+ setAnimationDidStopSelector:

[UIViewsetAnimationDidStopSelector:@selector(animationDidStop)];//動畫已經結束- (void)animationDidStop{NSLog(@"動畫已經結束");}

8、+ setAnimationDuration:

[UIView setAnimationDuration:1.0f]; //動畫的持續時間,秒為單位

9、+ setAnimationDelay:

[UIView setAnimationDelay:2.0f]; //動畫延遲執行

10、+ setAnimationCurve:

[UIView setAnimationCurve:UIViewAnimationCurveLinear]; //設置動畫顯示的方式

11、+ setAnimationRepeatCount:

[UIView setAnimationRepeatCount:1]; //動畫重復的次數

12、+ setAnimationRepeatAutoreverses:

[UIViewsetAnimationRepeatAutoreverses:YES];//設置動畫是否從頭開始

13、+ setAnimationBeginsFromCurrentState:

[UIViewsetAnimationBeginsFromCurrentState:NO];//設置動畫是否從當前狀態開始

14、+ setAnimationTransition:forView:cache:

[UIViewsetAnimationTransition:UIViewAnimationTransitionFlipFromLeft? ? ? forView:self.viewcache:NO];//設置動畫的過度效果

15、+ areAnimationsEnabled

[UIView areAnimationsEnabled]; //動畫結束返回YES

二十二、Using Motion Effects

1、- addMotionEffect:

2、motionEffects

3、- removeMotionEffect:

二十三、Preserving and Restoring State

1、restorationIdentifier

2、- encodeRestorableStateWithCoder:

3、- decodeRestorableStateWithCoder:

二十四、Capturing a View Snapshot

1、- snapshotViewAfterScreenUpdates:

2、- resizableSnapshotViewFromRect:afterScreenUpdates:withCapInsets:

3、- drawViewHierarchyInRect:afterScreenUpdates:

二十五、Identifying the View at Runtime

1、tag

2、- viewWithTag:

二十六、Converting Between View Coordinate Systems

1、- convertPoint:toView:

2、- convertPoint:fromView:

3、- convertRect:toView:

4、- convertRect:fromView:

二十七、Hit Testing in a View

1、- hitTest:withEvent:

2、- pointInside:withEvent:

二十八、Ending a View Editing Session

1、- endEditing:

二十九、Observing View-Related Changes

1、- didAddSubview:

2、- willRemoveSubview:

3、- willMoveToSuperview:

4、- didMoveToSuperview

5、- willMoveToWindow:

6、- didMoveToWindow

三十、Observing Focus

1、- canBecomeFocused

2、+ inheritedAnimationDuration

3、focused

三十一、Data Types

1、UIViewAnimationOptions

2、UIViewAnimationCurve

3、UIViewContentMode

4、UIViewTintAdjustmentMode

5、UISystemAnimation

6、UIViewAutoresizing

7、UIViewAnimationTransition

8、Constants

9、UIViewKeyframeAnimationOptions

10、UILayoutConstraintAxis

三十二、Fitting Size

1、UIView Intrinsic Metric Constant

2、UISemanticContentAttribute

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 在iOS中隨處都可以看到絢麗的動畫效果,實現這些動畫的過程并不復雜,今天將帶大家一窺ios動畫全貌。在這里你可以看...
    每天刷兩次牙閱讀 8,566評論 6 30
  • 在iOS中隨處都可以看到絢麗的動畫效果,實現這些動畫的過程并不復雜,今天將帶大家一窺iOS動畫全貌。在這里你可以看...
    F麥子閱讀 5,141評論 5 13
  • 動畫的繼承結構 CAAnimation{CAPropertyAnimation{CABasicAnimation{...
    早起的蟲兒子被鳥吃閱讀 907評論 0 1
  • 在iOS實際開發中常用的動畫無非是以下四種:UIView動畫,核心動畫,幀動畫,自定義轉場動畫。 1.UIView...
    請叫我周小帥閱讀 3,155評論 1 23
  • 中間的提醒內容 指示器、HUD、遮蓋、蒙板 半透明的指示器如何實現? 指示器的alpha = 1.0 指示器的背景...
    iOS_Cqlee閱讀 476評論 0 4