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

自定义转场效果 presentViewController实现由左向右

2016-06-01 15:06 441 查看
本文主要是记录一些在网上找到的一些方法,然后自己整理了下让自己更好理解一点, 如有侵权,请告知

本次实现的代码主要是在网上下载的一些转场动画的基础上更改一下

但是源代码下载地址给忘了,有点对不起原作者了,所以才想着平时记录一些代码,不然过段时间就又会忘记,

刚刚找源代码地址没找到,找到了几篇文章
http://blog.csdn.net/ityanping/article/details/39270609
该文章内容为:

视图切换,没有NavigationController的情况下,一般会使用presentViewController来切换视图并携带切换时的动画,

其中切换方法如下:

– presentViewController:animated:completion: 弹出,出现一个新视图 可以带动画效果,完成后可以做相应的执行函数经常为nil

– dismissViewControllerAnimated:completion:退出一个新视图 可以带动画效果,完成后可以做相应的执行函数经常为nil

切换动画在压入一个新视图和弹出顶层视图均可以使用,下面只以压入视图为例。

presentModalViewController:animated:completion:使用系统自带四种动画

简单的实现方式:

[page2Controller setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];

[self presentModalViewController:myNextViewController animated:YES  completion:nil];

系统支持的四种动画:

typedef enum {

UIModalTransitionStyleCoverVertical=0, //默认方式,竖向上推

UIModalTransitionStyleFlipHorizontal, //水平反转

UIModalTransitionStyleCrossDissolve,//隐出隐现

UIModalTransitionStylePartialCurl,//部分翻页效果

} UIModalTransitionStyle;

presentModalViewController:animated:completion: 不用自带的四种动画效果

实现全翻页效果:

CATransition *animation = [CATransition animation];

animation.duration = 1.0;

animation.timingFunction = UIViewAnimationCurveEaseInOut;

animation.type = @"pageCurl";

//animation.type = kCATransitionPush;

animation.subtype = kCATransitionFromLeft;

[self.view.window.layer addAnimation:animation forKey:nil];

[self presentModalViewController:myNextViewController animated:NO completion:nil];

常見的轉換類型(type):

kCATransitionFade              //淡出

kCATransitionMoveIn          //覆盖原图

kCATransitionPush              //推出

kCATransitionReveal          //底部显出来

SubType:

kCATransitionFromRight

kCATransitionFromLeft    // 默认值

kCATransitionFromTop

kCATransitionFromBottom

设置其他动画类型的方法(type):

pageCurl  向上翻一页

pageUnCurl 向下翻一页

rippleEffect 滴水效果

suckEffect 收缩效果,如一块布被抽走

cube 立方体效果

oglFlip 上下翻转效果;

自己实现:

代码地址:http://download.csdn.net/my

首先自定义一个类来管理转场效果

主要代码有

.h文件

声明一个枚举来定义转场类型

typedef enum : NSUInteger {

/**

*  淡入淡出

*/

AnimationTypeFade = 1,

/**

*  推挤

*/

AnimationTypePush,

/**

*  揭开

*/

AnimationTypeReveal,

/**

*  覆盖

*/

AnimationTypeMoveIn,

/**

*  立方体

*/

AnimationTypeCube,

/**

*  吮吸

*/

AnimationTypeSuckEffect,

/**

*  翻转

*/

AnimationTypeOglFlip,

/**

*  波纹

*/

AnimationTypeRippleEffect,

/**

*  翻页

*/

AnimationTypePageCurl,

/**

*  反翻页

*/

AnimationTypePageUnCurl,

/**

*  开镜头

*/

AnimationTypeCameraIrisHollowOpen,

/**

*  关镜头

*/

AnimationTypeCameraIrisHollowClose,

/**

*  下翻页

*/

AnimationTypeCurlDown,

/**

*  上翻页

*/

AnimationTypeCurlUp,

/**

*  左翻转

*/

AnimationTypeFlipFromLeft,

/**

*  右翻转

*/

AnimationTypeFlipFromRight,

} AnimationType;

