QAQ。看swift视频中有这个效果,自己项目中刚好要用到。就自己实现了一遍。先看效果:
A页面present出来B页面,A就是presenting页面,B就是presented页面。
自定义modal动画,需要遵守几个协议。
1.UIViewControllerTransitioningDelegate,modal转场协议。和之前的自定义导航动画类似。
2.UIViewControllerAnimatedTransitioning,转场动画协议,用来自定义动画。
还有交互的动画,用来自定义手势啥的,暂时没用上。
一、UIViewControllerTransitioningDelegate
主要实现3个代理方法
-(UIPresentationController*)presentationControllerForPresentedViewController:(UIViewController *)presented presentingViewController:(UIViewController *)presenting sourceViewController:(UIViewController *)source{
};
返回一个UIPresentationController控制器。相当于动画过程的容器。用来关联A和B两个控制器,用来自定义一些画面。比如我项目里就添加了一个黑色半透明的view,点击黑色部分也能dismiss。
//弹出
-(id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{
}
根据方法名字就能理解,这个是present动画的代理,需要返回动画对象。
这个动画对象在下面说明。
//消失
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed{
}
dismiss动画,和上个方法一样,需要返回动画对象。
二、UIViewControllerAnimatedTransitioning
需要实现2个代理方法
//动画的时间
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext{
}
//动画执行过程,根据自己需求写动画效果。一般uiview简单动画就能满足需求
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext{
}
主要也就这几个代理方法。
一、遵守动画和转场协议的对象
其实转场协议也可以直接写在控制起来里,控制器遵守协议,看起来能更好理解代码过程。根据自己需求处理。
动画协议也是,自定义一个Object对象遵守或者控制器遵守都行。为了代码的简洁,我把转场协议和动画协议写在一个文件里了。
具体代码如下
PS:我转场代理和动画代理写到一个文件了。
h文件
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface PresentAnimator : NSObject <UIViewControllerTransitioningDelegate, UIViewControllerAnimatedTransitioning>
//弹出页面的尺寸,由控制器管理,当然也可以写死。看需求了
@property (nonatomic, assign) CGRect presentFrame;
//present还是dismiss
@property (nonatomic, assign) BOOL isPresented;
@end
m文件,有点长
#import "PresentAnimator.h"
#import "CardListPresentC.h"
@implementation PresentAnimator
#pragma mark UIViewControllerTransitioningDelegate代理
- (UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented presentingViewController:(UIViewController *)presenting sourceViewController:(UIViewController *)source{
//自定义转场控制器,用来关联2个页面,直接返回就行
//ps:下面有这个文件的代码
CardListPresentC *presentedVC = [[CardListPresentC alloc] initWithPresentedViewController:presented presentingViewController:presenting];
presentedVC.presentFrame = self.presentFrame;
return presentedVC;
}
//弹出
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{
//present过程,返回了遵守了动画协议的对象,由于我转场协议和动画协议写在了一个文件,所以直接返回了self
self.isPresented = YES;
return self;
}
//消失
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed{
//同上
self.isPresented = NO;
return self;
}
#pragma mark 动画代理
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext{
return 0.5;
}
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext{
//动画过程,根据self.isPresented判断弹出还是消失,self.isPresented是由上面转场代理赋值
//弹出和消失都是这个方法,代码相似,就把transitionContext参数传递一下,分开写了。
if (self.isPresented) {
[self presentVC:transitionContext];
}else{
[self popVC:transitionContext];
}
}
//present 过程
- (void)presentVC:(id<UIViewControllerContextTransitioning>)transitionContext {
//根据transitionContext上下文,取得toView,即需要弹出页面的view
UIView *toView = [transitionContext viewForKey:UITransitionContextToViewKey];
[transitionContext.containerView addSubview:toView];
//进行缩放,高度变为0,就看不到了
toView.transform = CGAffineTransformMakeScale(1, 0);
//设置锚点,改变动画其实位置
toView.layer.anchorPoint = CGPointMake(0.5, 1);
//动画缩放,回复原样
[UIView animateWithDuration:0.5 animations:^{
toView.transform = CGAffineTransformMakeScale(1, 1);
} completion:^(BOOL finished) {
//动画结束,通知系统动画结束或者被什么打断。好像必须要写
[transitionContext completeTransition:!transitionContext.transitionWasCancelled];
}];
}
//dismiss过程
- (void)popVC:(id<UIViewControllerContextTransitioning>)transitionContext {
UIView *fromView = [transitionContext viewForKey:UITransitionContextFromViewKey];
[transitionContext.containerView addSubview:fromView];
fromView.layer.anchorPoint = CGPointMake(0.5, 1);
[UIView animateWithDuration:0.5 animations:^{
fromView.transform = CGAffineTransformMakeScale(1, 0.001);
} completion:^(BOOL finished) {
[fromView removeFromSuperview];
[transitionContext completeTransition:!transitionContext.transitionWasCancelled];
}];
}
@end
二、自定义转场控制器
#import "CardListPresentC.h"
#define SWidth [UIScreen mainScreen].bounds.size.width
#define SHeigth [UIScreen mainScreen].bounds.size.height
@interface CardListPresentC ()
@property (nonatomic, strong) UIView *blackBGView;
@end
@implementation CardListPresentC
- (void)containerViewWillLayoutSubviews{
[super containerViewWillLayoutSubviews];
//
self.presentedView.frame = self.presentFrame;
//添加了一个黑色半透明背景
self.blackBGView = [[UIView alloc] initWithFrame:self.containerView.bounds];
self.blackBGView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
[self.containerView insertSubview:self.blackBGView atIndex:0];
//添加一个手势,点击背景dismiss
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hiddenCardList)];
[self.blackBGView addGestureRecognizer:tap];
}
- (void)hiddenCardList{
//点击黑色背景执行dismiss
[self.presentedViewController dismissViewControllerAnimated:YES completion:nil];
}
三、控制器的调用
//确定了需要弹出页面的尺寸
CGRect presentFrame = CGRectMake(35, SCREENHEIGHT*0.64-listHeight, SCREENWIDTH-70, listHeight+10);
//需要弹出的控制器
BoxMCardListVC *listVC = [[BoxMCardListVC alloc] init];
listVC.dataArr = weakSelf.cardListArr;
listVC.presentFrame = presentFrame;
//声明是自定义动画
listVC.modalPresentationStyle = UIModalPresentationCustom;
//初始化转场对象
PresentAnimator *animator = [[PresentAnimator alloc] init];
animator.presentFrame = presentFrame;
//声明转场代理
listVC.transitioningDelegate = animator;
[weakSelf presentViewController:listVC animated:YES completion:nil];