//動畫
// ViewController.murl
// 轉場動畫-CATransitionspa
//.net
// Created by dc008 on 15/12/22.3d
// Copyright © 2015年 崔曉宇. All rights reserved.orm
//對象
#import "ViewController.h"圖片
#define IMAGE_COUNT 10//圖片數量ip
@interface ViewController ()字符串
{
UIImageView *_imageView;
int _current;
}
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_imageView = [[UIImageView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
_imageView.contentMode = UIViewContentModeScaleAspectFill;
_imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",_current]];
[self.view addSubview:_imageView];
//添加手勢
UISwipeGestureRecognizer *leftSwipeGesure = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(leftSwipe:)];
leftSwipeGesure.direction = UISwipeGestureRecognizerDirectionLeft;//設置手勢方向
[self.view addGestureRecognizer:leftSwipeGesure];
UISwipeGestureRecognizer *rightSwipeGesure = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(rightSwipe:)];
rightSwipeGesure.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:rightSwipeGesure];
}
- (void)rightSwipe : (UISwipeGestureRecognizer *)gesture{
NSLog(@"向右滑動");
[self transitionAnimation:YES];//YES表示下一張圖片
}
- (void)leftSwipe : (UISwipeGestureRecognizer *)gesture{
NSLog(@"向左滑動");
[self transitionAnimation:NO];//NO表示上一張圖片
}
#pragma mark 轉場動畫
- (void)transitionAnimation : (BOOL) isNext {
//1.建立動畫對象
CATransition *transition = [[CATransition alloc]init];
//2.設置動畫類型,對於蘋果官方沒有公開的動畫類型只能使用字符串
//cube 立方體翻轉效果
//oglFlip 翻轉效果
//suckEffect 收縮效果
//rippleEffect 水滴波紋效果
//pageCurl 向上翻頁效果
//pageUnCurl 向下翻頁效果
//cameralIrisHollowOpen 攝像頭打開效果
//cameralIrisHollowClose 攝像頭關閉效果
transition.type = @"cube";
//設置子類型(轉場動畫從什麼方向)
if (isNext) {
transition.subtype = kCATransitionFromRight;
}
else{
transition.subtype = kCATransitionFromLeft;
}
//設置動畫時間
transition.duration = 1.0;
//設置轉場後的新視圖
if (isNext) {
//下一張圖片
_current = (_current + 1) % IMAGE_COUNT;
}
else{
//上一張圖片
_current = (_current - 1 + IMAGE_COUNT) % IMAGE_COUNT;
}
_imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",_current]];
//添加動畫
[_imageView.layer addAnimation:transition forKey:@"CXY"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end