今天我们来学习一下新的动画类型, 转场动画,
什么是转场动画呢?
转场动画就是从一个场景以动画的形式过渡到下一个场景,
其实专场动画还是比较简单的,
主要步骤有:
创建转场动画
设置转场类型
设置子转场类型(可选)
-
设置转场后的新视图并添加到图层,
在网上看到一个利用转场做轮播图的效果感觉挺好,
@implementation ViewController{
UIImageView * _james;
int _currentIndex;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor clearColor];
_james = [[UIImageView alloc] initWithFrame: [UIScreen mainScreen].bounds];
_james.image = [UIImage imageNamed:@"1.jpeg"];
[self.view addSubview:_james];UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftSwipe:)]; [self.view addGestureRecognizer:leftSwipe]; leftSwipe.direction=UISwipeGestureRecognizerDirectionLeft; UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightSwipe:)]; rightSwipe.direction = UISwipeGestureRecognizerDirectionRight; [self.view addGestureRecognizer:rightSwipe]; } - (void)leftSwipe:(UIGestureRecognizer *)gesture { NSLog(@"left"); [self transitionAnimation:YES]; } - (void)rightSwipe:(UIGestureRecognizer *)gesture {NSLog(@"right"); [self transitionAnimation:NO]; } - (void)transitionAnimation:(BOOL)isNext{ // 创建转场动画 CATransition *trans = [[CATransition alloc] init]; trans.type = @"cube"; if (isNext) { trans.subtype = kCATransitionFromRight; }else{ trans.subtype = kCATransitionFromLeft; } // 设置时间 trans.duration = 1.0; _james.image = [self getImage:isNext]; // 把转场动画添加到 layer 上 [_james.layer addAnimation:trans forKey:@"transition"]; } - (UIImage *)getImage:(BOOL)isNext{ if (isNext) { _currentIndex = (_currentIndex + 1) % 5; }else{ _currentIndex = (_currentIndex - 1 + 5) % 5; } UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpeg",_currentIndex]]; return image; }
效果图为:今天我们来学习一下新的动画类型, 转场动画,
什么是转场动画呢?
转场动画就是从一个场景以动画的形式过渡到下一个场景,
其实专场动画还是比较简单的,
主要步骤有:
创建转场动画
设置转场类型
设置子转场类型(可选)
-
设置转场后的新视图并添加到图层,
在网上看到一个利用转场做轮播图的效果感觉挺好,
@implementation ViewController{
UIImageView * _james;
int _currentIndex;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor clearColor];
_james = [[UIImageView alloc] initWithFrame: [UIScreen mainScreen].bounds];
_james.image = [UIImage imageNamed:@"1.jpeg"];
[self.view addSubview:_james];UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftSwipe:)]; [self.view addGestureRecognizer:leftSwipe]; leftSwipe.direction=UISwipeGestureRecognizerDirectionLeft; UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightSwipe:)]; rightSwipe.direction = UISwipeGestureRecognizerDirectionRight; [self.view addGestureRecognizer:rightSwipe]; } - (void)leftSwipe:(UIGestureRecognizer *)gesture { NSLog(@"left"); [self transitionAnimation:YES]; } - (void)rightSwipe:(UIGestureRecognizer *)gesture {NSLog(@"right"); [self transitionAnimation:NO]; } - (void)transitionAnimation:(BOOL)isNext{ // 创建转场动画 CATransition *trans = [[CATransition alloc] init]; trans.type = @"cube"; if (isNext) { trans.subtype = kCATransitionFromRight; }else{ trans.subtype = kCATransitionFromLeft; } // 设置时间 trans.duration = 1.0; _james.image = [self getImage:isNext]; // 把转场动画添加到 layer 上 [_james.layer addAnimation:trans forKey:@"transition"]; } - (UIImage *)getImage:(BOOL)isNext{ if (isNext) { _currentIndex = (_currentIndex + 1) % 5; }else{ _currentIndex = (_currentIndex - 1 + 5) % 5; } UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpeg",_currentIndex]]; return image; }
效果图为: