iOS自定義轉(zhuǎn)場(chǎng)動(dòng)畫(4)——自定義模態(tài)跳轉(zhuǎn)之dismiss與手勢(shì)驅(qū)動(dòng)

Dismiss

效果:


接著使用上一個(gè)代碼
http://www.itdecent.cn/p/009f77cab231

1、新建PresentTransition繼承NSObject,并在.h中遵守UIViewControllerAnimatedTransitioning協(xié)議。

2、實(shí)現(xiàn)協(xié)議的兩個(gè)方法,并在其中編寫 Push 的動(dòng)畫。類似Present,只需要修改少量代碼:

// 返回動(dòng)畫的時(shí)間
- (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;
    
    // 設(shè)置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;
    
    // 動(dòng)畫
    [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]]; // 如果參數(shù)寫成yes,當(dāng)用戶取消pop時(shí),會(huì)繼續(xù)執(zhí)行動(dòng)畫,也就是讓detailVC消失,設(shè)置成這個(gè)參數(shù),會(huì)避免這樣的錯(cuò)誤
    }];
}

3、在ViewController.m中加入一個(gè)方法即可:

// dismiss動(dòng)畫
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed{
    return [[DismissTransition alloc] init];
}

加入手勢(shì)驅(qū)動(dòng)

  1. 想要同時(shí)實(shí)現(xiàn) present 和 dismiss 手勢(shì),就需要給兩個(gè) viewController.view 添加手勢(shì)。首先在 ViewController 中給自己添加一個(gè)屏幕右邊的手勢(shì),在init SecondViewController時(shí)給它的view添加一個(gè)屏幕左邊的手勢(shì),讓它們使用同一個(gè)手勢(shì)監(jiān)聽方法,都交給viewController處理,也就是self
    在ViewController中封裝了一個(gè)增加手勢(shì)的方法,參數(shù)為承載手勢(shì)的view和手勢(shì)
// 添加手勢(shì)的方法
-(void)addScreenEdgePanGestureRecognizer:(UIView *)view edges:(UIRectEdge)edges{
    UIScreenEdgePanGestureRecognizer * edgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(edgePanGesture:)]; // viewController和SecondViewController的手勢(shì)都由self管理
    edgePan.edges = edges;
    [view addGestureRecognizer:edgePan];
}
  1. 在viewDidLoad()和用于present的按鈕的點(diǎn)擊方法中分別加入手勢(shì):
-(void)viewDidLoad {
    [super viewDidLoad];
    
    self.transitioningDelegate = self;
    [self addScreenEdgePanGestureRecognizer:self.view edges:UIRectEdgeRight]; // 為self.view增加右側(cè)的手勢(shì),用于push
}
-(void)presentClick{
    SecondViewController * secondVC = [[SecondViewController alloc] init];
    secondVC.transitioningDelegate = self; // 必須second同樣設(shè)置delegate才有動(dòng)畫
    [self addScreenEdgePanGestureRecognizer:secondVC.view edges:UIRectEdgeLeft];
    [self presentViewController:secondVC animated:YES completion:^{
    }];
}
  1. 實(shí)現(xiàn)手勢(shì)的監(jiān)聽方法,首先定義一個(gè)屬性:
@property (nonatomic, retain) UIPercentDrivenInteractiveTransition * percentDrivenTransition;
  1. 因?yàn)橛袃蓚€(gè)手勢(shì),要區(qū)別他們使用的是 KeyWindow。手勢(shì)監(jiān)聽方法:
// 手勢(shì)的監(jiān)聽方法
-(void)edgePanGesture:(UIScreenEdgePanGestureRecognizer *)edgePan{
    CGFloat progress = fabs([edgePan translationInView:[UIApplication sharedApplication].keyWindow].x / [UIApplication sharedApplication].keyWindow.bounds.size.width);// 有兩個(gè)手勢(shì),所以這里計(jì)算百分比使用的是 KeyWindow
    
    if(edgePan.state == UIGestureRecognizerStateBegan){
        self.percentDrivenTransition = [[UIPercentDrivenInteractiveTransition alloc] init];
        if(edgePan.edges == UIRectEdgeRight){
            // present,避免重復(fù),直接調(diào)用點(diǎn)擊方法
            [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.最后實(shí)現(xiàn) UIViewControllerTransitioningDelegate 協(xié)議的另外兩個(gè)方法,分別返回 Present 和 Dismiss 動(dòng)畫的百分比。

// 百分比present
-(id<UIViewControllerInteractiveTransitioning>)interactionControllerForPresentation:(id<UIViewControllerAnimatedTransitioning>)animator{
    return _percentDrivenTransition;
}
// 百分比dismiss
-(id<UIViewControllerInteractiveTransitioning>)interactionControllerForDismissal:(id<UIViewControllerAnimatedTransitioning>)animator{
    return _percentDrivenTransition;
}

現(xiàn)在,關(guān)于Modal的自定義跳轉(zhuǎn)動(dòng)畫就完成了

點(diǎn)擊此處下載源碼

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容