#import "ViewController.h"
#define IMAGE_COUNT 5
@interface ViewController ()
/* 图片 */
@property (strong,nonatomic) UIImageView *imageView;
/* 当前下标*/
@property (assign,nonatomic) NSInteger currentIndex;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.imageView = [[UIImageView alloc] init];
self.imageView.frame = [UIScreen mainScreen].bounds;
//这个图片会在View中显示,并且比例不变
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
self.imageView.image = [UIImage imageNamed:@"0.jpg"];
[self.view addSubview:self.imageView];
//添加手势
UISwipeGestureRecognizer *left = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftSwipe:)];
left.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:left];
UISwipeGestureRecognizer *right = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightSwipe:)];
right.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:right];
}
- (void)leftSwipe:(UISwipeGestureRecognizer *)gestur{
[self transitionAnimation:YES];
}
- (void)rightSwipe:(UISwipeGestureRecognizer *)gestur{
[self transitionAnimation:NO];
}
- (void)transitionAnimation:(BOOL)isNext{
//1.创建转场动画
CATransition *tran = [[CATransition alloc] init];
//效果
tran.type = @"cube";
if (isNext) {
tran.subtype = kCATransitionFromRight;
}else{
tran.subtype = kCATransitionFromLeft;
}
tran.duration = 1.0f;
self.imageView.image = [self getImage:isNext];
[self.imageView.layer addAnimation:tran forKey:@"Transition"];
}
- (UIImage *)getImage:(BOOL)isNext{
//后一张
if (isNext) {
self.currentIndex = (self.currentIndex + 1) % IMAGE_COUNT;
}else{
self.currentIndex = (self.currentIndex - 1 + IMAGE_COUNT) % IMAGE_COUNT;
}
//往数组中添加图片,图片名与下标名对应
NSString *imageName = [NSString stringWithFormat:@"%ld.jpg",self.currentIndex];
return [UIImage imageNamed:imageName];
}
@end
进阶 12-5 转场动画
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- UITabBarController自定义转场动画 实现UITabBarControllerDelegate 实现...
- 学习收集,非原创 一、 简介 在Android开发过程中,经常会碰到Activity之间的切换效果的问题,下面介绍...