//3D轉場動畫的實現方法
// ViewController.m
// 3DAnimation
//
// Created by Apple-YangWei on 15/5/12.
// Copyright (c) 2015年 Apple-YangWei. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property(nonatomic,assign) int index;//記錄圖片的順序
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.index = 1;
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(43, 39, 235, 394)];
//imageView.image = [UIImage imageNamed:@"1.JPG"];
//點擊上一張
UIButton *previousButton = [[UIButton alloc] initWithFrame:CGRectMake(self.imageView.center.x-150, self.imageView.bounds.size.height+50, 88, 40)];
previousButton.backgroundColor = [UIColor grayColor];
[previousButton setTitle:@"previous" forState:UIControlStateNormal];
[previousButton addTarget:self action:@selector(previousPicture) forControlEvents:UIControlEventTouchUpInside];
//點擊下一張
UIButton *nextButton = [[UIButton alloc] initWithFrame:CGRectMake(self.imageView.center.x+50, self.imageView.bounds.size.height+50, 88, 40)];
nextButton.backgroundColor = [UIColor grayColor];
[nextButton setTitle:@"next" forState:UIControlStateNormal];
[nextButton addTarget:self action:@selector(nextPicture) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:previousButton];
[self.view addSubview:nextButton];
[self.view addSubview:imageView];
}
-(void)previousPicture{
self.index--;
if (self.index < 1) {
self.index = 7;
}
[self animateWith:kCATransitionFromLeft];
}
-(void)nextPicture{
self.index++;
if(self.index > 7){
self.index = 1;
}
[self animateWith:kCATransitionFromRight];
}
//獲取picture,並創建動畫
-(void)animateWith: (NSString *) direction{
NSString *imageName = [NSString stringWithFormat:@"%d.JPG", self.index];
UIImage *newImage = [UIImage imageNamed:imageName];
self.imageView.image = newImage;
// 1.創建核心動畫
CATransition *ca = [CATransition animation];
//動畫過度類型
ca.type = @"cube";
//動畫過度方向
ca.subtype = direction;
//動畫時間
ca.duration = 1;
//添加到圖層
[self.imageView.layer addAnimation:ca forKey:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end