轉場內容

//
//  ViewController.m
//  轉場動畫
//
//  Created by lanou3g on 16/6/8.
//  Copyright ? 2016年 fwlong. All rights reserved.
//

#import "ViewController.h"

#define IMAGE_COUNT 5
@interface ViewController ()
{

    UIImageView * _imageView;
    int _currentindex;

}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //定義imageView
    _imageView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    //這個圖片繪制view里顯示,并且比例不變
    _imageView.contentMode = UIViewContentModeScaleAspectFit;
    
    
    _imageView.image = [UIImage imageNamed:@"0.jpg"];
    //開啟用戶交互
    _imageView.userInteractionEnabled = YES;
    [self.view addSubview:_imageView];
    //添加手勢
    UISwipeGestureRecognizer * leftSwipGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftSwipe:)];
    //手勢方向
    leftSwipGesture.direction = UISwipeGestureRecognizerDirectionLeft;
    //添加
    [self.view addGestureRecognizer:leftSwipGesture];
    
    //添加手勢
    UISwipeGestureRecognizer * rightSwipGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightSwipe:)];
    //手勢方向
    rightSwipGesture.direction = UISwipeGestureRecognizerDirectionRight;
    //添加
    [self.view addGestureRecognizer:rightSwipGesture];

    
}
-(void)leftSwipe:(UISwipeGestureRecognizer *)getSture
{
    [self transitionAnimation:YES];
}
//轉場動畫
-(void)transitionAnimation:(BOOL)isNext
{
    //創(chuàng)建對象
    CATransition * transition = [CATransition new];
    //設置動畫類型,蘋果官方并沒有公開動畫類型,只能用字符串
    //cube:立體旋轉
    //pageCurl:翻頁
    //oglFlip:中心旋轉
    //    *  @"moveIn"                   新視圖移到舊視圖上面
    //    *  @"reveal"                   顯露效果(將舊視圖移開,顯示下面的新視圖)
    //    *  @"fade"                     交叉淡化過渡(不支持過渡方向)             (默認為此效果)
    //    *  @"pageCurl"                 向上翻一頁
    //    *  @"pageUnCurl"               向下翻一頁
    //    *  @"suckEffect"               收縮效果,類似系統(tǒng)最小化窗口時的神奇效果(不支持過渡方向)
    //    *  @"rippleEffect"             滴水效果,(不支持過渡方向)
    //    *  @"oglFlip"                  上下左右翻轉效果
    //    *  @"rotate"                   旋轉效果
    //    *  @"push"
    //    *  @"cameraIrisHollowOpen"     相機鏡頭打開效果(不支持過渡方向)
    //    *  @"cameraIrisHollowClose"    相機鏡頭關上效果(不支持過渡方向)
    //    tran.type = @"pageCurl";
    transition.type = @"cameraIrisHollowClose";
    //設置子類型
    if (isNext == YES) {
        //過度
        transition.subtype = kCATransitionFromRight;
    }else{
        transition.subtype = kCATransitionFromLeft;

    }
    //設置轉場動畫后的新視圖添加轉場動畫
    _imageView.image = [self getImage:isNext];
    [_imageView.layer addAnimation:transition forKey:nil];
}
-(UIImage *)getImage:(BOOL)isNext
{
    if (isNext) {
        //下一張
        _currentindex = (_currentindex + 1)%IMAGE_COUNT;
    }else{
        //前一張
        _currentindex = (_currentindex - 1 + IMAGE_COUNT)%IMAGE_COUNT;
    }
    return [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",_currentindex]];
}
-(void)rightSwipe:(UISwipeGestureRecognizer *)getSture
{
   [self transitionAnimation:NO];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容