iOS 轉(zhuǎn)場動(dòng)畫簡單實(shí)現(xiàn) (push)

先看效果

項(xiàng)目中有要求要用一個(gè)切換特效,研究了下發(fā)現(xiàn)是拿轉(zhuǎn)場動(dòng)畫做的,所以把之前的知識(shí)重新復(fù)習(xí)下,這里提供一個(gè)簡單的思路實(shí)現(xiàn)轉(zhuǎn)場動(dòng)畫,具體的效果大概是跳轉(zhuǎn)的新的控制器頁面從左上角開始向右邊拉伸到充滿屏幕,同時(shí)底部有一個(gè)view彈上來.

我想創(chuàng)建兩個(gè)控制器,當(dāng)前控制器是GHTransitionAnimationViewController 目標(biāo)控制器是GHTransitionAnimationToViewController

在GHTransitionAnimationViewController 需要設(shè)置navigationController.delegate,遵守協(xié)議UINavigationControllerDelegate

然后新建一個(gè)繼承自NSObejct的GHTransitionAnimation類,這個(gè)類需要遵守協(xié)議UIViewControllerAnimatedTransitioning 同時(shí)必須實(shí)現(xiàn)兩個(gè)代理方法

這個(gè)代理方法是返回的動(dòng)畫持續(xù)時(shí)間, NSTimeInterval類型

-?(NSTimeInterval)transitionDuration:(id?)transitionContext{

}

方法是轉(zhuǎn)場動(dòng)畫的核心方法,所有的動(dòng)畫需要在這個(gè)方法里去寫

-?(void)animateTransition:(id?)transitionContext?{

}

transitionContext是轉(zhuǎn)場上下文,提供轉(zhuǎn)場動(dòng)畫的所有細(xì)節(jié),在上下文我們可以拿到原控制器和目標(biāo)控制器

-?(void)animateTransition:(id?)transitionContext{

/**?目標(biāo)控制器?*/

UIViewController?*toViewController?=?[transitionContext?viewControllerForKey:UITransitionContextToViewControllerKey];

/**?原控制器?*/

UIViewController?*fromViewController?=?[transitionContext?viewControllerForKey:UITransitionContextFromViewControllerKey];

UIView?*toView?=?nil;

UIView?*fromView?=?nil;

if?([transitionContext?respondsToSelector:@selector(viewForKey:)])?{

fromView?=?[transitionContext?viewForKey:UITransitionContextFromViewKey];

toView?=?[transitionContext?viewForKey:UITransitionContextToViewKey];

}?else?{

fromView?=?fromViewController.view;

toView?=?toViewController.view;

}

/**?containView?*/

UIView?*containView?=?[transitionContext?containerView];

[containView?addSubview:toView];

UIImageView?*imageView?=?[toView?subviews].firstObject;

imageView.frame?=?CGRectMake(0,?64,?0,?0);

UIView?*view?=?[toView?subviews].lastObject;

view.frame?=?CGRectMake(0,?kScreenHeight??-?64,?kScreenWidth,?50);

CGFloat?width?=?[UIScreen?mainScreen].bounds.size.width;

CGFloat?height?=?[UIScreen?mainScreen].bounds.size.height;

[containView?addSubview:imageView];

toView.frame?=?CGRectMake(0,?64,?0,?0);

[UIView?animateWithDuration:[self?transitionDuration:transitionContext]?animations:^{

//????????view.frame?=?CGRectMake(0,?kScreenHeight?-?50?-?64,?kScreenWidth,?50);

toView.frame?=?CGRectMake(0,?64,?width,?height);

imageView.frame?=?CGRectMake(0,?64,?kScreenWidth,?220);

view.frame?=?CGRectMake(0,?kScreenHeight?-?50?-?64,?kScreenWidth,?50);

}?completion:^(BOOL?finished)?{

[transitionContext?completeTransition:YES];

}];

}

GHTransitionAnimationToViewController 里創(chuàng)建兩個(gè)view

GHImageView?*imageView?=?[[GHImageView?alloc]init];

imageView.image?=?[UIImage?imageNamed:@"jingtian1"];

imageView.frame?=?CGRectMake(0,?64,?kScreenWidth,?220);

imageView.tag?=?10;

[self.view?addSubview:imageView];

self.view.backgroundColor?=?[UIColor?redColor];

GHView?*bottomView?=?[[GHView?alloc]init];

bottomView.frame?=?CGRectMake(0,?kScreenHeight?-?50?-?64,?kScreenWidth,?50);

bottomView.backgroundColor?=?[UIColor?orangeColor];

bottomView.tag?=?10;

[self.view?addSubview:bottomView];

最后要調(diào)用方法,在原控制器里添加自定義的類

-?(id)navigationController:(UINavigationController?*)navigationController?animationControllerForOperation:(UINavigationControllerOperation)operation?fromViewController:(UIViewController?*)fromVC?toViewController:(UIViewController?*)toVC{

if?(operation?==?UINavigationControllerOperationPush)?{

return?[[GHTransitionAnimation?alloc]?init];

}

return?nil;

}

說明: [transitionContext completeTransition:YES];這個(gè)方法是告訴控制器轉(zhuǎn)場動(dòng)畫完成,在結(jié)束動(dòng)畫一定要寫,然后控制器認(rèn)為動(dòng)畫沒有結(jié)束,所有的控件沒有交互效果.

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 前言的前言 唐巧前輩在微信公眾號(hào)「iOSDevTips」以及其博客上推送了我的文章后,我的 Github 各項(xiàng)指標(biāo)...
    VincentHK閱讀 5,566評(píng)論 3 44
  • 在iOS中隨處都可以看到絢麗的動(dòng)畫效果,實(shí)現(xiàn)這些動(dòng)畫的過程并不復(fù)雜,今天將帶大家一窺iOS動(dòng)畫全貌。在這里你可以看...
    F麥子閱讀 5,262評(píng)論 5 13
  • 在iOS中隨處都可以看到絢麗的動(dòng)畫效果,實(shí)現(xiàn)這些動(dòng)畫的過程并不復(fù)雜,今天將帶大家一窺ios動(dòng)畫全貌。在這里你可以看...
    每天刷兩次牙閱讀 8,688評(píng)論 6 30
  • @上一章,介紹了主要的iOS7所增加的API,可以發(fā)現(xiàn),它們不是一個(gè)個(gè)死的方法,蘋果給我們開發(fā)者提供的是都是協(xié)議接...
    Detailscool閱讀 1,251評(píng)論 0 49
  • 今年的國慶節(jié)正逢中秋節(jié),一下子8天長假,好多人都在張羅著回家。同樣的,我也不能免俗,越臨近假日,心情越煩躁,總想要...
    云淑閱讀 461評(píng)論 3 1

友情鏈接更多精彩內(nèi)容