先看效果
項目中有要求要用一個切換特效,研究了下發(fā)現(xiàn)是拿轉(zhuǎn)場動畫做的,所以把之前的知識重新復(fù)習(xí)下,這里提供一個簡單的思路實現(xiàn)轉(zhuǎn)場動畫,具體的效果大概是跳轉(zhuǎn)的新的控制器頁面從左上角開始向右邊拉伸到充滿屏幕,同時底部有一個view彈上來.
我想創(chuàng)建兩個控制器,當(dāng)前控制器是GHTransitionAnimationViewController 目標(biāo)控制器是GHTransitionAnimationToViewController
在GHTransitionAnimationViewController 需要設(shè)置navigationController.delegate,遵守協(xié)議UINavigationControllerDelegate
然后新建一個繼承自NSObejct的GHTransitionAnimation類,這個類需要遵守協(xié)議UIViewControllerAnimatedTransitioning 同時必須實現(xiàn)兩個代理方法
這個代理方法是返回的動畫持續(xù)時間, NSTimeInterval類型
-?(NSTimeInterval)transitionDuration:(id?)transitionContext{
}
方法是轉(zhuǎn)場動畫的核心方法,所有的動畫需要在這個方法里去寫
-?(void)animateTransition:(id?)transitionContext?{
}
transitionContext是轉(zhuǎn)場上下文,提供轉(zhuǎn)場動畫的所有細節(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)建兩個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];這個方法是告訴控制器轉(zhuǎn)場動畫完成,在結(jié)束動畫一定要寫,然后控制器認為動畫沒有結(jié)束,所有的控件沒有交互效果.