ios開發核心動畫五:轉場動畫


 

 

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageV;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    
    
  
 
    
    
}

static int _i = 1;
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    

    
    //轉場代碼與轉場動畫必須得在同一個方法當中.
    
    //轉場代碼
//    _i++;
//    if (_i == 4) {
//        _i = 1;
//    }
//    
//    NSString *imageName = [NSString stringWithFormat:@"%d",_i];
//    self.imageV.image = [UIImage imageNamed:imageName];
//
//    
//    //添加轉場動畫
//    CATransition *anim = [CATransition animation];
//    anim.duration  = 1;
//    //設置轉場的類型
//    anim.type = @"pageCurl";
//    
//    //設置動畫的起始位置
//    anim.startProgress = 0.3;
//    //設置動畫的結束位置
//    anim.endProgress = 0.5;
//    
//    
//    
//    [self.imageV.layer addAnimation:anim forKey:nil];
    
    
    
//    
    
    [UIView transitionWithView:self.imageV duration:0.5 options:UIViewAnimationOptionTransitionCurlUp animations:^{
        
        //轉場 代碼
        _i++;
        if (_i == 4) {
            _i = 1;
        }
        
        NSString *imageName = [NSString stringWithFormat:@"%d",_i];
        self.imageV.image = [UIImage imageNamed:imageName];
    } completion:^(BOOL finished) {
        
    }];

    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

 

1.什么是轉場動畫?

就是從一個場景轉換到另一個場景,像導航控制器的push效果,就是一個轉場.

 

2.如何創建轉場動畫

  創建轉場動畫

      CATransition *anim = [CATransition animation];

      設置轉場類型

      anim.type = @"cube";

      anim.duration = 1;

      設置轉場的方向

      anim.subtype = kCATransitionFromLeft;

  設置動畫的開始位置

      anim.startProgress = 0.5;

  設置動畫的結束位置

      anim.endProgress  =0.8;

      添加動畫.了

     [_imageV.layer addAnimation:anim forKey:nil];

 

 

要執行動畫的代碼稱為轉場代碼.

轉場動畫要和轉場代碼寫在同一個方法當中才有動畫效果.

 

3.UIView進行轉場動畫

 

  [UIView transitionWithView:self.imageV duration:1 

    options:UIViewAnimationOptionTransitionFlipFromRight 

    animations:^{

        轉場代碼

        } completion:^(BOOL finished) {

        動畫執行完畢時調用.

    }];

   

    使用UIView轉場的類型比較少.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM