• Stars
    star
    372
  • Rank 114,858 (Top 3 %)
  • Language
    Objective-C
  • Created almost 10 years ago
  • Updated over 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

注:其中有些效果调用了CATransition的Private API, 仅供娱乐

CATransitionDemo

注:其中有些效果调用了CATransition的Private API, 仅供娱乐。

补充:还是有好多小伙伴问那些可以在AppStore中使用,调用私有API的肯定是不行的,而下方有些就调用了私有API。

下方会用到一些私有API, 有些效果在APPStore中是不能使用的,私有API如下:

NSString *const kCATransitionCube = @"cube";  
NSString *const kCATransitionSuckEffect = @"suckEffect"; 
NSString *const kCATransitionOglFlip = @"oglFlip";  
NSString *const kCATransitionRippleEffect = @"rippleEffect";  
NSString *const kCATransitionPageCurl = @"pageCurl"; 
NSString *const kCATransitionPageUnCurl = @"pageUnCurl";   
NSString *const kCATransitionCameraIrisHollowOpen = @"cameraIrisHollowOpen";
NSString *const kCATransitionCameraIrisHollowClose = @"cameraIrisHollowClose";

而下方这些可以放心使用:

// CAAnimation.h

/* Common transition types. */

CA_EXTERN NSString * const kCATransitionFade
    __OSX_AVAILABLE_STARTING (__MAC_10_5, __IPHONE_2_0);
CA_EXTERN NSString * const kCATransitionMoveIn
    __OSX_AVAILABLE_STARTING (__MAC_10_5, __IPHONE_2_0);
CA_EXTERN NSString * const kCATransitionPush
    __OSX_AVAILABLE_STARTING (__MAC_10_5, __IPHONE_2_0);
CA_EXTERN NSString * const kCATransitionReveal
    __OSX_AVAILABLE_STARTING (__MAC_10_5, __IPHONE_2_0);

今天主要用到的动画类是CALayer下的CATransition至于各种动画类中如何继承的在这也不做赘述,网上的资料是一抓一大把。好废话少说切入今天的正题。

一.封装动画方法

1.用CATransition实现动画的封装方法如下,每句代码是何意思,请看注释之。

#pragma CATransition动画实现
- (void) transitionWithType:(NSString *) type WithSubtype:(NSString *) subtype ForView : (UIView *) view
{
    //创建CATransition对象
    CATransition *animation = [CATransition animation];
    
    //设置运动时间
    animation.duration = DURATION;
    
    //设置运动type
    animation.type = type;
    if (subtype != nil) {
        
        //设置子类
        animation.subtype = subtype;
    }
    
    //设置运动速度
    animation.timingFunction = UIViewAnimationOptionCurveEaseInOut;
    
    [view.layer addAnimation:animation forKey:@"animation"];
}

代码说明:

  • CATransition常用的属性如下:

  • duration:设置动画时间

  • type:稍后下面会详细的介绍运动类型

  • subtype:和type匹配使用,指定运动的方向,下面也会详细介绍

  • timingFunction :动画的运动轨迹,用于变化起点和终点之间的插值计算,形象点说它决定了动画运行的节奏,比如是

  • 均匀变化(相同时间变化量相同)还是先快后慢,先慢后快还是先慢再快再慢.    

    • 动画的开始与结束的快慢,有五个预置分别为(下同):
    • kCAMediaTimingFunctionLinear 线性,即匀速
    • kCAMediaTimingFunctionEaseIn 先慢后快
    • kCAMediaTimingFunctionEaseOut 先快后慢
    • kCAMediaTimingFunctionEaseInEaseOut 先慢后快再慢
    • kCAMediaTimingFunctionDefault 实际效果是动画中间比较快.

2.用UIView的block回调实现动画的代码封装 

#pragma UIView实现动画
- (void) animationWithView : (UIView *)view WithAnimationTransition : (UIViewAnimationTransition) transition
{
    [UIView animateWithDuration:DURATION animations:^{
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        [UIView setAnimationTransition:transition forView:view cache:YES];
    }];
}

3.改变View的背景图,便于切换时观察

#pragma 给View添加背景图
-(void)addBgImageWithImageName:(NSString *) imageName
{
     self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:imageName]];
}

二.调用上面的方法实现我们想要的动画

1.我们在View上添加多个Button,给不同的Button设置不同的Tag值,然后再ViewController中绑定同一个方法,点击不同的button实现不同的页面切换效果。storyBoard上的控件效果如下图所示:

2.下面我们就开始编写点击button要回调的方法

