#import "ViewController.h"
#define ImageCount 5
@interface ViewController ()
@property (strong, nonatomic) UIImageView *imageView;
@property (assign, nonatomic) NSInteger currentIndex;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.imageView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
//保持图片原比例
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
self.imageView.image = [UIImage imageNamed:@"1"];
[self.view addSubview:self.imageView];
//添加左轻扫手势
//UISwipeGestureRecognizer: 手势类型默认只能向右滑动, 触发方法
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:)];
[self.view addGestureRecognizer:right];
}
//设置左滑动手势
- (void)leftSwipe:(UISwipeGestureRecognizer *)gesture{
//结合手势方向, 指定是否是下一张(当向左滑动是, 需要展示下一张)参数为:YES
[self transitionAnimation:YES];
}
//设置右滑动手势
- (void)RightSwipe:(UISwipeGestureRecognizer *)gesture{
//当向右滑动时, 需要展示上一张, 所以参数为:NO
[self transitionAnimation:NO];
}
//手势方法中, 判断是上一张, 还是下一张, 传递参数
- (void)transitionAnimation:(BOOL)isNext{
//1. 创建转场动画
CATransition *transition = [[CATransition alloc] init];
//2. 效果
transition.type = @"cube";
//3. 向左滑动
if (isNext) {
//设置动画的过度方向, 从右开始向左翻转
transition.subtype = kCATransitionFromRight;
}
//向右滑动
else{
////设置动画的过度方向, 从左开始向右翻转
transition.subtype = kCATransitionFromLeft;
}
//4. 设置动画持续时间
transition.duration = 1.0;
//5. 调用自定义方法, 获取图片
self.imageView.image = [self getImage:isNext];
//6. 添加到layer层上
[self.imageView.layer addAnimation:transition forKey:@"transition"];
}
- (UIImage *)getImage:(BOOL)isNext{
//后一张
if (isNext) {
//例如: 当currentIndex = 1时, (1+1)%5 = 2;
self.currentIndex = (self.currentIndex + 1) % ImageCount;
}
//前一张
else{
//例如: 当currentIndex = 1是
self.currentIndex = (self.currentIndex - 1 + ImageCount) % ImageCount;
}
//往数组中添加图片
NSString *imagName = [NSString stringWithFormat:@"%ld", self.currentIndex + 1];
NSLog(@"%ld",self.currentIndex);
return [UIImage imageNamed:imagName];
}
@end```
###转场效果type类型:
![Type.png](http://upload-images.jianshu.io/upload_images/1803308-00b6e1804512b4ea.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
CATransition_图片浏览效果
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 转场动画 转场动画就是从一个场景以动画的形式过渡到另一个场景。自定义转场动画的意义是脱离系统固定的转场,实现UI交...
- 先看看CAAnimation动画的继承结构 CAAnimation{ CAPropertyAnimation { ...