iOS自定义转场动画

ios8自定义转场动画

  • 首先只能在iOS 8后使用

    默认情况下modal会移除以前控制器的view, 替换为当前弹出的view
    如果自定义转场, 那么就不会移除以前控制器的view


// vc是要被model的控制器
let vc = UIViewController() 
// 设置转场代理为当前控制器
vc?.transitioningDelegate = self
        
// 设置转场的样式为自定义
vc?.modalPresentationStyle = UIModalPresentationStyle.custom
        
present(vc!, animated: true, completion: nil)

1.遵守UIViewControllerTransitioningDelegate协议,实现协议方法, 设置转场样式为自定义

@available(iOS 8.0, *)

optional public func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController?

2.要返回一个UIPresentationController,所以自顶一个类继承UIPresentationController


class CustomPopoverPresentationController: UIPresentationController {
    
    /**
    初始化方法, 用于创建负责转场动画的对象
    
    :param: presentedViewController  被展现的控制器
    :param: presentingViewController 发起的控制器, Xocde6是nil, Xcode7是野指针
    
    :returns: 负责转场动画的对象
    */
    override init(presentedViewController: UIViewController, presenting presentingViewController: UIViewController?) {
        super.init(presentedViewController: presentedViewController, presenting: presentingViewController)
    }
    
    /**
    即将布局转场子视图时调用
    */
    override func containerViewWillLayoutSubviews()
    {
    
        // presentedView 表示为弹出的视图,containerView 表示容器view
        // 1.修改弹出视图的大小
        presentedView?.frame = CGRect(x: 100, y: 56, width: 200, height: 200)
        
        // 2.在容器视图上添加一个蒙版, 插入到展现视图的下面
        containerView?.insertSubview(coverView, at: 0)
    }
    
    // MARK: - 懒加载
    fileprivate lazy var coverView: UIView = {
        // 1.创建view
       let view = UIView()
        view.backgroundColor = UIColor(white: 0.0, alpha: 0.2)
        view.frame = UIScreen.main.bounds
        
        // 2.添加监听
        let tap = UITapGestureRecognizer(target: self, action: "close")
        view.addGestureRecognizer(tap)
        return view
    }()
    
    func close(){
        // presentedViewController拿到当前弹出的控制器
        presentedViewController.dismiss(animated: true, completion: nil)
    }
}

3.返回的UIPresentationController就用自定义类创建

    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController?
    {
        return CustomPopoverPresentationController(presentedViewController: presented, presenting: presenting)
    }

4.实现协议中另外两个方法来自定义动画的实现

  • 只要实现了以下方法, 那么系统自带的默认动画就没有了, "所有"东西都需要程序员自己来实现

  • 自定义一个isPresent表示是否model出来了

@available(iOS 2.0, *) // 即将弹出
optional public func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning?

    
@available(iOS 2.0, *) // 即将消失
optional public func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning?



/**
告诉系统谁来负责Modal的展现动画
:param: presented  被展现视图
:param: presenting 发起的视图
:returns: 谁来负责
*/
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning?
{
    isPresent = true
    return self
}
    
/**
告诉系统谁来负责Modal的消失动画
:param: dismissed 被关闭的视图
:returns: 谁来负责
*/
func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning?
{
    isPresent = false
    return self
}

5.由于UIViewControllerAnimatedTransitioning是协议,而设置了当前控制器遵守协议,所以需要遵守协议实现方法,如下遵守UIViewControllerAnimatedTransitioning协议 ,实现方法

public func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval

// This method can only  be a nop if the transition is interactive and not a percentDriven interactive transition.
public func animateTransition(using transitionContext: UIViewControllerContextTransitioning)

// 实现方法
    // MARK: - UIViewControllerAnimatedTransitioning
    /**
    返回动画时长
    
    :param: transitionContext 上下文, 里面保存了动画需要的所有参数
    
    :returns: 动画时长
    */
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval
    {
        return 0.5
    }
    
    /**
    告诉系统如何动画, 无论是展现还是消失都会调用这个方法
    
    :param: transitionContext 上下文, 里面保存了动画需要的所有参数
    */
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning)
    {
        // 1.拿到展现视图
        /*
        let toVC = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)
        let fromVC = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)
        // 通过打印发现需要修改的就是toVC上面的View
        print(toVC)
        print(fromVC)
        */
        if isPresent
        {
            // 展开
            print("展开")
            let toView = transitionContext.view(forKey: UITransitionContextViewKey.to)!
            toView.transform = CGAffineTransform(scaleX: 1.0, y: 0.0);
            
            // 注意: 一定要将视图添加到容器上
            transitionContext.containerView.addSubview(toView)
            
            // 设置锚点
            toView.layer.anchorPoint = CGPoint(x: 0.5, y: 0)
            
            // 2.执行动画
            UIView.animate(withDuration: 0.5, animations: { () -> Void in
                // 2.1清空transform
                toView.transform = CGAffineTransform.identity
                }, completion: { (_) -> Void in
                    // 2.2动画执行完毕, 一定要告诉系统
                    // 如果不写, 可能导致一些未知错误
                    transitionContext.completeTransition(true)
            }) 
        }else
        {
            // 关闭
            print("关闭")
            let fromView = transitionContext.view(forKey: UITransitionContextViewKey.from)
            
            UIView.animate(withDuration: 0.2, animations: { () -> Void in
                // 注意:由于CGFloat是不准确的, 所以如果写0.0会没有动画
                // 压扁
                fromView?.transform = CGAffineTransform(scaleX: 1.0, y: 0.000001)
                }, completion: { (_) -> Void in
                    // 如果不写, 可能导致一些未知错误
                    transitionContext.completeTransition(true)
            })
        }
    }


你可能感兴趣的:(iOS自定义转场动画)