iOS動畫

原文地址:iOS動畫作者:酷雲
最普通動畫: 
//開始動畫 
[UIView beginAnimations:nil context:nil];  
//設定動畫持續時間 
[UIView setAnimationDuration:2]; 
//動畫的內容 
frame.origin.x += 150; 
[img setFrame:frame]; 
//動畫結束 
[UIView commitAnimations]; 

連續動畫:一個接一個地顯示一系列的圖像 
NSArray *myImages = [NSArray arrayWithObjects: 
[UIImage imageNamed:@"myImage1.png"], 
[UIImage imageNamed:@"myImage2.png"], 
[UIImage imageNamed:@"myImage3.png"], 
[UIImage imageNamed:@"myImage4.gif"], nil]; 

UIImageView *myAnimatedView = [UIImageView alloc]; 
[myAnimatedView initWithFrame:[self bounds]]; 
myAnimatedView.animationImages = myImages; //animationImages屬性返回一個存放動畫圖片的數組 
myAnimatedView.animationDuration = 0.25; //瀏覽整個圖片一次所用的時間 
myAnimatedView.animationRepeatCount = 0; // 0 = loops forever 動畫重複次數 
[myAnimatedView startAnimating]; 
[self addSubview:myAnimatedView]; 
[myAnimatedView release]; 

CATransition Public API動畫: 
CATransition *animation = [CATransition animation]; 
//動畫時間 
    animation.duration = 0.5f; 
//先慢後快 
    animation.timingFunction = UIViewAnimationCurveEaseInOut; 
animation.fillMode = kCAFillModeForwards; 
//animation.removedOnCompletion = NO; 

//各種動畫效果 
 
 
//各種組合 
animation.type = kCATransitionPush; 
animation.subtype = kCATransitionFromRight; 

[self.view.layer addAnimation:animation forKey:@"animation"]; 

CATransition Private API動畫: 
animation.type可以設定爲以下效果 
動畫效果彙總: 
 

UIView Animations 動畫: 
[UIView beginAnimations:@"animationID" context:nil]; 
[UIView setAnimationDuration:0.5f]; 
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 
[UIView setAnimationRepeatAutoreverses:NO]; 
//以下四種效果 
 

[self.view exchangeSubviewAtIndex:1 withSubviewAtIndex:0]; 
[UIView commitAnimations]; 
IOS4.0新方法: 
方法: +(void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations; 
+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion; //多一個動畫結束後可以執行的操作. 
//下邊是嵌套使用,先變大再消失的動畫效果. 
[UIView animateWithDuration:1.25 animations:^{ 
CGAffineTransform newTransform = CGAffineTransformMakeScale(1.2, 1.2); 
[firstImageView setTransform:newTransform]; 
[secondImageView setTransform:newTransform];} 
completion:^(BOOL finished){ 
[UIView animateWithDuration:1.2 animations:^{ 
[firstImageView setAlpha:0]; 
[secondImageView setAlpha:0];} completion:^(BOOL finished){ 
[firstImageView removeFromSuperview]; 
[secondImageView removeFromSuperview]; }]; 
}]; 


cocoachina有一個例子UIViewDemo,傳送門:http://www.cocoachina.com/bbs/read.php?tid-11820.html
發佈了100 篇原創文章 · 獲贊 2 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章