#import "ViewController.h" #define IMAGE_COUNT 4 @interface ViewController () { CATransition *_transaction; UIImageView *_image; int _currentImage;//圖片名稱 } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; _image=[[UIImageView alloc]initWithFrame:[[UIScreen mainScreen]bounds]]; _image.contentMode=UIViewContentModeScaleAspectFill;//按比例填滿屏幕 _image.image=[UIImage imageNamed:[NSString stringWithFormat:@"%i",_currentImage]]; [self.view addSubview:_image]; //添加手勢---左劃 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:)]; right.direction=UISwipeGestureRecognizerDirectionRight; [self.view addGestureRecognizer:right]; } - (void) leftSwipe : (UISwipeGestureRecognizer *) swipeGesture{ [self transitionAnimation:YES]; } - (void) rightSwipe : (UISwipeGestureRecognizer *) swipeGesture{ [self transitionAnimation:NO]; } - (void) transitionAnimation:(BOOL) isNext{ //1.建立動畫對象 _transaction=[[CATransition alloc]init]; //2.設置動畫類型,對於蘋果官方沒有公開的動畫類型只能使用字符串 //cube 立方體翻轉效果 //oglFlip 翻轉效果 //suckEffect 收縮效果 //rippleEffect 水滴波紋效果 //pageCurl 向上翻頁效果 //pageUnCurl 向下翻頁效果 //cameralTrisHollowOpen 攝像頭打開效果 //cameraIrisHollowClose 攝像頭關閉效果 _transaction.type=@"rippleEffect"; if (isNext) { _transaction.subtype=kCATransitionFromLeft; }else{ _transaction.subtype=kCATransitionFromRight; } //設置動畫時間 _transaction.duration=4; //設置翻轉後的圖片 if (isNext) { _currentImage=(_currentImage+1) % IMAGE_COUNT; }else{ _currentImage=(_currentImage- 1 + IMAGE_COUNT) %IMAGE_COUNT; } //添加動畫 _image.image=[UIImage imageNamed:[NSString stringWithFormat:@"%i",_currentImage]]; [_image.layer addAnimation:_transaction forKey:@"YC"]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end