今天我們來學習一下新的動畫類型, 轉場動畫,
什么是轉場動畫呢?
轉場動畫就是從一個場景以動畫的形式過渡到下一個場景,
其實專場動畫還是比較簡單的,
主要步驟有:
創(chuàng)建轉場動畫
設置轉場類型
設置子轉場類型(可選)
-
設置轉場后的新視圖并添加到圖層,
在網(wǎng)上看到一個利用轉場做輪播圖的效果感覺挺好,
@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{ // 創(chuàng)建轉場動畫 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; }
效果圖為:今天我們來學習一下新的動畫類型, 轉場動畫,
什么是轉場動畫呢?
轉場動畫就是從一個場景以動畫的形式過渡到下一個場景,
其實專場動畫還是比較簡單的,
主要步驟有:
創(chuàng)建轉場動畫
設置轉場類型
設置子轉場類型(可選)
-
設置轉場后的新視圖并添加到圖層,
在網(wǎng)上看到一個利用轉場做輪播圖的效果感覺挺好,
@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{ // 創(chuàng)建轉場動畫 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; }
效果圖為: