转场动画(长按屏幕有不同的翻转效果)

时间:2023-02-08 20:07:35

转场动画(长按屏幕有不同的翻转效果)

 

//

//  ViewController.m

//  UI-NO-39-2 转场动画 1

//

//  Created by 容伟 on 15/9/30.

//  Copyright (c) 2015年 容伟. All rights reserved.

//

 

/*

 CATransition 转场动画  可以切换视图、视图控制器

 type 转场动画的 动画效果

 subtype 转场动画  动画的方向

 

 kCATransitionFade   交叉淡化过渡

 kCATransitionMoveIn 新视图移到旧视图上面

 kCATransitionPush   新视图把旧视图推出去

 kCATransitionReveal 将旧视图移开,显示下面的新视图

 

 

 私有api 不建议使用 苹果不提供维护 并且有可能app审核不通过

 pageCurl            向上翻一页

 pageUnCurl          向下翻一页

 rippleEffect        滴水效果

 suckEffect          收缩效果 如一块布被抽走

 cube                立方体效果

 oglFlip             上下翻转效果

 */

/* 过渡效果

 fade     //交叉淡化过渡(不支持过渡方向) kCATransitionFade

 push     //新视图把旧视图推出去  kCATransitionPush

 moveIn   //新视图移到旧视图上面   kCATransitionMoveIn

 reveal   //将旧视图移开,显示下面的新视图  kCATransitionReveal

 cube     //立方体翻滚效果

 oglFlip  //上下左右翻转效果

 suckEffect   //收缩效果,如一块布被抽走(不支持过渡方向)

 rippleEffect //滴水效果(不支持过渡方向)

 pageCurl     //向上翻页效果

 pageUnCurl   //向下翻页效果

 cameraIrisHollowOpen  //相机镜头打开效果(不支持过渡方向)

 cameraIrisHollowClose //相机镜头关上效果(不支持过渡方向)

 */

 

/* 过渡方向

 kCATransitionFromRight

 kCATransitionFromLeft

 kCATransitionFromBottom

 */

#import "ViewController.h"

#import "NextViewController.h"

 

// 枚举类型

typedef enum Direction {

    Right = 0,

    Left,

}Direction;

 

@interface ViewController ()

{

    NSArray *imageList;

    UIImageView *showImage;

    int index;

}

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    imageList = @[@"9.jpg", @"11.jpg", @"12.jpg", @"13.jpg"];

    showImage = [[UIImageView alloc] initWithFrame:self.view.frame];

    showImage.image = [UIImage imageNamed:imageList[0]];

    showImage.userInteractionEnabled = YES;

    [self.view addSubview:showImage];

    

    UISwipeGestureRecognizer *rightSwipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(right:)];

    rightSwipeGesture.direction = UISwipeGestureRecognizerDirectionRight;

    [self.view addGestureRecognizer:rightSwipeGesture];

    

    UISwipeGestureRecognizer *leftSwipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(left:)];

    leftSwipeGesture.direction = UISwipeGestureRecognizerDirectionLeft;

    [self.view addGestureRecognizer:leftSwipeGesture];

 

    

//    添加长按手势

    UILongPressGestureRecognizer *next = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(next:)];

    [self.view addGestureRecognizer:next];

}

 

#pragma mark - 切换试图控制器

- (void)next:(UILongPressGestureRecognizer *)sender {

    if (sender.state == UIGestureRecognizerStateBegan) {

        NextViewController *next = [[NextViewController alloc] init];

    

        

        CATransition *animation = [CATransition animation];

        animation.type = @"cube";

        animation.subtype = kCATransitionFromLeft;

        animation.duration = 1;

        [self.navigationController.view.layer addAnimation:animation forKey:nil];

        

//        如果使用 自定义的转场动画  必须禁用系统的动画

    [self.navigationController pushViewController:next animated:NO];

        }

}

 

- (void)right:(UILongPressGestureRecognizer *)sender {

    [self changeImageWithDirection:Right];

}

- (void)left:(UILongPressGestureRecognizer *)sender {

    [self changeImageWithDirection:Left];

}

 

- (void)changeImageWithDirection:(Direction)direction {

//    通过判断 图片是自加 或者 自减

    index = direction == Right ? [self addSelf]:[self releaseSelf];

 

#pragma mark - 添加转场动画

    CATransition *transition = [CATransition animation];

    transition.type = direction == Right ? @"cube":@"rippleEffect";

    transition.subtype = direction == Right ? kCATransitionFromRight:kCATransitionFromLeft;

    transition.duration = 1;

    [showImage.layer addAnimation:transition forKey:nil];

    

    showImage.image = [UIImage imageNamed:imageList[index]];

}

 

#pragma mark - 向左滑动 图片自加

// 需要通过方向  判断 自加 还是 自减  把计算好的值 赋给全局变量index

- (int)addSelf {

    index ++;

//    如果超出了 图片的元素  修复成0

//    如果没有超出  返回 自加之后的值

    return index >= imageList.count-1 ? 0:index;

}

 

#pragma mark - 向右滑动 图片自减

- (int)releaseSelf {

    index --;

    return index < 0 ? (int)imageList.count-1:index;

}

 

 

 

    

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

@end

 转场动画(长按屏幕有不同的翻转效果)