天天看點

IOS中動畫的實作:以及視圖的移動、縮放和旋轉

一、動畫和移動視圖

IOS上實作動畫的方式有多種,我們可以獲得的最高層次的動畫能力是通過UIKit,UIKit中包括一些Core Animation的較低層次的功能,并且包裝成非常簡潔的API供我們使用。

     UIKit中實作動畫的起點是調用UIView類中的類方法beginAnimations:context:。第一個參數是一個可選動畫的名稱,第二個參數是一個可選的上下文,在之後傳遞給動畫的委托方法時你可以擷取它。在你通過beginAnimations:context:開始一個動畫後,它實際上不會馬上執行而是知道你調用UIView類的commintAnimations類方法。在begainAnimations:context:和commitAnimations之間,你對一個試圖對象的計算(如移動)将在commitAnimations被調用之後開始進行動畫。

UIKit執行動畫時的重要方法,UIView類的類方法:

beginAnimationss:context: 開始一個動畫塊。調用這個類方法之後,你對試圖任何動畫屬性的改變,都會在動畫塊送出之後形成動畫。

setAnimationDuration:以秒為機關設定動畫的持續時間。

setAnimationDelegate:設定一個對象來接受動畫開始之前、動畫期間或動畫結束之後發生的各種事情的委托對象。設定一個委托對象不會立即開始發送動畫委托消息。你必須對視圖對象使用其他的setter方法來告訴UIKit你的委托對象中的那個選擇器(selector)要接收哪些委托消息。

setAnimationDidstopSelector:設定動畫完成時委托對象中應該被調用的方法。

setAnimationWillStartSelector:設定動畫即将開始時委托對象中被調用選擇器。

setAnimationDelay:設定一個動畫啟動的延遲時間。以秒為機關

setAnimationRepeatCount:設定一個動畫塊需要重複它的動畫的次數

- (void) startTopLeftImageViewAnimation{ 

    [self.xcodeImageView1 setFrame:CGRectMake(0.0f, 

    [self.xcodeImageView1 setAlpha:1.0f]; 

    [UIView beginAnimations:@"xcodeImageView1Animation" 

        context:xcodeImageView1]; 

    [UIView setAnimationDuration:3.0f]; 

    [UIView setAnimationDelegate:self]; 

    [UIView setAnimationDidStopSelector: 

        @selector(imageViewDidStop:finished:context:)]; 

     [self.xcodeImageView1 setFrame:CGRectMake(220.0f, 

     [self.xcodeImageView1 setAlpha:0.0f]; [UIView commitAnimations];

- (void)imageViewDidStop:(NSString *) 

    paramAnimationID finished:(NSNumber *)paramFinished 

    context:(void *)paramContext{ 

    UIImageView *contextImageView = (UIImageView *)paramContext; 

    [contextImageView removeFromSuperview]; 

動畫和縮放視圖:

- (void) viewDidAppear:(BOOL)paramAnimated{ 

    [super viewDidAppear:paramAnimated]; 

    self.xcodeImageView.center = self.view.center; 

self.xcodeImageView.transform = CGAffineTransformIdentity; 

    [UIView beginAnimations:nil context:NULL]; 

    [UIView setAnimationDuration:5.0f]; 

    self.xcodeImageView.transform = 

    CGAffineTransformMakeScale(2.0f, 2.0f); 

    [UIView commitAnimations]; 

動畫和旋轉