IOS視圖控制器自定義動畫效果

(1)這裏的動畫效果指的是界面切換的動畫效果,我們常見的又淡入淡出,右出左進等等,當然還有一些高級動畫,這種動畫適合遊戲類的,對於一般APP會顯得太花哨。

(2)我們在此處沒有增加任何框架(QuartzCore)也沒有導入什麼頭文件(QuartzCore.h),就可以直接用CATransiton(相當於是CAAnimation的子類)來創建一個對象,如animation1。

(3)創建完之後我們就對這個動畫對象進行動畫設置,這裏面主要是涉及到type屬性,而且值有兩種:一種是調用系統自帶的一些效果,kCATransition開頭的,如animation1.type=kCATransitionPush、kCATransitionMoveIn、kCATransitionFade等;另一種是傳入名稱的方法,這個名稱也是內置的吧,在某一個庫裏吧(難道就是在QuartzCore.h裏?),不管那麼多,反正是可以直接用@“ ”來賦值即可,比如animation.type=@"cube"、@“pageCurl”、@“rippleEffect”等。

(4)當然,最重要的時我們這些動畫效果都是在導航控制器視圖的layer層上的,所以要顯示這個動畫,得把它加載進去,即直接賦值即可,如[self.navigationController.view.layer addAnimation:animation1 forKey:nil];

(5)還有個次重要的,就是得關閉我們系統默認的動畫,[self.navigationController popViewControllerAnimated:NO];也就是設置爲NO即可,防止出現怪異的動畫疊加。

我們依然是在導航控制器中說的這個事兒,所以當然我們需要創建2個視圖控制器放到導航控制器中,用於界面跳轉(切換)時用。

這個是根試圖控制器ViewController.m文件,核心部分比較簡單,三個步驟。

#import "ViewController.h"
#import "SecondViewController.h"
@interface ViewController ()

@end
@implementation ViewController

- (void)viewDidLoad {  
     UIButton *btn1=[UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     btn1.backgroundColor=[UIColor whiteColor];  
     btn1.frame=CGRectMake(38, 80, 300, 30);  
     [btn1 setTitle:@"JUMP TO SECOND" 
     forState:UIControlStateNormal];  
     [btn1 addTarget:self action:@selector(jumpTo) forControlEvents:UIControlEventTouchUpInside];  [self.view addSubview:btn1]; 
     [super viewDidLoad];  // Do any additional setup after loading the view, typically from a nib.
  }

-(void)jumpTo{  
    SecondViewController *second1=[[SecondViewController alloc]init];  
    //簡單三步(1)實例化一個轉化動畫對象animation2(2)設置一個動畫pageCurl(3)把這個動畫對象加載到導航控制器視圖的layer層上  
    CATransition *animation2=[CATransition animation];  
    animation2.type=@"oglFlip";  
    [self.navigationController.view.layer addAnimation:animation2 forKey:nil];  //當然,我們需要關閉默認的動畫,雖然有些動畫不受影響,但有的會和默認動畫疊加
    [self.navigationController pushViewController:second1 animated:NO];
}@end

下面這個是子頁面的SecondViewController.m的文件:

#import "SecondViewController.h"@interface SecondViewController ()@end@implementation SecondViewController

- (void)viewDidLoad {  //  
    UIBarButtonItem *barBtn1=[[UIBarButtonItem alloc]initWithTitle:@"回家" style:UIBarButtonItemStylePlain target:self action:@selector(backTo)];  
    self.navigationItem.leftBarButtonItem=barBtn1;  
    self.view.backgroundColor=[UIColor purpleColor];  
    [super viewDidLoad];  // Do any additional setup after loading the view.}

-(void)backTo{  //默認的動畫效果是:從右向左出來,從左向右回去    //不論哪種方法,先創建一個切換對象即CATransition對象 
     CATransition *animation1=[CATransition animation];  
     //然後設置切換的時間,即動畫的時間                                           
     animation1.duration=1;  
     //下面是重頭戲,有好幾種方法設置動畫的類型 
      //第一種是:用默認的type和subtype來設置系統自帶的,有7、8種簡單基本的,但貌似subtype依然失效了,總之,用type來設置系統自帶的8種足夠  
      //從上下左右出現,以及Fade、MoveIn、Push、Reveal等8種簡單的  
      animation1.type=kCATransition;  animation1.subtype=kCATransitionPush;    
      //第二種方式,直接用名字來定義,難道這個需要QuartzCore框架支持麼?xcode6貌似已經配置了,不需要導入任何頭文件,而且這個效果會覆蓋上面第一種用到得8中默認簡單動畫  
      //如cube、rippleEffect、suckEffect、oglFlip、pageCurl、pageUnCurl  animation1.type=@"oglFlip";    //設置完動畫後,就是設置動畫的時機,也就是顯示和退出效果比如淡入淡出等等  
      //貌似沒有什麼效果,而且還是可以省略不寫的  [animation1 setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];  
      //最重要的一步,是把這個動畫加載到導航控制器的view的layer層  
      [self.navigationController.view.layer addAnimation:animation1 forKey:nil];  //把默認的動畫關閉,雖然有些動畫不受影響,但有的會和默認動畫疊加  
      [self.navigationController popViewControllerAnimated:NO];
}

- (void)didReceiveMemoryWarning {  
    [super didReceiveMemoryWarning];  
    // Dispose of any resources that can be recreated.
    }@end
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章