本文主要是記錄一些在網上找到的一些方法,然后自己整理了下讓自己更好理解一點, 如有侵權,請告知
本次實現的代碼主要是在網上下載的一些轉場動畫的基礎上更改一下
但是源代碼下載地址給忘了,有點對不起原作者了,所以才想著平時記錄一些代碼,不然過段時間就又會忘記,
剛剛找源代碼地址沒找到,找到了幾篇文章
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];
}