Dismiss
效果:
接著使用上一個代碼
http://www.lxweimin.com/p/009f77cab231
1、新建PresentTransition繼承NSObject,并在.h中遵守UIViewControllerAnimatedTransitioning協議。
2、實現協議的兩個方法,并在其中編寫 Push 的動畫。類似Present,只需要修改少量代碼:
// 返回動畫的時間
- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext{
return 0.8;
}
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext{
ViewController * fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
SecondViewController * toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIView * container = [transitionContext containerView];
[container addSubview:toVC.view];
[container bringSubviewToFront:fromVC.view];
// 改變m34
CATransform3D transfrom = CATransform3DIdentity;
transfrom.m34 = -0.002;
container.layer.sublayerTransform = transfrom;
// 設置archPoint和position
CGRect initalFrame = [transitionContext initialFrameForViewController:fromVC];
toVC.view.frame = initalFrame;
fromVC.view.frame = initalFrame;
fromVC.view.layer.anchorPoint = CGPointMake(0, 0.5);
fromVC.view.layer.position = CGPointMake(0, initalFrame.size.height / 2.0);
// 添加陰影效果
CAGradientLayer * shadowLayer = [[CAGradientLayer alloc] init];
shadowLayer.colors =@[
[UIColor colorWithWhite:0 alpha:1],
[UIColor colorWithWhite:0 alpha:0.5],
[UIColor colorWithWhite:1 alpha:0.5]
];
shadowLayer.startPoint = CGPointMake(0, 0.5);
shadowLayer.endPoint = CGPointMake(1, 0.5);
shadowLayer.frame = initalFrame;
UIView * shadow = [[UIView alloc] initWithFrame:initalFrame];
shadow.backgroundColor = [UIColor clearColor];
[shadow.layer addSublayer:shadowLayer];
[fromVC.view addSubview:shadow];
shadow.alpha = 0;
// 動畫
[UIView animateKeyframesWithDuration:[self transitionDuration:transitionContext] delay:0 options:2 animations:^{
fromVC.view.layer.transform = CATransform3DMakeRotation(-M_PI_2, 0, 1, 0);
shadow.alpha = 1.0;
} completion:^(BOOL finished) {
fromVC.view.layer.anchorPoint = CGPointMake(0.5, 0.5);
fromVC.view.layer.position = CGPointMake(CGRectGetMidX(initalFrame), CGRectGetMidY(initalFrame));
fromVC.view.layer.transform = CATransform3DIdentity;
[shadow removeFromSuperview];
[transitionContext completeTransition: ![transitionContext transitionWasCancelled]]; // 如果參數寫成yes,當用戶取消pop時,會繼續執行動畫,也就是讓detailVC消失,設置成這個參數,會避免這樣的錯誤
}];
}
3、在ViewController.m中加入一個方法即可:
// dismiss動畫
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed{
return [[DismissTransition alloc] init];
}
加入手勢驅動
- 想要同時實現 present 和 dismiss 手勢,就需要給兩個 viewController.view 添加手勢。首先在 ViewController 中給自己添加一個屏幕右邊的手勢,在init SecondViewController時給它的view添加一個屏幕左邊的手勢,讓它們使用同一個手勢監聽方法,都交給viewController處理,也就是self
在ViewController中封裝了一個增加手勢的方法,參數為承載手勢的view和手勢
// 添加手勢的方法
-(void)addScreenEdgePanGestureRecognizer:(UIView *)view edges:(UIRectEdge)edges{
UIScreenEdgePanGestureRecognizer * edgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(edgePanGesture:)]; // viewController和SecondViewController的手勢都由self管理
edgePan.edges = edges;
[view addGestureRecognizer:edgePan];
}
- 在viewDidLoad()和用于present的按鈕的點擊方法中分別加入手勢:
-(void)viewDidLoad {
[super viewDidLoad];
self.transitioningDelegate = self;
[self addScreenEdgePanGestureRecognizer:self.view edges:UIRectEdgeRight]; // 為self.view增加右側的手勢,用于push
}
-(void)presentClick{
SecondViewController * secondVC = [[SecondViewController alloc] init];
secondVC.transitioningDelegate = self; // 必須second同樣設置delegate才有動畫
[self addScreenEdgePanGestureRecognizer:secondVC.view edges:UIRectEdgeLeft];
[self presentViewController:secondVC animated:YES completion:^{
}];
}
- 實現手勢的監聽方法,首先定義一個屬性:
@property (nonatomic, retain) UIPercentDrivenInteractiveTransition * percentDrivenTransition;
- 因為有兩個手勢,要區別他們使用的是 KeyWindow。手勢監聽方法:
// 手勢的監聽方法
-(void)edgePanGesture:(UIScreenEdgePanGestureRecognizer *)edgePan{
CGFloat progress = fabs([edgePan translationInView:[UIApplication sharedApplication].keyWindow].x / [UIApplication sharedApplication].keyWindow.bounds.size.width);// 有兩個手勢,所以這里計算百分比使用的是 KeyWindow
if(edgePan.state == UIGestureRecognizerStateBegan){
self.percentDrivenTransition = [[UIPercentDrivenInteractiveTransition alloc] init];
if(edgePan.edges == UIRectEdgeRight){
// present,避免重復,直接調用點擊方法
[self presentClick];
}else if(edgePan.edges == UIRectEdgeLeft){
[self dismissViewControllerAnimated:YES completion:^{
}];
}
}else if(edgePan.state == UIGestureRecognizerStateChanged){
[self.percentDrivenTransition updateInteractiveTransition:progress];
}else if(edgePan.state == UIGestureRecognizerStateCancelled || edgePan.state == UIGestureRecognizerStateEnded){
if(progress > 0.5){
[_percentDrivenTransition finishInteractiveTransition];
}else{
[_percentDrivenTransition cancelInteractiveTransition];
}
_percentDrivenTransition = nil;
}
}
5.最后實現 UIViewControllerTransitioningDelegate 協議的另外兩個方法,分別返回 Present 和 Dismiss 動畫的百分比。
// 百分比present
-(id<UIViewControllerInteractiveTransitioning>)interactionControllerForPresentation:(id<UIViewControllerAnimatedTransitioning>)animator{
return _percentDrivenTransition;
}
// 百分比dismiss
-(id<UIViewControllerInteractiveTransitioning>)interactionControllerForDismissal:(id<UIViewControllerAnimatedTransitioning>)animator{
return _percentDrivenTransition;
}
現在,關于Modal的自定義跳轉動畫就完成了