首页 > 代码库 > 为UIView视图切换添加动画效果

为UIView视图切换添加动画效果

我们定义了一个动画类来实现视图切换的动画效果,这个类只包含一个类方法,可直接调用,具体代码如下:

头文件:

+ View Code?
1
2
3
4
5
6
7
8
9
10
11
12
13
#import <Foundation/Foundation.h>
 
@interface ViewAnimation : NSObject
 
/*============================页面切换的方法==============================
    View1 表示当前页面
    View2 表示目标页面
    VC    两个view所在的viewController
    共有12种动画效果和四个动画方向,这里采用随机挑选动画和方向的方案
======================================================================*/
+(void)TransView1:(UIView*)v1 View2:(UIView*)v2 VC:(UIViewController*)vc;
 
@end

 体文件:

+ View Code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#import "ViewAnimation.h"
#import <QuartzCore/QuartzCore.h>
 
#define kDuration 0.4       //  动画效果持续时间(秒)
 
@implementation ViewAnimation
 
//  页面切换的方法
+(void)TransView1:(UIView*)v1 View2:(UIView*)v2 VC:(UIViewController *)vc;
{
    CATransition *animation = [CATransition animation];
    animation.delegate = self;
    animation.duration = kDuration;
    animation.timingFunction = UIViewAnimationCurveEaseInOut;
     
    switch (rand()%11) {
        case 1:
            animation.type = kCATransitionFade;
            break;
        case 2:
            animation.type = kCATransitionPush;
            break;
        case 3:
            animation.type = kCATransitionReveal;
            break;
        case 4:
            animation.type = kCATransitionMoveIn;
            break;
        case 5:
            animation.type = @"cube";
            break;
        case 6:
            animation.type = @"suckEffect";
            break;
        case 7:
            animation.type = @"oglFlip";
            break;
        case 8:
            animation.type = @"rippleEffect";
            break;
        case 9:
            animation.type = @"pageCurl";
            break;
        case 10:
            animation.type = @"pageUnCurl";
            break;
        case 11:
            animation.type = @"cameraIrisHollowOpen";
            break;
        case 0:
            animation.type = @"cameraIrisHollowClose";
            break;
        default:
            animation.type = kCATransitionMoveIn;
            break;
    }
     
    switch (rand()%3) {
        case 0:
            animation.subtype = kCATransitionFromLeft;
            break;
        case 1:
            animation.subtype = kCATransitionFromBottom;
            break;
        case 2:
            animation.subtype = kCATransitionFromRight;
            break;
        case 3:
            animation.subtype = kCATransitionFromTop;
            break;
        default:
            animation.subtype = kCATransitionFromRight;
            break;
    }
     
    [vc.view addSubview:v2];
     
    NSInteger x1 = [[vc.view subviews]indexOfObject:v1];
    NSInteger x2 = [[vc.view subviews]indexOfObject:v2];
     
    [vc.view exchangeSubviewAtIndex:x1 withSubviewAtIndex:x2];
    [[vc.view layer]addAnimation:animation forKey:@"animation"];
}
 
@end