美文网首页Swift开发
自定义转场动画

自定义转场动画

作者: CN_HarrySun | 来源:发表于2018-09-07 14:48 被阅读1次
1. 自定义动画管理类
//
//  PresentTransitionAnimated.swift
//  Bitwin
//
//  Created by HarrySun on 2018/9/6.
//  Copyright © 2018年 HarrySun. All rights reserved.
//

import UIKit

/// present动画类
class PresentTransitionAnimated: NSObject, UIViewControllerAnimatedTransitioning {
    
    var duration = 0.2
    
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return duration
    }
    
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        
        // 得到容器视图
        let containerView = transitionContext.containerView
        // 目标视图
        guard let toViewController = transitionContext.viewController(forKey: .to) as? GuessBuyViewController else { return }
        let toView = toViewController.baseView
        containerView.addSubview(toView)
        
        toView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        toView.bgView.snp.updateConstraints({ (make) in
            make.left.right.equalToSuperview()
            make.bottom.equalToSuperview().offset(EXHelper.screenWidth * 1.32)
        })
        containerView.layoutIfNeeded()
        
        UIView.animate(withDuration: duration, animations: {
            toView.bgView.snp.remakeConstraints({ (make) in
                make.bottom.equalToSuperview()
                make.left.right.equalToSuperview()
            })
            containerView.layoutIfNeeded()
        }, completion: { (_) in
            transitionContext.completeTransition(true)
        })
    }
}

/// dismiss动画类
class DismissTransitionAnimated: NSObject, UIViewControllerAnimatedTransitioning {
    
    var duration = 0.2
    
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return duration
    }
    
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        
        // 得到容器视图
        let containerView = transitionContext.containerView
        // 目标视图
        guard let fromViewController = transitionContext.viewController(forKey: .from) as? GuessBuyViewController else { return }
        let fromView = fromViewController.baseView
        containerView.addSubview(fromView)
        
        UIView.animate(withDuration: duration, animations: {
            fromView.bgView.snp.remakeConstraints({ (make) in
                make.top.equalTo(EXHelper.screenHeight)
                make.left.right.equalToSuperview()
            })
            containerView.layoutIfNeeded()
        }, completion: { (_) in
            transitionContext.completeTransition(true)
        })
    }
}
2. VC中使用,需要设置代理
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        let vc = TargetViewController()
        vc.transitioningDelegate = self
        MainTabBarController.main?.present(vc, animated: true, completion: nil)
    }

3. 实现代理方法

extension TargetViewController: UIViewControllerTransitioningDelegate {
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return PresentTransitionAnimated()
    }

    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return DismissTransitionAnimated()
    }
}

相关文章

网友评论

    本文标题:自定义转场动画

    本文链接:https://www.haomeiwen.com/subject/gyjcgftx.html