(1).定义枚举来标示按钮所对应的动画类型,代码如下:

typedef enum : NSUInteger {
    Fade = 1,                   //淡入淡出
    Push,                       //推挤
    Reveal,                     //揭开
    MoveIn,                     //覆盖
    Cube,                       //立方体
    SuckEffect,                 //吮吸
    OglFlip,                    //翻转
    RippleEffect,               //波纹
    PageCurl,                   //翻页
    PageUnCurl,                 //反翻页
    CameraIrisHollowOpen,       //开镜头
    CameraIrisHollowClose,      //关镜头
    CurlDown,                   //下翻页
    CurlUp,                     //上翻页
    FlipFromLeft,               //左翻转
    FlipFromRight,              //右翻转
    
} AnimationType;

(2),获取Button的Tag值:

     UIButton *button = sender;
     AnimationType animationType = button.tag;

(3).每次点击button都改变subtype的值,包括上,左,下,右

NSString *subtypeString;
    
    switch (_subtype) {
        case 0:
            subtypeString = kCATransitionFromLeft;
            break;
        case 1:
            subtypeString = kCATransitionFromBottom;
            break;
        case 2:
            subtypeString = kCATransitionFromRight;
            break;
        case 3:
            subtypeString = kCATransitionFromTop;
            break;
        default:
            break;
    }
    _subtype += 1;
    if (_subtype > 3) {
        _subtype = 0;
    }

(4),通过switch结合上边的枚举来判断是那个按钮点击的

 switch (animationType)
{
     //各种Case,此处代码下面会给出  
}

3.调用我们封装的运动方法,来实现动画效果

(1),淡化效果

 case Fade:
     [self transitionWithType:kCATransitionFade WithSubtype:subtypeString ForView:self.view];
     break;

(2) PUSH效果

 case Push:
     [self transitionWithType:kCATransitionPush WithSubtype:subtypeString ForView:self.view];
     break;

(3)、揭开效果

 case Reveal:
     [self transitionWithType:kCATransitionReveal WithSubtype:subtypeString ForView:self.view];
     break;

(4)、覆盖效果

case MoveIn:
    [self transitionWithType:kCATransitionMoveIn WithSubtype:subtypeString ForView:self.view];
    break;

(5)、立方体切换

case Cube:
    [self transitionWithType:@"cube" WithSubtype:subtypeString ForView:self.view];
    break;

(6)、吮吸效果

case SuckEffect:
    [self transitionWithType:@"suckEffect" WithSubtype:subtypeString ForView:self.view];
    break;

(7)、翻转效果

case OglFlip:
    [self transitionWithType:@"oglFlip" WithSubtype:subtypeString ForView:self.view];
    break;

(8)、波纹效果

case RippleEffect:
    [self transitionWithType:@"rippleEffect" WithSubtype:subtypeString ForView:self.view];
    break;

(9)、翻页效果

case PageCurl:
    [self transitionWithType:@"pageCurl" WithSubtype:subtypeString ForView:self.view];
    break;

case PageUnCurl:
    [self transitionWithType:@"pageUnCurl" WithSubtype:subtypeString ForView:self.view];
    break;

(10)、相机打开效果

case CameraIrisHollowOpen:
    [self transitionWithType:@"cameraIrisHollowOpen" WithSubtype:subtypeString ForView:self.view];
    break;

case CameraIrisHollowClose:
    [self transitionWithType:@"cameraIrisHollowClose" WithSubtype:subtypeString ForView:self.view];
    break;

(11),调用上面封装的第二个动画方法

case CurlDown:
    [self animationWithView:self.view WithAnimationTransition:UIViewAnimationTransitionCurlDown];
    break;

case CurlUp:
    [self animationWithView:self.view WithAnimationTransition:UIViewAnimationTransitionCurlUp];
    break;

case FlipFromLeft:
    [self animationWithView:self.view WithAnimationTransition:UIViewAnimationTransitionFlipFromLeft];
    break;

case FlipFromRight:
    [self animationWithView:self.view WithAnimationTransition:UIViewAnimationTransitionFlipFromRight];
    break;

More Repositories

1

MVP

MVP
Objective-C
815
star
2

ZLCustomeSegmentControlView

视错觉Demo介绍
Objective-C
231
star
3

DataStruct-Swift

数据结构相关Swift示例,相关博客地址:
Swift
178
star
4

ObjCRuntimeDemo

http://www.cnblogs.com/ludashi/p/6294112.html
Objective-C
174
star
5

SliderTabBar

