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

ios 转场动画

2015-03-06 19:40 323 查看
首先要名白什么是转场,转场就是每个段落(构成电视片的最小单位是镜头,一个个镜头连接在一起形成的镜头序列)都具有某个单一的、相对完整的意思,如表现一个动作过程,表现一种相关关系,表现一种含义等等。它是电视片中一个完整的叙事层次,就像戏剧中的幕,小说中的章节一样,一个个段落连接在一起,就形成了完整的电视片。因此,段落是电视片最基本的结构形式,电视片在内容上的结构层次是通过段落表现出来的。而段落与段落、场景与场景之间的过渡或转换,就叫做转场。

本质就是不同场景的过渡就是转场
ios专场动画就是一个实例(CAAnimation)所有的转场动画都是继承该类,在视图的图层上加上转场动画就产生了比较炫的过渡动画,现实现如
//
//  ViewController.m
//  转场动画
//
//  Created by dengyanzhou on 15/1/28.
//  Copyright (c) 2015年 dengyanzhou. All rights reserved
#import "ViewController.h"
#define IMAGE_COUNT 5
@interface ViewController (){
    UIImageView *_imageView;
     int _currentIndex;
    int _index;
    CALayer *_layer;
    NSMutableArray *_images;
}
@end
@implementation ViewController
- (void)viewDidLoad {
    [superviewDidLoad];
    //定义图片控件
    _imageView=[[UIImageViewalloc]init];
    _imageView.frame=[UIScreenmainScreen].applicationFrame;
    _imageView.contentMode=UIViewContentModeScaleAspectFit;
    _imageView.image=[UIImageimageNamed:@"0.jpg"];//默认图片
    [self.viewaddSubview:_imageView];
    //添加手势
    UISwipeGestureRecognizer *leftSwipeGesture=[[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@selector(leftSwipe:)];
    leftSwipeGesture.direction=UISwipeGestureRecognizerDirectionLeft;
    [self.viewaddGestureRecognizer:leftSwipeGesture];
    
    UISwipeGestureRecognizer *rightSwipeGesture=[[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@selector(rightSwipe:)];
    rightSwipeGesture.direction=UISwipeGestureRecognizerDirectionRight;
    [self.viewaddGestureRecognizer:rightSwipeGesture];
    //设置背景
    self.view.layer.contents = (id)[UIImageimageNamed:@"0.png"].CGImage;
    //创建图像显示图层
    _layer=[[CALayeralloc]init];
    _layer.bounds=CGRectMake(0,0,
400,300);
    _layer.position=CGPointMake(160,284);
    [self.view.layeraddSublayer:_layer];
    _images=[NSMutableArrayarray];
    for (int i=0; i<7; ++i) {
        NSString *imageName=[NSStringstringWithFormat:@"%d.jpg",i];
        UIImage *image=[UIImageimageNamed:imageName];
        [_imagesaddObject:image];
    }
//    
//    //定义时钟对象
//    CADisplayLink *displayLink=[CADisplayLink displayLinkWithTarget:self selector:@selector(step)];
//    //添加时钟对象到主运行循环
//    [displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];

    // Do any additional setup after loading the view, typically from a nib.
}

#pragma mark 每次屏幕刷新就会执行一次此方法(每秒接近60次)
-(void)step{
    //定义一个变量记录执行次数
    staticint s=0;
    //每秒执行6次
    if (++s%60==0) {
        UIImage *image=_images[_index];
        _layer.contents=(id)image.CGImage;//更新图片
        _index=(_index+1)%IMAGE_COUNT;
    }
}

#pragma mark 向左滑动浏览下一张图片
-(void)leftSwipe:(UISwipeGestureRecognizer *)gesture
{
    [selftransitionAnimation:YES];
}
#pragma mark 向右滑动浏览上一张图片
-(void)rightSwipe:(UISwipeGestureRecognizer *)gesture
{
    [selftransitionAnimation:NO];
}
#pragma mark 转场动画
-(void)transitionAnimation:(BOOL)isNext
{
       //1.创建转场动画对象
    CATransition *transition=[[CATransitionalloc]init];
    //2.设置动画类型,注意对于苹果官方没公开的动画类型只能使用字符串,并没有对应的常量定义
    transition.type=@"cube";
    //设置子类型
    if (isNext) {
        transition.subtype =kCATransitionFromRight;
    }else{
        transition.subtype =kCATransitionFromLeft;
    }
    //设置动画时常
    transition.duration=1.0f;
    [_imageView.layeraddAnimation:transition
forKey:@"KCTransitionAnimation"];
    //3.设置转场后的新视图添加转场动画
    _imageView.image=[selfgetImage:isNext]; 
  
}
#pragma mark 取得当前图片
-(UIImage *)getImage:(BOOL)isNext{
    if (isNext) {
        _currentIndex=(_currentIndex+1)%IMAGE_COUNT;
    }else{
        _currentIndex=(_currentIndex-1+IMAGE_COUNT)%IMAGE_COUNT;
    }
    NSString *imageName=[NSStringstringWithFormat:@"%i.jpg",_currentIndex];
    return [UIImageimageNamed:imageName];
}

- (void)didReceiveMemoryWarning {
    [superdidReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
效果如下





内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  专场动画