转场动画CATransition入门须知:
1、 CATransition 转场动画 可以切换视图 视图控制器
2、CATransition 的两个重要属性
type 转场动画的 动画效果
subtype 转场动画动画的方向
3、type的动画效果如下:
kCATransitionFade 交叉淡化过渡
kCATransitionMoveIn 新视图移到旧视图上面
kCATransitionPush 新视图把旧视图推出去
kCATransitionReveal 将旧视图移开,显示下面的新视图
私有api 不建议使用 苹果不提供维护 并且有可能app审核不通过
pageCurl 向上翻一页
pageUnCurl 向下翻一页
rippleEffect 滴水效果
suckEffect 收缩效果 如一块布被抽走
cube 立方体效果
oglFlip 上下翻转效果
1、定义3张图片的数组,在初始化一个UIImageView,然后再self。view上添加三个手势触发不同的方法
解析:左划手势,显示后面的图片;右划手势,显示前面的图片;长按手势,跳到下一个页面
<span style="color:#333333;">{
NSArray *imageList;
UIImageView *showImage;
NSInteger index;
}</span>
这是定义的全局变量。
<span style="color:#333333;"> imageList = @[@"101.jpg",@"105.jpg",@"108.jpg"];
showImage = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
showImage.image = [UIImage imageNamed:imageList[0]];
[self.view addSubview:showImage];
UISwipeGestureRecognizer *right = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(right)];
right.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:right];
UISwipeGestureRecognizer *left = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(left)];
left.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:left];
UILongPressGestureRecognizer *longpress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(next:)];
[self.view addGestureRecognizer:longpress];</span>
2、右划调用方法
<span style="color:#333333;">- (void)right
{
index --;
if (index < 0) {
index = imageList.count-1;
}
</span><pre name="code" class="objc" style="font-size: 14px;"><span style="color:#333333;"> CATransition *transition1 = [[CATransition alloc]init];
transition1.type = @"cube"; </span><span style="color:#33cc00;">// 可更改为我们一开始就提到的动画效果</span><span style="color:#333333;">
transition1.subtype = kCATransitionFromRight; </span><span style="color:#33cc00;">// 动画执行的方向</span><span style="color:#333333;">
transition1.duration = 1;
[showImage.layer addAnimation:transition1 forKey:@"transition1"];</span>
showImage.image = [UIImage imageNamed:imageList[index]];}
3、左划调用方法
<span style="color:#333333;">- (void)left
{
index ++;
if (index>=imageList.count) {
index = 0;
}</span>
<pre name="code" class="objc" style="font-size: 14px;"><span style="color:#333333;"> CATransition *transition2 = [[CATransition alloc]init];
transition2.type = @"rippleEffect"; </span><span style="color: rgb(51, 204, 0); font-family: 'Heiti SC Light';">// 可更改为我们一开始就提到的动画效果</span><span style="color:#333333;">
transition2.subtype = kCATransitionFromLeft;
transition2.duration = 1;
[showImage.layer addAnimation:transition2 forKey:@"transition2"];</span>
showImage.image = [UIImage imageNamed:imageList[index]];}
4、长按切换视图调用方法
<span style="color:#333333;">- (void)next:(UILongPressGestureRecognizer *)sender
{
if (sender.state == UIGestureRecognizerStateBegan) {
NextViewController *next = [[NextViewController alloc]init];
CATransition *transition = [[CATransition alloc]init];
transition.type = @"rippleEffect";
transition.subtype = kCAGravityTopLeft;
transition.duration = 1;
[self.navigationController.view.layer addAnimation:transition forKey:@"transition"];
[self.navigationController pushViewController:next animated:NO];
}
}</span>
切记
,
在使用长按手势切换视图的时候,我们得对长按手势进行判断,否则系统会一直调用长按手势这个方法
<span style="color: rgb(204, 51, 204); white-space: pre;"> </span><span style="color:#ff0000;">if (sender.state == UIGestureRecognizerStateBegan) {
<span style="white-space: pre;"> </span>}</span>
判断手势的状态开始后就执行这个方法