標籤:
#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轉場的類型比較少.
ios開發核心動畫五:轉場動畫