多个表视图手势切换,加手势的选项卡~
Objective-C
140
star
6

WeChat

妨微信聊天
Objective-C
119
star
7

CEThemeSwitcher

http://www.cnblogs.com/ludashi/p/6289741.html
Swift
66
star
8

DisplayTestDemo

UI性能测试
Objective-C
63
star
9

MVVMWithReactiveCocoa

一个使用ReactiveCocoa的MVVM
Objective-C
58
star
10

MaskLayerDemo

MaskLayer的使用
Swift
48
star
11

PerfectDemo

使用Swift的Perfect开发Web服务端
Makefile
46
star
12

ZLImageScrollView

http://www.cnblogs.com/ludashi/p/4748209.html#3401571
Objective-C
45
star
13

ZLImageViewDisplay

iOS图片轮播
Objective-C
42
star
14

MasonryDemo

MasonryDemo和Masonry源码解析
Objective-C
41
star
15

DesignPatterns-Swift

使用Swift实现的常用设计模式示例
Swift
34
star
16

CodeRefactoring-Swift

重构·改善既有代码的设计(Swift版)
Swift
32
star
17

LoginManagerSDKSimpleDemo

登录SDK简化版Demo,对应博客地址:
Objective-C
29
star
18

FlyOver

Swift
27
star
19

FATabSwitchView

频道选择放大组件
Swift
26
star
20

GCDDemo-Swift

GCD 内容相关Demo, Swift版
Swift
23
star
21

CustomWaterfallFlow

使用UICollectionView,自定义布局实现可定制化的瀑布流
Objective-C
19
star
22

CEImagesScrollDisplay

iOS幻灯片(参考淘宝幻灯片实现)
Swift
18
star
23

CEScrollMenue

资讯类App中的分类展示、分类切换、分类编辑
Swift
14
star
24

NotificationWithSubThread

NotificationWithSubThread
Objective-C
13
star
25

CESelectThemes

Swift
11
star
26

NSTimerWithRunLoop

Swift
10
star
27

CollectionViewControllerDemo

CollectionView中自带的流试布局
Objective-C
10
star
28

NSURLSessionDemo

URLSession for Swift3.0
Swift
9
star
29

FPSProfileDemo

Objective-C
9
star
30

iOS_NetWorkingAndAlamofire

iOS网络编程与Alamofire源码解析
Swift
9
star
31

SpringDemo

JavaEE开发框架Spring4+相关Demo
Java
8
star
32

CustomCollectionViewLayout

Objective-C
8
star
33

SwiftDelegateDemo

Swift中协议以及委托代理回调实例
Swift
8
star
34

SpringBootProject

Java
7
star
35

SpotlightSearchDemo

Objective-C
7
star
36

DependencyInjectionForiOS

iOS中的依赖注入
Objective-C
7
star
37

CustomTransformCollecionLayout

使用CollectionView自定义相片浏览器。博客地址:
Objective-C
7
star
38

TellBook

iOS通讯录
Objective-C
6
star
39

AutoLayoutContentHuggingAndCompressionResistance

AutoLayout Content Hugging And Compression Resistance Demo
Swift
5
star
40

CustomeFaceKeyBoard

自定义表情键盘,支持历史表情记录
Objective-C
5
star
41

CGAffineTransformDemo

仿射变换总结-Swift版
Swift
4
star
42

ChatDemo

微信聊天部分实现
Objective-C
3
star
43

AndroidBaseControlAndLayout

安卓开发基本控件和四种布局方式
Java
3
star
44

TipSwiftForRac

Swift
3
star
45

ProvincePickerDemo

省市Picker
PHP
3
star
46

SQLiteResource

SQLite相关博文所需资源
C
2
star
47

SpringMVCWithMaven

Java
2
star
48

AspectOrientedForiOS

iOS面向切面编程
Objective-C
2
star
49

SinaWeibo

新浪微博客户端实现
Objective-C
2
star
50

ReactNativeTestDemo

RN相关的使用示例
TypeScript
2
star
51

PDFWithQLPreviewController

PDF文件下载并预览
Swift
1
star
52

AndroidListViewDemo

Android开发中ListView使用实例
Java
1
star
53

BlueToothDemo

一个关于BLE4.0的Demo
Objective-C
1
star
54

CEExtension-Swift

iOS开发常用扩展-Swift版, Updating……
Swift
1
star
55

AndroidCustomNavigationBar

安卓中自定义导航栏
Java
1
star
56

CETabDisplay

标签展示
Swift
1
star
57

Location

地图定位
Objective-C
1
star