zoukankan      html  css  js  c++  java
  • swift 动画

     

    // MARK:- 事件监听的函数

    extension HomeViewController {

        @objc private func titleBtnClick(titleBtn : TitleButton) {

            // 1.改变按钮的状态

            titleBtn.selected = !titleBtn.selected

            

            // 2.创建弹出的控制器

            let popoverVc = PopoverViewController()

            

            // 3.设置控制器的modal样式

            popoverVc.modalPresentationStyle = .Custom

            

            // 4.设置转场的代理

            popoverVc.transitioningDelegate = self

            

            // 弹出控制器

            presentViewController(popoverVc, animated: true, completion: nil)

        }

    }

     

     

    // MARK:- 自定义转场代理的方法

    extension HomeViewController : UIViewControllerTransitioningDelegate {

        // 目的:改变弹出View的尺寸

        func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? {

            return LZJPresentationController(presentedViewController: presented, presentingViewController: presenting)

        }

        

        // 目的:自定义弹出的动画

        func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {

            isPresented = true

            return self

        }

        

        // 目的:自定义消失的动画

        func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {

            isPresented = false

            return self

        }

    }

     

     

    // MARK:- 弹出和消失动画代理的方法

    extension HomeViewController : UIViewControllerAnimatedTransitioning {

        /// 动画执行的时间

        func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {

            return 0.5

        }

        

        /// 获取`转场的上下文`:可以通过转场上下文获取弹出的View和消失的View

        // UITransitionContextFromViewKey : 获取消失的View

        // UITransitionContextToViewKey : 获取弹出的View

        func animateTransition(transitionContext: UIViewControllerContextTransitioning) {

            isPresented ? animationForPresentedView(transitionContext) : animationForDismissedView(transitionContext)

        }

        

        /// 自定义弹出动画

        private func animationForPresentedView(transitionContext: UIViewControllerContextTransitioning) {

            // 1.获取弹出的View

            let presentedView = transitionContext.viewForKey(UITransitionContextToViewKey)!

            

            // 2.将弹出的View添加到containerView

            transitionContext.containerView()?.addSubview(presentedView)

            

            // 3.执行动画

            presentedView.transform = CGAffineTransformMakeScale(1.0, 0.0)

            presentedView.layer.anchorPoint = CGPointMake(0.5, 0)

            UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in

                presentedView.transform = CGAffineTransformIdentity

                }) { (_) -> Void in

                    // 必须告诉转场上下文你已经完成动画

                    transitionContext.completeTransition(true)

            }

        }

        

        /// 自定义消失动画

        private func animationForDismissedView(transitionContext: UIViewControllerContextTransitioning) {

            // 1.获取消失的View

            let dismissView = transitionContext.viewForKey(UITransitionContextFromViewKey)

            

            // 2.执行动画

            UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in

                dismissView?.transform = CGAffineTransformMakeScale(1.0, 0.00001)

                }) { (_) -> Void in

                    dismissView?.removeFromSuperview()

                    // 必须告诉转场上下文你已经完成动画

                    transitionContext.completeTransition(true)

            }

        }

    }

    -------------------------------------------------------------------------------------

     

     

    import UIKit

     

    class LZJPresentationController: UIPresentationController {

        // MARK:- 懒加载属性

        private lazy var coverView : UIView = UIView()

        

        // MARK:- 系统回调函数

        override func containerViewWillLayoutSubviews() {

            super.containerViewWillLayoutSubviews()

            

            // 1.设置弹出View的尺寸

            presentedView()?.frame = CGRect(x: 100, y: 55, 180, height: 250)

            

            // 2.添加蒙版

            setupCoverView()

        }

    }

     

    // MARK:- 设置UI界面相关

    extension LZJPresentationController {

        private func setupCoverView() {

            // 1.添加蒙版

            containerView?.insertSubview(coverView, atIndex: 0)

            

            // 2.设置蒙版的属性

            coverView.backgroundColor = UIColor(white: 0.8, alpha: 0.2)

            coverView.frame = containerView!.bounds

            

            // 3.添加手势

            let tapGes = UITapGestureRecognizer(target: self, action: "coverViewClick")

            coverView.addGestureRecognizer(tapGes)

        }

    }

     

    // MARK:- 事件监听

    extension LZJPresentationController {

        @objc private func coverViewClick() {

            presentedViewController.dismissViewControllerAnimated(true, completion: nil)

        }

    }

     

     

  • 相关阅读:
    基于Python的TCP阻塞式echo服务器
    Memcache仅仅支持简单数据类型
    Memcached(七)Memcached的并发实例
    Memcached(六)Memcached的并发实例
    Memcached(五)Memcached的并发实例
    Tcl/Tk的安装(转)
    centos6.5下安装Scipy工具包
    Mysql ERROR 1045 (28000): Access denied for user 'root'@'localhost'问题的解决
    mysql启动失败解决方案
    Linux更换python版本
  • 原文地址:https://www.cnblogs.com/liuzhenjie/p/5409345.html
Copyright © 2011-2022 走看看