IOS动画

//
//  ViewController.m
//  IOS-动画特效
//
//  Created by wangtouwang on 15/5/5.
//  Copyright (c) 2015年 wangtouwang. All rights reserved.
//

#import "ViewController.h"
#define kDuration 0.7   // 动画持续时间(秒)
#define KS_HEIGTH [UIScreen mainScreen].bounds.size.height
#define KS_WIDTH  [UIScreen mainScreen].bounds.size.Width

@interface ViewController ()

@property(nonatomic,strong) UIView *imageView;

@end

@implementation ViewController
@synthesize typeID;
@synthesize blueView;
@synthesize greenView;

-(void)animationFunction:(UIButton *)btn{
    NSInteger index =  btn.tag;
    CATransition *transition = [CATransition animation];
    //代理
    transition.delegate = self;
    //持续时间
    transition.duration= kDuration;
    //类型
    transition.type = [self getAnimationType:index];
    //方向
    transition.subtype = [self getSubType];
    // 动画的开始与结束的快慢*/
    transition.timingFunction = UIViewAnimationCurveEaseInOut;
    //事件源
    NSInteger blue = [[_imageView subviews] indexOfObject:blueView];
    NSInteger green = [[_imageView subviews] indexOfObject:greenView];
    [_imageView exchangeSubviewAtIndex:green withSubviewAtIndex:blue];
    
    //开始动画
    [_imageView.layer addAnimation:transition forKey:@"animation"];
}


