iOS內(nèi)部提供了自定義轉(zhuǎn)場(chǎng)動(dòng)畫的代理
@property (nullable, nonatomic, weak) id <UIViewControllerTransitioningDelegate> transitioningDelegate;
我們可以在UIViewController直接寫轉(zhuǎn)場(chǎng)動(dòng)畫,也可以新建一個(gè)類寫轉(zhuǎn)場(chǎng)動(dòng)畫,首先我們要給要跳轉(zhuǎn)的UIViewController設(shè)置transitioningDelegate
- (instancetype)init ?{
? ? self?= [super?init];
? ? if?(self) ?{?
? ? ? ??self.transitioningDelegate = self;
????}
?? ?????return self; ?
}
然后實(shí)現(xiàn)幾個(gè)代理方法
- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
? ? return self;
}
- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
? ? return self;
}
- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext {
?? ?return?0.3;//這里設(shè)置動(dòng)畫時(shí)長(zhǎng)
}
//所有的過(guò)渡動(dòng)畫事務(wù)都在這個(gè)方法里面完成
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext{
? ? //取出轉(zhuǎn)場(chǎng)前后的視圖控制器
? ? UIViewController * fromVC = (UIViewController *)[transitionContext ?viewControllerForKey:UITransitionContextFromViewControllerKey];
? ? UIViewController * toVC = (UIViewController *)[transitionContext ?viewControllerForKey:UITransitionContextToViewControllerKey];
? ? //取出轉(zhuǎn)場(chǎng)前后視圖控制器上的視圖view
? ? UIView* toView = [transitionContext viewForKey:UITransitionContextToViewKey]?:toVC.view;
? ? UIView* fromView = [transitionContext viewForKey:UITransitionContextFromViewKey]?:fromVC.view;
? ? //這里有個(gè)重要的概念containerView,containerView作為要present的view的控制view,要present的view的所有動(dòng)畫都是在containerView上執(zhí)行的,這里模仿一個(gè)類似京東的加入購(gòu)物車彈出選擇規(guī)格動(dòng)畫
? ? UIView*containerView = [transitionContext containerView];?
//這里判斷toView是否是要present的view,如果是,那么是在做present動(dòng)畫,如果不是,那么是在執(zhí)行dismiss動(dòng)畫
//這里要特別說(shuō)明,動(dòng)畫執(zhí)行完成時(shí),要調(diào)用?[transitionContext completeTransition:YES];這是在通知系統(tǒng)動(dòng)畫完成了,如果沒(méi)有這一句,那么相當(dāng)于轉(zhuǎn)場(chǎng)動(dòng)畫沒(méi)執(zhí)行完成,導(dǎo)致頁(yè)面無(wú)法點(diǎn)擊,如果在執(zhí)行了這一句代碼時(shí),,,繼續(xù)對(duì)頁(yè)面進(jìn)行動(dòng)畫,會(huì)導(dǎo)致動(dòng)畫失效
? ? if(toView == self.view) {
? ? ? ? toView.frame = containerView.bounds;
? ? ? ? [containerView addSubview:toView];
?? ? ? ?CATransform3D transform = CATransform3DIdentity;
? ? ? ? transform.m34 = -1/1000;
? ? ? ? transform = CATransform3DRotate(transform,M_PI/16,1,0,0);
? ? ? ? transform = CATransform3DTranslate(transform,0,0, -100);
? ? ? ? transform = CATransform3DScale(transform,0.96,0.96,1);
? ? ? ? [UIView animateWithDuration:0.5 animations:^{
? ? ? ? ? ? fromView.layer.transform= transform;
? ? ? ? }completion:^(BOOLfinished) {
? ? ? ? ? ? [transitionContext completeTransition:YES];
? ? ? ? }];
? ? }else{
? ? ? ? [UIView animateWithDuration:0.3 animations:^{
? ? ? ? ? ? toView.layer.transform = CATransform3DMakeScale(1, 1, 1);
? ? ? ? } completion:^(BOOL?finished) {
? ? ? ? ? ? [fromView removeFromSuperview];
? ? ? ? ? ? [transitionContext completeTransition:YES];
? ? ? ? }];
? ? }
}