• 【Swift学习笔记-《PRODUCT》读书记录-实现自定义转场动画】


    iOS默认的push动画是把即将展示的控制器从右边推过来。有时我们想实现类似PPT中的一些动画,这时候就需要自定义转场动画了。如下图我们想实现一个淡出并且放大的过场动画,在退出时是一个淡出缩小的动画。

    首先需要自定义一个类DiaryAnimator.swift遵守 UIViewControllerAnimatedTransitioning 协议,然后实现它,代码如下:

    import UIKit
    
    class DiaryAnimator: NSObject, UIViewControllerAnimatedTransitioning {
        // 用于接受外界的operation
        var operation:UINavigationControllerOperation!
        
        // 返回动画时间
        func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
            return 0.4
        }
        
        // 要设置的动画
        //UIKit calls this method when presenting or dismissing a view controller. Use this method to configure the animations associated with your custom transition. You can use view-based animations or Core Animation to configure your animations.
        
    
        func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
            
            let containerview = transitionContext.containerView
            
            let fromVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from)
            let fromView = fromVC!.view
            
            let toVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)
            let toView = toVC?.view
            
            // 设置新场景透明度
            toView?.alpha = 0.0
            
            // 设置初始值
            if operation == UINavigationControllerOperation.pop {
                fromView?.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
            }else{
                toView?.transform = CGAffineTransform(scaleX: 0.3, y: 0.3)
            }
            
            containerview.insertSubview(toView!, aboveSubview: fromView!)
            
            UIView.animate(withDuration: transitionDuration(using: transitionContext), delay: 0, options: UIViewAnimationOptions.curveEaseInOut, animations: {
                if self.operation ==  UINavigationControllerOperation.pop {
                    // 放大要转出的场景
                    fromView?.transform = CGAffineTransform(scaleX: 3.3,y: 3.3)
                } else {
                    // 设置新场景为原始大小
                    toView?.transform = CGAffineTransform(scaleX: 1.0,y: 1.0)
                }
                
                toView?.alpha = 1.0
            },completion: { finished in
                transitionContext.completeTransition(true)
            })
        }
        
    }
    

     自定义好动画后,在要使用的控制器中实现 UINavigationControllerDelegate 协议的 optional func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationControllerOperation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? 方法,返回自定义的动画即可

    extension HomeYearController : UINavigationControllerDelegate
    {
        func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationControllerOperation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
            let animate = DiaryAnimator()
            animate.operation = operation
           return animate
        }
    }
    



  • 相关阅读:
    根据EsLint配置WebStorm格式化代码风格
    Vue中使用vant-UI实现移动端自定义省市区三级联动
    Vue中使用Element-UI实现表格跨页多选
    Vue中使用iview-UI实现切换Tab页网络请求优化
    Vue中使用iview-UI按需引入Select组件下拉框无法生效问题
    Vue中使用iview-UI表格样式修改和使用自定义模板数据渲染相关
    Vue中使用Element-UI表单验证相关问题及解决
    Vue 3.0 多页面项目之商家平台练习
    五 创建道路模型(2 道路的挖填方量计算及条件部件)
    五 创建道路模型(1 道路三要素)
  • 原文地址:https://www.cnblogs.com/heyode/p/6513581.html
Copyright © 2020-2023  润新知