swift自定义简单的转场动画

效果图:

swift自定义简单的转场动画_第1张图片
swift转场动画.gif

主要思路:遵循两个代理:转场代理和转场动画代理

  • 在转场代理中自定义转场类:在里面实现对添加的view的frame设置、蒙版设置
  • 转场动画代理中:分别从写消失的动画和出现的动画

注意点:1、设置modal的样式为.Custon,否则model出现界面后,原来界面不存在 2、当动画完成后记得通知转场动画上下文,动画完成了3、蒙版记得设置frame

swift自定义简单的转场动画_第2张图片
自定义转场类.png

代码

//
//  LYPresentationController.swift
//  Swift自定义转成动画练习
//
//  Created by liyang on 16/8/3.
//  Copyright © 2016年 liyang. All rights reserved.
//

import UIKit

class LYPresentationController: UIPresentationController {
    
    private lazy var coverView : UIView = UIView()
    
    // 所有的被modal出来的控制器都是被添加在一个containerView上的,通过重写这个方法,来改变modal出控制器frame
    override func containerViewWillLayoutSubviews() {
        // 1、设置frame
        presentedView()?.frame = CGRect(x: 50, y: 55, width: 180, height: 250)
        // 2、设置蒙版
        setupCoverView()
    }
}
// MARK: - 设置蒙版
extension LYPresentationController {
    private func setupCoverView() {
        // 1、添加蒙版
        containerView?.insertSubview(coverView, atIndex: 0)
        coverView.frame = containerView!.bounds
        // 2、给蒙版添加一个手势,当点击蒙版的时候,是控制器dismiss
        coverView.backgroundColor = UIColor(white: 0.8, alpha: 0.2)
        let tapGr = UITapGestureRecognizer(target: self, action: #selector(LYPresentationController.coverViewClick))
        coverView.addGestureRecognizer(tapGr)
    }
}

// MARK: - 控件的监听方法
extension LYPresentationController {
    @objc private func coverViewClick() {
        presentedViewController.dismissViewControllerAnimated(true, completion: nil)
    }
}
swift自定义简单的转场动画_第3张图片
主vc.png

代码

//
//  ViewController.swift
//  Swift自定义转成动画练习
//
//  Created by liyang on 16/8/3.
//  Copyright © 2016年 liyang. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    // MARK:- false是消失,true是弹出
    private lazy var isPresent : Bool = false
    
    override func touchesBegan(touches: Set, withEvent event: UIEvent?) {
        // 1、创建要被modal出的控制器Vc
        let modalVc = ModalViewController()
        // 2、设置转场的代理
        modalVc.transitioningDelegate = self
        // 3、设置modal样式
        modalVc.modalPresentationStyle = .Custom
        presentViewController(modalVc, animated: true, completion: nil)
    }
}
// MARK: - 遵循转场代理方法
extension ViewController : UIViewControllerTransitioningDelegate {
    // 目的改变要被modal出来的控制器的frame
    func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? {
        return LYPresentationController(presentedViewController: presented, presentingViewController: presenting)
    }
    
    // 目的:设置弹出动画
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresent = true
        return self
    }
    
    // 目的:设置消失动画
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresent = false
        return self
    }
}
// MARK: - 转场动画代理
extension ViewController : UIViewControllerAnimatedTransitioning {
    // 转场动画的时间
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return 0.5
    }
    // 转场动画的上下文Context
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        isPresent ? animateTransitionForPresent(transitionContext) : animateTransitionForDismiss(transitionContext)
    }
    
    // 弹出的动画
    func animateTransitionForPresent(transitionContext: UIViewControllerContextTransitioning) {
        // 1、获取弹出的view
        let presentView = transitionContext.viewForKey(UITransitionContextToViewKey)!
        // 2、将弹出的View添加到containerView上
        transitionContext.containerView()?.addSubview(presentView)
        // 3、执行动画
        presentView.transform = CGAffineTransformMakeScale(1.0, 0.0)
        presentView.layer.anchorPoint = CGPointMake(0.5, 0) // 默认这个点在center,我们希望他在上面靠中间的位置
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: { 
            // 动画执行完毕,还原形变
            presentView.transform = CGAffineTransformIdentity
            }) { (_) in
                // 告诉上下文,动画执行完毕
                transitionContext.completeTransition(true)
        }
    }
    
    // 消失的动画
    func animateTransitionForDismiss(transitionContext: UIViewControllerContextTransitioning) {
        // 1、获取消失的view
        let dismissView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
        // 2、执行动画
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: { 
            dismissView.transform = CGAffineTransformMakeScale(1.0, 0.001)
            }) { (_) in
                dismissView .removeFromSuperview()
                transitionContext.completeTransition(true)
        }
    }
}

demo链接

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