您的位置:首页 > 移动开发 > IOS开发

iOS3D转场动画

2015-07-11 13:55 363 查看
//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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: