天天看點

IOS學習動畫一之 UIKit動畫

IOS的 普通動畫可使用UIKit提供的動畫方式;

           複雜動畫可使用Core Animation。

1、 通過動畫上下文使用UIKit動畫

-(void)animationOfUIKit  {  
    UIView *redView=[[UIView alloc]initWithFrame:CGRectMake(10, 10, 100, 100)];  
      
    [self.view addSubview:redView];  
    //開始動畫  
    [UIView beginAnimations:@"test" context:nil];  
    //動畫時長  
    [UIView setAnimationDuration:1];  
    //設定動畫淡入淡出  
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 
    /*  
     *要進行動畫設定的地方  
     */  
    redView.backgroundColor=[UIColor blueColor];  
    redView.frame=CGRectMake(50, 50, 200, 200);  
    redView.alpha=0.5;
    //動畫結束  
    [UIView commitAnimations];  
}      

2、通過代碼塊使用UIKit動畫

-(void)animationOfBlock  {  
    //初始化一個View,用來顯示動畫  
    UIView *redView=[[UIView alloc]initWithFrame:CGRectMake(10, 10, 100, 100)];
    [self.view addSubview:redView];  
  
    [UIView animateWithDuration:1 //時長  
                 delay:0 //延遲時間  
                 options:UIViewAnimationOptionTransitionFlipFromLeft//動畫效果  
                 animations:^{
                         //動畫設定區域  
                         redView.backgroundColor=[UIColor blueColor];  
                         redView.frame=CGRectMake(50, 50, 200, 200);  
                         redView.alpha=0.5;  
                     } completion:^(BOOL finish){  
                       //動畫結束時調用  
                     }];
}      

3、UIView的,翻轉、旋轉,偏移,翻頁,縮放,取反的動畫效果

CoreGraphics架構中的CGAffineTransform類可用于設定UIView的transform屬性,控制視圖的縮放、旋轉和平移等操作 (另稱為 放射變換矩陣),這種動畫都是針對視圖的原定最初位置的中心點為起始參照進行相應操作的,在操作結束之後可對設定量進行還原:

view.transform=CGAffineTransformIdentity;

翻轉的動畫

    //開始動畫  
   [UIView beginAnimations:@"doflip" context:nil];  
   //設定時常  
   [UIView setAnimationDuration:1];  
   //設定動畫淡入淡出  
   [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];  
   //設定代理  
   [UIView setAnimationDelegate:self];  
   //設定翻轉方向  
   [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:manImageView cache:YES];  
   //動畫結束  
   [UIView commitAnimations];      

旋轉動畫

//建立一個CGAffineTransform  transform對象  
CGAffineTransform  transform;   
//設定旋轉度數  
transform = CGAffineTransformRotate(manImageView.transform,M_PI/6.0);  
//動畫開始  
[UIView beginAnimations:@"rotate" context:nil ];  
//動畫時常  
[UIView setAnimationDuration:2];  
//添加代理  
[UIView setAnimationDelegate:self];  
//擷取transform的值  
[manImageView setTransform:transform];  
//關閉動畫  
[UIView commitAnimations];      

偏移動畫

[UIView beginAnimations:@"move" context:nil];
[UIView setAnimationDuration:2];
[UIView setAnimationDelegate:self];
//改變它的frame的x,y的值
manImageView.frame=CGRectMake(100,100, 120,100);
[UIView commitAnimations];      

翻頁動畫

[UIView beginAnimations:@"curlUp" context:nil];  
//指定動畫曲線類型,該枚舉是預設的,線性的是勻速的  
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
//設定動畫時常  
[UIView setAnimationDuration:1];  
[UIView setAnimationDelegate:self];  
//設定翻頁的方向  
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:manImageView cache:YES];  
//關閉動畫  
[UIView commitAnimations];      

縮放動畫

CGAffineTransform  transform = CGAffineTransformScale(manImageView.transform,1.2,1.2);  
[UIView beginAnimations:@"scale" context:nil];  
[UIView setAnimationDuration:2];  
[UIView setAnimationDelegate:self];  
[manImageView setTransform:transform];  
[UIView commitAnimations];      
CGAffineTransform transform = CGAffineTransformInvert(manImageView.transform);  
[UIView beginAnimations:@"Invert" context:nil];  
[UIView setAnimationDuration:2];//動畫時常  
[UIView setAnimationDelegate:self];  
[manImageView setTransform:transform];//擷取改變後的view的transform  
[UIView commitAnimations];//關閉動畫