再声明一个枚举来管理转场方向

/**

定义转场方向

*/

typedef enum : NSUInteger {

AnimationDirectionBottom = 0,

AnimationDirectionLeft,

AnimationDirectionRight,

AnimationDirectionTop

} AnimationDirection;

声明一个方法

/**

*  自定义转场效果

*

*  @param type              转场类型 push 或者 其他类型

*  @param animationDirection 转场方向

*  @param duration          时间 默认为1s

*  @param fromViewController fromVC

*  @param toViewController  toVC

*/

+ (void)transitionWithType:(AnimationType)type WithAnimationDirection:(AnimationDirection)animationDirection duration:(NSTimeInterval)duration fromVC:(UIViewController *)fromViewController toVC:(UIViewController *)toViewController;

.m文件实现方法

+ (void)transitionWithType:(AnimationType)type WithAnimationDirection:(AnimationDirection)animationDirection duration:(NSTimeInterval)duration fromVC:(UIViewController *)fromViewController toVC:(UIViewController *)toViewController

{

//创建CATransition对象

CATransition *animation = [CATransition animation];

NSString *animationType = kCATransitionFade;

//设置运动时间

animation.duration = duration ? duration : 1.f;

switch (type) {

case AnimationTypeFade://淡入淡出

{

animationType =kCATransitionFade;

}

break;

case AnimationTypePush://推挤

{

animationType =kCATransitionPush;

}

break;

case AnimationTypeReveal://揭开

{

animationType =kCATransitionReveal;

}

break;

case AnimationTypeMoveIn://覆盖

{

animationType =kCATransitionMoveIn;

}

break;

case AnimationTypeCube://立方体

{

animationType =@"cube";

}

break;

case AnimationTypeSuckEffect://吮吸

{

animationType =@"suckEffect";

}

break;

case AnimationTypeOglFlip://翻转

{

animationType =@"oglFlip";

}

break;

case AnimationTypeRippleEffect://波纹

{

animationType =@"rippleEffect";

}

break;

case AnimationTypePageCurl://翻页

{

animationType =@"pageCurl";

}

break;

case AnimationTypePageUnCurl://反翻页

{

animationType =@"pageUnCurl";

}

break;

case AnimationTypeCameraIrisHollowOpen://开镜头

{

animationType =@"cameraIrisHollowOpen";

}

break;

case AnimationTypeCameraIrisHollowClose://关镜头

{

animationType =@"cameraIrisHollowClose";

}

break;

case AnimationTypeCurlDown://下翻页

{

animationType =@"pageUnCurl";

}

break;

case AnimationTypeCurlUp://上翻页

{

animationType =@"pageUnCurl";

}

break;

case AnimationTypeFlipFromLeft://左翻转

{

animationType =@"pageUnCurl";

}

break;

case AnimationTypeFlipFromRight://右翻转

{

animationType =@"pageUnCurl";

}

break;

default:

break;

}

NSString *subtype = kCATransitionFromRight;

switch (animationDirection) {

case AnimationDirectionBottom:

{

subtype =kCATransitionFromBottom;

}

break;

case AnimationDirectionLeft:

{

subtype =kCATransitionFromLeft;

}

break;

case AnimationDirectionRight:

{

subtype =kCATransitionFromRight;

}

break;

case AnimationDirectionTop:

{

subtype =kCATransitionFromTop;

}

break;

default:

break;

}

//设置运动type

animation.type = animationType;

if (subtype != nil) {

//设置子类

animation.subtype = subtype;

}

//设置运动速度

animation.timingFunction = UIViewAnimationOptionCurveEaseInOut;

[fromViewController.view.window.layer addAnimation:animation forKey:@"animation"];

[fromViewController presentViewController:toViewController animated:NO completion:nil];

}

demo下载地址:http://download.csdn.net/detail/qq_31927183/9553330
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息