Skip to content

Commit

Permalink
1.自定义转场实现push、pop
Browse files Browse the repository at this point in the history
  • Loading branch information
Caffe-del committed Sep 20, 2020
1 parent f0cf350 commit 9c63f24
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 21 deletions.
34 changes: 34 additions & 0 deletions BottomBar/BaseNavigationController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@


//
// BaseNavigationController.swift
// BottomBar
//
// Created by 全达晖 on 2020/9/20.
// Copyright © 2020 全达晖. All rights reserved.
//

import UIKit

class BaseNavigationController: UINavigationController, UINavigationControllerDelegate {

override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
}

// 非手势交互转场
func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationController.Operation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
if operation == .push {
return PushAnimatedTransitionAsFromBottomToTop.init()
} else if (operation == .pop) {
return PopAnimatedTransitioningAsFromTopToBottom.init()
}
return nil
}

// 手势交互转场
func navigationController(_ navigationController: UINavigationController, interactionControllerFor animationController: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? {
return nil
}
}
8 changes: 6 additions & 2 deletions BottomBar/MusicDetailVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@ class MusicDetailVC: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.init(red: 255/255, green: 228/255, blue: 181/255, alpha: 1)
self.view.backgroundColor = .systemGray
// Do any additional setup after loading the view.
}

override func viewWillAppear(_ animated: Bool) {
if let tabBarController = self.tabBarController as? RootTabBarController {
tabBarController.updateBottomStyle(.none)
tabBarController.updateBottomStyle(.miniPlayViewOnly)
}
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.navigationController?.popViewController(animated: true)
}
}
17 changes: 2 additions & 15 deletions BottomBar/MusicViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class MusicViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.init(red: 229/255, green: 225/255, blue: 238/255, alpha: 1)
self.navigationController?.delegate = self

let switchBtn = UISwitch.init(frame: CGRect.init(origin: self.view.center, size: CGSize.init(width: 60, height: 50)))
switchBtn.isOn = true
Expand All @@ -23,7 +22,9 @@ class MusicViewController: UIViewController {

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let vc = MusicDetailVC()
vc.modalPresentationStyle = .fullScreen
self.navigationController?.pushViewController(vc, animated: true)
// self.navigationController?.present(vc, animated: true, completion: nil)
}

override func viewWillAppear(_ animated: Bool) {
Expand All @@ -41,17 +42,3 @@ class MusicViewController: UIViewController {
}
}
}

extension MusicViewController: UINavigationControllerDelegate {
func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationController.Operation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return nil
}

func navigationController(_ navigationController: UINavigationController, interactionControllerFor animationController: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? {
return nil
}

func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {

}
}
40 changes: 40 additions & 0 deletions BottomBar/PopAnimatedTransitioningAsFromTopToBottom.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// PopAnimatedTransitioningAsFromTopToBottom.swift
// BottomBar
//
// Created by 全达晖 on 2020/9/20.
// Copyright © 2020 全达晖. All rights reserved.
//

import UIKit

class PopAnimatedTransitioningAsFromTopToBottom: NSObject, UIViewControllerAnimatedTransitioning {
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 0.35
}

func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
let toVC = transitionContext.viewController(forKey: .to)
let fromVC = transitionContext.viewController(forKey: .from)

let toView: UIView!
let fromView: UIView!

if transitionContext.responds(to: #selector(transitionContext.view(forKey:))) {
toView = transitionContext.view(forKey: .to)
fromView = transitionContext.view(forKey: .from)
} else {
fromView = fromVC?.view
toView = toVC?.view
}
transitionContext.containerView.insertSubview(toView, belowSubview: fromView)

fromView.frame = CGRect.init(x: 0, y: 0, width: screenWidth, height: screenHeight)

UIView.animate(withDuration: self.transitionDuration(using: transitionContext), delay: 0, options: .curveEaseInOut, animations: {
fromView.frame = CGRect.init(x: 0, y: screenHeight, width: screenWidth, height: screenHeight)
}) { (finish) in
transitionContext.completeTransition(true)
}
}
}
38 changes: 38 additions & 0 deletions BottomBar/PushAnimatedTransitionAsFromBottomToTop.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// PushAnimatedTransitionAsFromBottomToTop.swift
// BottomBar
//
// Created by 全达晖 on 2020/9/20.
// Copyright © 2020 全达晖. All rights reserved.
//

import UIKit

class PushAnimatedTransitionAsFromBottomToTop: NSObject, UIViewControllerAnimatedTransitioning {

func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 0.35
}

func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
let toVC = transitionContext.viewController(forKey: .to)
let toView: UIView!

if transitionContext.responds(to: #selector(transitionContext.view(forKey:))) {
toView = transitionContext.view(forKey: .to)
} else {
toView = toVC?.view
}

let containerView = transitionContext.containerView
containerView.addSubview(toView)

toView.frame = CGRect.init(x: 0, y: screenHeight, width: screenWidth, height: screenHeight)

UIView.animate(withDuration: self.transitionDuration(using: transitionContext), delay: 0, options: .curveEaseInOut, animations: {
toView.frame = CGRect.init(x: 0, y: 0, width: screenWidth, height: screenHeight)
}) { (finish) in
transitionContext.completeTransition(true)
}
}
}
8 changes: 4 additions & 4 deletions BottomBar/RootTabBarController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,16 @@ class RootTabBarController: UITabBarController {
self.setValue(rootTabBar, forKey: "tabBar")

let musicVC = MusicViewController()
let musicNav = UINavigationController.init(rootViewController: musicVC)
let musicNav = BaseNavigationController.init(rootViewController: musicVC)

let videoVC = VideoViewController()
let videoNav = UINavigationController.init(rootViewController: videoVC)
let videoNav = BaseNavigationController.init(rootViewController: videoVC)

let BBSVC = BBSViewController()
let BBSNav = UINavigationController.init(rootViewController: BBSVC)
let BBSNav = BaseNavigationController.init(rootViewController: BBSVC)

let accountVC = AccountViewController()
let accountNav = UINavigationController.init(rootViewController: accountVC)
let accountNav = BaseNavigationController.init(rootViewController: accountVC)

self.viewControllers = [musicNav, videoNav, BBSNav, accountNav];

Expand Down

0 comments on commit 9c63f24

Please sign in to comment.