UISwipeGestureRecognizer *leftSwipeGesture=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(leftSwipe:)];
leftSwipeGesture.direction=UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:leftSwipeGesture];
UISwipeGestureRecognizer *rightSwipeGesture=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(rightSwipe:)];
rightSwipeGesture.direction=UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:rightSwipeGesture];
UISwipeGestureRecognizer *topSwipeGesture=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(topSwipe:)];
topSwipeGesture.direction=UISwipeGestureRecognizerDirectionUp;
[self.view addGestureRecognizer:topSwipeGesture];
UISwipeGestureRecognizer *bottomSwipeGesture=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(bottomSwipe:)];
bottomSwipeGesture.direction=UISwipeGestureRecognizerDirectionDown;
[self.view addGestureRecognizer:bottomSwipeGesture];
-(void)leftSwipe:(UISwipeGestureRecognizer *)gesture{
[self transitionAnimation:1];
}
#pragma mark 向右滑动浏览上一张图片
-(void)rightSwipe:(UISwipeGestureRecognizer *)gesture{
[self transitionAnimation:2];
}
-(void)topSwipe:(UISwipeGestureRecognizer *)gesture{
[self transitionAnimation:3];
}
-(void)bottomSwipe:(UISwipeGestureRecognizer *)gesture{
[self transitionAnimation:4];
}
#pragma mark 转场动画
-(void)transitionAnimation:(NSInteger)direction{
//1.创建转场动画对象
CATransition *transition=[[CATransition alloc]init];
//2.设置动画类型,注意对于苹果官方没公开的动画类型只能使用字符串,并没有对应的常量定义
transition.type=@"rippleEffect";
BOOL bl = YES;
//设置子类型
if (direction == 1) {
transition.subtype=kCATransitionFromRight;
bl = YES;
}else if (direction == 2) {
transition.subtype=kCATransitionFromLeft;
bl = NO;
}else if (direction == 3) {
transition.subtype=kCATransitionFromTop;
bl = YES;
}else{
transition.subtype=kCATransitionFromBottom;
bl = NO;
}
//设置动画时常
transition.duration=1.0f;
//3.设置转场后的新视图添加转场动画
if (bl) {
currentIndex=(currentIndex+1)%IMAGE_COUNT;
}else{
currentIndex=(currentIndex-1+IMAGE_COUNT)%IMAGE_COUNT;
}
NSString *imageName=[NSString stringWithFormat:@"%d",(int)currentIndex];
_imageView.image = [UIImage imageNamed:imageName];
[_imageView.layer addAnimation:transition forKey:@"KCTransitionAnimation1"];
}