好久沒更新了,最近一直比較忙福澡,也是有點(diǎn)懶了沒更新,實在不好意思宛渐,學(xué)習(xí)的步伐不能停下啊竞漾。
好了,廢話不多說今天來聊一聊iOS的自定義轉(zhuǎn)場的實現(xiàn)窥翩。
簡述
iOS的新頁面的彈出方式分為兩種Push&Pop和Present&Dismiss业岁,轉(zhuǎn)場方式的自定義也主要針對這兩種方式進(jìn)行。其中Push&Pop方式需要針對Push和Pop分別定義動畫寇蚊,也就是說需要分別創(chuàng)建動畫文件笔时。而Present&Dismiss方式的話只需要一個即可。
Push&Pop方式
動畫文件
首先來看一下動畫文件的實現(xiàn)仗岸,
1允耿、先創(chuàng)建一個類繼承自NSObject
2、遵守協(xié)議UIViewControllerAnimatedTransitioning
3扒怖、接下來主要實現(xiàn)三個方法即可
@interface PushTransition : NSObject<UIViewControllerAnimatedTransitioning>
@property (nonatomic,strong) id transitionContext;//專場上下文较锡,用來協(xié)調(diào),from和to的頁面
@property (nonatomic,strong) UIViewController *fromViewController;//push操作主要針對fromViewController做動畫
@end
First
主要用來返回動畫執(zhí)行時間,即為push動畫的時間盗痒,直接return一個時間即可蚂蕴,單位為秒
- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext
{
return 2;
}
Second
下面這個方法就主要涉及到動畫的實現(xiàn)了低散,需要對FromViewController的view做動畫,來實現(xiàn)push的動畫效果骡楼,主要解釋已在注釋里面寫的很清楚了
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext
{_transitionContext = transitionContext;//首先記錄transitionContext熔号,下面的方法會用到
//我們主要通過這個transitionContext來獲取需要做動畫的fromViewController和toViewController,fromView和toView
//通過viewControllerForKey:這個方法來獲取,有兩個key來獲取相應(yīng)的Controller
// Currently only two keys are defined by the system:
// UITransitionContextToViewControllerKey 獲取要出現(xiàn)的Controller
// UITransitionContextFromViewControllerKey 獲取要消失的Controller
// Currently only two keys are defined by the system:
// UITransitionContextToViewKey //獲取要出現(xiàn)的view即上面toViewController的View
// UITransitionContextFromViewKey //獲取要出現(xiàn)的view即fromViewController的View
UIView *fromView = [transitionContext viewForKey:UITransitionContextFromViewKey];
UIView *toView = [transitionContext viewForKey:UITransitionContextToViewKey];
//拿到ContainerView
UIView *containerView = [transitionContext containerView];
//把fromView和toView都加到containerView中鸟整,在容器中是完成動畫
[containerView addSubview:toView];
[containerView addSubview:fromView];
//CATransform3D類型的四維仿射變換矩陣引镊,并且提供預(yù)置好的進(jìn)行旋轉(zhuǎn)、變形之后的仿射變換矩陣篮条,m34來控制透視效果,有興趣的話可以在自己了解一下
CATransform3D transform = CATransform3DIdentity;
transform.m34 = -1/500.0;
fromView.layer.transform = transform;
fromView.layer.anchorPoint = CGPointMake(0, 0.5);
fromView.layer.position = CGPointMake(0, CGRectGetMidY(fromView.frame));
//動畫變化
[UIView animateWithDuration:2 animations:^{
fromView.layer.transform = CATransform3DMakeRotation(-M_PI_2, 0, 1, 0);
} completion:^(BOOL finished) {
// This must be called whenever a transition completes (or is cancelled.)
// Typically this is called by the object conforming to the
// UIViewControllerAnimatedTransitioning protocol that was vended by the transitioning
// delegate. For purely interactive transitions it should be called by the
// interaction controller. This method effectively updates internal view
// controller state at the end of the transition.
//動畫完成后必須調(diào)用
[_transitionContext finishInteractiveTransition];
[_transitionContext completeTransition:YES];
}];
}
Pop的話弟头,其實跟Push差不多這里就只貼代碼不做過多解釋了
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext{
return 2;
}
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext{
_transitionContext = transitionContext;
UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
[fromViewController.view.layer removeAllAnimations];
UIView *containerView = [transitionContext containerView];
[containerView addSubview:fromViewController.view];
[containerView addSubview:toViewController.view];
CATransform3D transform = CATransform3DIdentity;
transform.m34 = -1/500.0;
toViewController.view.layer.transform = transform;
toViewController.view.layer.anchorPoint = CGPointMake(0, 0.5);
toViewController.view.layer.position = CGPointMake(0, CGRectGetMidY(fromViewController.view.frame));
toViewController.view.layer.transform = CATransform3DMakeRotation(-M_PI_2, 0, 1, 0);
[UIView animateWithDuration:2 animations:^{
toViewController.view.layer.transform = CATransform3DIdentity;
} completion:^(BOOL finished) {
[_transitionContext finishInteractiveTransition];
[_transitionContext completeTransition:YES];
}];
}
下面我們來介紹一下present和dismiss
這里先簡單提一下代理,OC中的代理兑燥,實在是十分好用亮瓷,為了將我們的代碼耦合性降低。我們可以單獨(dú)生成一個類降瞳,來實現(xiàn)動畫嘱支,這樣的話可以精簡Controller里面的代碼,另外是代碼的復(fù)用性進(jìn)一步增加挣饥。動畫文件可以單獨(dú)放在任何地方應(yīng)用除师。
下面我們來實現(xiàn).h
//首先遵循以下兩個協(xié)議,要實現(xiàn)自定義轉(zhuǎn)場必須遵守的
@interface ModalTransition : NSObject <UIViewControllerTransitioningDelegate,UIViewControllerAnimatedTransitioning>
@property (nonatomic, assign) BOOL isPresent;//記錄是Present還是Dimiss
@end
.m實現(xiàn)扔枫,這里我只是很簡單的寫了一個彈簧動畫汛聚,對于動畫方式大家可以自由發(fā)揮,寫出更加炫酷的效果
@implementation ModalTransition
//下面兩個代理方法要返回一個遵循UIViewControllerAnimatedTransitioning協(xié)議的對象短荐,返回self即可倚舀。記錄一下是Present還是Dimiss。主要講解我還放在注釋里面
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{
_isPresent = YES;
return self;
}
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed{
_isPresent = NO;
return self;
}
//返回動畫時間
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext{
return 1.0;
}
//根據(jù)_isPresent來執(zhí)行不同的動畫
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext{
_isPresent ? [self animationForPresentedView:transitionContext] : [self animationForDismissView:transitionContext];
}
//自定義方法忍宋,Present的動畫實現(xiàn)
-(void)animationForPresentedView:(id<UIViewControllerContextTransitioning>)transitionContext{
UIView *presentedView = [transitionContext viewForKey:UITransitionContextToViewKey];
UIView *fromView = [transitionContext viewForKey:UITransitionContextFromViewKey];
//注意添加的次序
[[transitionContext containerView] addSubview:fromView];
[[transitionContext containerView] addSubview:presentedView];
presentedView.transform = CGAffineTransformTranslate(presentedView.transform, 0, 667);
[UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0 usingSpringWithDamping:0.6 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
presentedView.transform = CGAffineTransformIdentity;
} completion:^(BOOL finished) {
//完成后記得調(diào)用completeTransition:方法
[transitionContext completeTransition:YES];
}];
}
//自定義方法痕貌,Dismiss的動畫實現(xiàn)
-(void)animationForDismissView:(id<UIViewControllerContextTransitioning>)transitionContext{
UIView *dismissedView = [transitionContext viewForKey:UITransitionContextFromViewKey];
UIView *toView = [transitionContext viewForKey:UITransitionContextToViewKey];
[[transitionContext containerView] addSubview:toView];
//注意添加的次序
[[transitionContext containerView] addSubview:dismissedView];
[UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
dismissedView.transform = CGAffineTransformTranslate(dismissedView.transform, 0, 667);
} completion:^(BOOL finished) {
//完成后記得調(diào)用completeTransition:方法
[transitionContext completeTransition:YES];
}];
}
@end
使用方法也標(biāo)簡單
PresentViewController *presentController = [[PresentViewController alloc]init];
presentController.modalTransitionStyle = UIModalPresentationCustom;
presentController.transitioningDelegate = _Transition;//_Transition即為上面冬瓜文件生成的對象,這樣就可以使用自定義的present動畫
[self presentViewController:presentController animated:YES completion:nil];
到這里基本就實現(xiàn)完成了糠排,不管是Pop&Push還是Present&Dismiss你都可以實現(xiàn)任何你想要的炫酷效果(當(dāng)然前提是你能把炫酷的動畫優(yōu)雅的實現(xiàn)出來)舵稠,我這里只是拋磚引玉,期待你有更完美入宦,漂亮的動畫效果哺徊。