-(void)animationFunction2:(UIButton *)btn{
    NSInteger index =  btn.tag;
    CGContextRef context = UIGraphicsGetCurrentContext();
    [UIView beginAnimations:nil context:context];//开始一个动画块
    [UIView setAnimationDelegate:self];//代理
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];//设置动画块中的动画属性变化的曲线。
    [UIView setAnimationDuration:kDuration];//在动画块中设置动画的延迟属性 (以秒为单位)
   //在动画块中为视图设置过渡
    switch (index) {
        case 10:
            [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view cache:YES];
            break;
        case 9:
            [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
            break;
        case 12:
            [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
            break;
        case 11:
            [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
            break;
        default:
            break;
    }
    //事件源
    NSInteger blue = [[_imageView subviews] indexOfObject:blueView];
    NSInteger green = [[_imageView subviews] indexOfObject:greenView];
    [_imageView exchangeSubviewAtIndex:green withSubviewAtIndex:blue];
    
    // 动画完毕后调用某个方法
    //[UIView setAnimationDidStopSelector:@selector(animationFinished:)];
    [UIView commitAnimations];//结束一个动画块并开始当他在动画块外时
}

-(NSString *)getAnimationType:(NSInteger)index{
    NSString *type=nil;
    switch (index) {
        case 5:
            type = kCATransitionFade;
            break;
        case 6:
            type = kCATransitionPush;
            break;
        case 7:
            type = kCATransitionReveal;
            break;
        case 8:
            type = kCATransitionMoveIn;
            break;
        case 4:
            type = @"cube";
            break;
        case 3:
            type = @"suckEffect";
            break;
        case 1:
            type = @"pageCurl";
            break;
        case 2:
            type = @"pageUnCurl";
            break;
        default:
            break;
    }

    return type;
}



-(NSString *)getSubType{
    NSString *subtype = nil;
    switch (self.typeID) {
        case 0:
            subtype = kCATransitionFromLeft;
            break;
        case 1:
            subtype = kCATransitionFromBottom;
            break;
        case 2:
            subtype = kCATransitionFromRight;
            break;
        case 3:
            subtype = kCATransitionFromTop;
            break;
        default:
            break;
    }
    self.typeID += 1;
    if (self.typeID > 3) {
        self.typeID = 0;
    }
    return subtype;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self.navigationItem setTitle:@"动画特效"];
    
    UIButton *addBtn1 = [[UIButton alloc] initWithFrame:CGRectMake(10,70, 80, 30)];
    [addBtn1 setTitle:@"翻页" forState:UIControlStateNormal];
    addBtn1.titleLabel.font=[UIFont systemFontOfSize:13.0f];
    [addBtn1 setBackgroundColor:[UIColor grayColor]];
    addBtn1.tag=1;
    [addBtn1 addTarget:self action:@selector(animationFunction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:addBtn1];
    
    UIButton *addBtn2 = [[UIButton alloc] initWithFrame:CGRectMake(100,70, 80, 30)];
    [addBtn2 setTitle:@"反翻页" forState:UIControlStateNormal];
    addBtn2.titleLabel.font=[UIFont systemFontOfSize:13.0f];
    [addBtn2 setBackgroundColor:[UIColor grayColor]];
    addBtn2.tag=2;
    [addBtn2 addTarget:self action:@selector(animationFunction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:addBtn2];
    
    UIButton *addBtn3 = [[UIButton alloc] initWithFrame:CGRectMake(190,70, 80, 30)];
    [addBtn3 setTitle:@"波纹" forState:UIControlStateNormal];
    addBtn3.titleLabel.font=[UIFont systemFontOfSize:13.0f];
    [addBtn3 setBackgroundColor:[UIColor grayColor]];
    addBtn3.tag=3;
    [addBtn3 addTarget:self action:@selector(animationFunction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:addBtn3];
    
    UIButton *addBtn4 = [[UIButton alloc] initWithFrame:CGRectMake(280,70, 80, 30)];
    [addBtn4 setTitle:@"立方体" forState:UIControlStateNormal];
    addBtn4.titleLabel.font=[UIFont systemFontOfSize:13.0f];
    [addBtn4 setBackgroundColor:[UIColor grayColor]];
    addBtn4.tag=4;
    [addBtn4 addTarget:self action:@selector(animationFunction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:addBtn4];
    
    UIButton *addBtn5 = [[UIButton alloc] initWithFrame:CGRectMake(10,110, 80, 30)];
    [addBtn5 setTitle:@"淡化" forState:UIControlStateNormal];
    addBtn5.titleLabel.font=[UIFont systemFontOfSize:13.0f];
    [addBtn5 setBackgroundColor:[UIColor grayColor]];
    addBtn5.tag=5;
    [addBtn5 addTarget:self action:@selector(animationFunction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:addBtn5];
    
    UIButton *addBtn6 = [[UIButton alloc] initWithFrame:CGRectMake(100,110, 80, 30)];
    [addBtn6 setTitle:@"推挤" forState:UIControlStateNormal];
    addBtn6.titleLabel.font=[UIFont systemFontOfSize:13.0f];
    [addBtn6 setBackgroundColor:[UIColor grayColor]];
    addBtn6.tag=6;
    [addBtn6 addTarget:self action:@selector(animationFunction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:addBtn6];
    
    UIButton *addBtn7 = [[UIButton alloc] initWithFrame:CGRectMake(190,110, 80, 30)];
    [addBtn7 setTitle:@"揭开" forState:UIControlStateNormal];
    addBtn7.titleLabel.font=[UIFont systemFontOfSize:13.0f];
    [addBtn7 setBackgroundColor:[UIColor grayColor]];
    addBtn7.tag=7;
    [addBtn7 addTarget:self action:@selector(animationFunction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:addBtn7];
    
    UIButton *addBtn8 = [[UIButton alloc] initWithFrame:CGRectMake(280,110, 80, 30)];
    [addBtn8 setTitle:@"覆盖" forState:UIControlStateNormal];
    addBtn8.titleLabel.font=[UIFont systemFontOfSize:13.0f];
    [addBtn8 setBackgroundColor:[UIColor grayColor]];
    addBtn8.tag=8;
    [addBtn8 addTarget:self action:@selector(animationFunction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:addBtn8];
    
    
    UIButton *addBtn9 = [[UIButton alloc] initWithFrame:CGRectMake(10,150, 80, 30)];
    [addBtn9 setTitle:@"上翻" forState:UIControlStateNormal];
    addBtn9.titleLabel.font=[UIFont systemFontOfSize:13.0f];
    [addBtn9 setBackgroundColor:[UIColor grayColor]];
    addBtn9.tag=9;
    [addBtn9 addTarget:self action:@selector(animationFunction2:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:addBtn9];
    
    UIButton *addBtn10 = [[UIButton alloc] initWithFrame:CGRectMake(100,150, 80, 30)];
    [addBtn10 setTitle:@"下翻" forState:UIControlStateNormal];
    addBtn10.titleLabel.font=[UIFont systemFontOfSize:13.0f];
    [addBtn10 setBackgroundColor:[UIColor grayColor]];
    addBtn10.tag=10;
    [addBtn10 addTarget:self action:@selector(animationFunction2:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:addBtn10];
    
    UIButton *addBtn11 = [[UIButton alloc] initWithFrame:CGRectMake(190,150, 80, 30)];
    [addBtn11 setTitle:@"左翻" forState:UIControlStateNormal];
    addBtn11.titleLabel.font=[UIFont systemFontOfSize:13.0f];
    [addBtn11 setBackgroundColor:[UIColor grayColor]];
    addBtn11.tag=11;
    [addBtn11 addTarget:self action:@selector(animationFunction2:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:addBtn11];
    
    UIButton *addBtn12 = [[UIButton alloc] initWithFrame:CGRectMake(280,150, 80, 30)];
    [addBtn12 setTitle:@"右翻" forState:UIControlStateNormal];
    addBtn12.titleLabel.font=[UIFont systemFontOfSize:13.0f];
    [addBtn12 setBackgroundColor:[UIColor grayColor]];
    addBtn12.tag=12;
    [addBtn12 addTarget:self action:@selector(animationFunction2:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:addBtn12];
    
    
    _imageView = [[UIView alloc] initWithFrame:CGRectMake(0, 190, [UIScreen mainScreen].bounds.size.width, KS_HEIGTH-190)];
    [self.view addSubview:_imageView];
    
    blueView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"XXX_123.png"]];
    blueView.frame=CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, KS_HEIGTH-150);
    [_imageView addSubview:blueView];
    
    greenView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"XXX_321.png"]];
    greenView.frame=CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, KS_HEIGTH-150);
    [_imageView addSubview:greenView];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}




@end

IOS动画_第1张图片

IOS动画_第2张图片

你可能感兴趣的:(ios)