1. 程式人生 > >iOS UIView非常用方法及屬性詳解

iOS UIView非常用方法及屬性詳解

建立一個帶有檢視的視窗 - (void) applicationDidFinishLaunching:(UIApplication *)application { // Create the window object and assign it to the // window instance variable of the application delegate. window = [[UIWindow alloc] initWithFrame:[[ UIScreen mainScreen]  bounds]]; window. backgroundColor = [UIColor whiteColor]; // Create a simple red square CGRect redFrame = CGRectMake(10, 10, 100, 100); UIView *redView = [[UIView alloc] initWithFrame:redFrame]; redView.backgroundColor = [UIColor redColor]; // Create a simple blue square CGRect blueFrame = CGRectMake(10, 150, 100, 100); UIView *blueView = [[UIView alloc] initWithFrame:blueFrame]; blueView.backgroundColor = [UIColor blueColor]; // Add the square views to the window [window addSubview:redView]; [window addSubview:blueView]; // Once added to the window, release the views to avoid the // extra retain count on each of them. [redView release]; [blueView release]; // Show the window. [window makeKeyAndVisible]; } 當您為某個檢視新增子檢視時,UIKit 會向相應的父子檢視傳送幾個訊息,通知它們當前發生的狀態變化。您可以在自己的定製檢視中對諸如 willMoveToSuperview
: 、 willMoveToWindow: 、 willRemoveSubview: 、 didAddSubview: 、 didMoveToSuperview 、和 didMoveToWindow這樣的方法進行過載,以便在事件發生的前後進行必要的處理,並根據發生的變化更新檢視的狀態資訊。 在檢視層次建立之後,您可以通過檢視的 superview 屬性來取得其父檢視,或者通過 subviews屬性取得檢視的子檢視。您也可以通過 isDescendantOfView:方法來判定一個檢視是否在其父檢視的檢視層中。一個檢視層次的根檢視沒有父檢視,因此其superview 屬性被設定為nil。對於當前被顯示在螢幕上的檢視,視窗物件通常是整個檢視層次的根檢視。  UIView 類定義了下面這些方法,用於在不同的檢視本地座標系統之間進行座標轉換: convertPoint:fromView: convertRect:fromView: convertPoint:toView: convertRect:toView: UIWindow 的版本則使用視窗座標系統。 convertPoint:fromWindow: convertRect:fromWindow: convertPoint:toWindow: convertRect:toWindow: UIView 類中包含一個 tag
 屬性。藉助這個屬性,您可以通過一個整數值來標識一個檢視物件。您可以通過這個屬性來唯一標識檢視層次中的檢視,以及在執行時進行檢視的檢索(基於tag 標識的檢索比您自行遍歷檢視層次要快)。tag 屬性的預設值為0。您可以通過UIView 的 viewWithTag:方法來檢索標識過的檢視 動畫塊從呼叫UIView 的 beginAnimations:context:類方法開始,而以呼叫 commitAnimations類 方法作為結束。在這兩個呼叫之間,您可以配置動畫的引數和改變希望實行動畫的屬性值。一旦呼叫commitAnimations 方法,UIKit 就會開始執行動畫,即把給定屬性從當前值到新值的變化過程用動畫表現出來。動畫塊可以被巢狀,但是在最外層的動畫塊提交之前,被巢狀的動畫不會被執行。 frame 檢視的邊框矩形,位於父檢視的座標系中。 bounds 檢視的邊界矩形,位於檢視的座標系中。 center 邊框的中心,位於父檢視的座標系中。 transform 檢視上的轉換矩陣,相對於檢視邊界的中心。 alpha 檢視的alpha 值,用於確定檢視的透明度。 用 setAnimationStartDate
:方法來設定動畫在commitAnimations 方法返回之後的發生日期。預設行為是使動畫立即在動畫執行緒中執行。 用 setAnimationDelay:方法來設定實際發生動畫和commitAnimations 方法返回的時間點之間的間隔。 用 setAnimationDuration:方法來設定動畫持續的秒數。 用 setAnimationCurve:方法來設定動畫過程的相對速度,比如動畫可能在啟示階段逐漸加速,而在結束階段逐漸減速,或者整個過程都保持相同的速度。 用 setAnimationRepeatCount:方法來設定動畫的重複次數。 用 setAnimationRepeatAutoreverses:方法來指定動畫在到達目標值時是否自動反向播放。您可以結合使用這個方法和 setAnimationRepeatCount:方法,使各個屬性在初始值和目標值之間平滑切換一段時間。 您可以通過UIView 的 setAnimationDelegate: 類方法來設定委託, 並通過 setAnimationWillStartSelector: 和 setAnimationDidStopSelector:方法來指定接收訊息的選擇器方法。訊息處理方法的形式如下: - (void)animationWillStart:(NSString *)animationID context:(void *)context; - (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context; 上面兩個方法的animationID 和context 引數和動畫塊開始時傳 beginAnimations:context:方法的引數相同: animationID - 應用程式提供的字串,用於標識一個動畫塊中的動畫。 context - 也是應用程式提供的物件,用於向委託物件傳遞額外的資訊。 setAnimationDidStopSelector:選擇器方法還有一個引數—即一個布林值。如果動畫順利完成,沒有被其它動畫取消或停止,則該值為YES。