bigbro

秘密基地,记录成长

秘密基地


hello, 我是王岩,人称大师兄(bigbro),一名iOS客户端开发小白,正在努力成为大神的路上

自定义ViewController转场动画

  • Step.1 创建UINavigationControllerDelegate的代理实现类
  • Step.2 实现func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationControllerOperation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning?,返回自定义动画对象
  • Step.3 创建实现UIViewControllerAnimatedTransitioning的Animator类
  • Step.4 实现UIViewControllerAnimatedTransitioning指定的协议方法
  • Step.5 在调用push方法前,指定UINavigationController的delegate为自定义的对象
//
//  WYCustomNavigationDelegate.swift
//  HXFundManager
//
//  Created by wangyan on 2018/3/29.
//  Copyright © 2018年 China Asset Management Co., Ltd. All rights reserved.
//

import Foundation

class WYCustomNavigationDelegate: NSObject, UINavigationControllerDelegate {
    
    func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationControllerOperation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        return WYTransitionAnimator()
    }
}

class WYTransitionAnimator: NSObject, UIViewControllerAnimatedTransitioning, CAAnimationDelegate {
    
    weak var transitionContext: UIViewControllerContextTransitioning?
    
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        
        return 0.5
    }
    
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        
        self.transitionContext = transitionContext
        
        let containerView = transitionContext.containerView
        if let toVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to) {
            
            containerView.addSubview(toVC.view)
            
            let frame = CGRect(x: 0, y: 0, width: 100, height: 100)
            let circleMaskPathInitial = UIBezierPath(ovalIn: frame)
            let extremePoint = CGPoint(x: frame.midX, y: toVC.view.bounds.height - frame.midY)
            let radius = sqrt((extremePoint.x * extremePoint.x) + (extremePoint.y * extremePoint.y))
            let circleMaskPathFinal = UIBezierPath(ovalIn: frame.insetBy(dx: -radius, dy: -radius))
            
            let maskLayer = CAShapeLayer()
            maskLayer.path = circleMaskPathFinal.cgPath
            toVC.view.layer.mask = maskLayer
            
            let maskLayerAnimation = CABasicAnimation(keyPath: "path")
            maskLayerAnimation.fromValue = circleMaskPathInitial.cgPath
            maskLayerAnimation.toValue = circleMaskPathFinal.cgPath
            maskLayerAnimation.duration = self.transitionDuration(using: transitionContext)
            maskLayerAnimation.delegate = self
            maskLayer.add(maskLayerAnimation, forKey: "path")
        }
        
    }
    
    func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
        
        self.transitionContext?.completeTransition(!self.transitionContext!.transitionWasCancelled)
        self.transitionContext?.viewController(forKey: UITransitionContextViewControllerKey.to)?.view.layer.mask = nil
    }
}
最近的文章

Tiercel源码学习——并行下载器的典型实现

总体框架 Manger是核心类,是功能接口类,提供对外和对内的API,负责任务的管理(新增、开始、暂停、取消、状态维护、并发控制、查询);session的管理(创建、reset、销毁) Cache负责缓存相关的工作,包括任务信息的缓存(1以pilist文件的形式保存执行中任务的元数据)、本地下载目录的维护 DownLoadTask是执行下载任务的实体,维护下载任务相关的描述信息,进度、下载速度、起止时间、下载URL SessionDelegate实现了URLSessionDataD...…

源码学习继续阅读
更早的文章

由TableView的reloadData和reloadSections想出去。。。

由于搬砖中偶遇使用reloadSections造成的显示异常的问题,发现了一下reloadSections的小猫腻~~ reloadData; Reloads the rows and sections of the table view.Call this method to reload all the data that is used to construct the table, including cells, section headers and foo...…

日常继续阅读