diff --git a/ETNavBarTransparent/ETNavBarTransparent.swift b/ETNavBarTransparent/ETNavBarTransparent.swift index 424302d..25a592a 100644 --- a/ETNavBarTransparent/ETNavBarTransparent.swift +++ b/ETNavBarTransparent/ETNavBarTransparent.swift @@ -18,7 +18,7 @@ extension UIColor { extension DispatchQueue { private static var _onceTracker = [String]() - + public class func once(token: String, block: () -> Void) { objc_sync_enter(self) defer { objc_sync_exit(self) } @@ -233,7 +233,7 @@ extension UIViewController { navigationController?.setNeedsNavigationBackground(alpha: alpha) } } - + open var navBarTintColor: UIColor { get { guard let tintColor = objc_getAssociatedObject(self, &AssociatedKeys.navBarTintColor) as? UIColor else { diff --git a/ETNavBarTransparentDemo/ETNavBarTransparentDemo.xcodeproj/project.pbxproj b/ETNavBarTransparentDemo/ETNavBarTransparentDemo.xcodeproj/project.pbxproj index a53f5d6..f41f87f 100644 --- a/ETNavBarTransparentDemo/ETNavBarTransparentDemo.xcodeproj/project.pbxproj +++ b/ETNavBarTransparentDemo/ETNavBarTransparentDemo.xcodeproj/project.pbxproj @@ -13,7 +13,7 @@ 373664FC1E6E9BE000239537 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 373664FA1E6E9BE000239537 /* LaunchScreen.storyboard */; }; 3736650D1E6E9C8E00239537 /* MainViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3736650B1E6E9C8E00239537 /* MainViewController.swift */; }; 3736650E1E6E9C8E00239537 /* ProfileViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3736650C1E6E9C8E00239537 /* ProfileViewController.swift */; }; - 378A88981E7063C1008AE1EC /* ETNavBarTransparent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 378A88971E7063C1008AE1EC /* ETNavBarTransparent.swift */; }; + D81840871E7A32890081479E /* ETNavBarTransparent.swift in Sources */ = {isa = PBXBuildFile; fileRef = D81840861E7A32890081479E /* ETNavBarTransparent.swift */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -25,7 +25,7 @@ 373664FD1E6E9BE000239537 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 3736650B1E6E9C8E00239537 /* MainViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MainViewController.swift; sourceTree = ""; }; 3736650C1E6E9C8E00239537 /* ProfileViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ProfileViewController.swift; sourceTree = ""; }; - 378A88971E7063C1008AE1EC /* ETNavBarTransparent.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ETNavBarTransparent.swift; sourceTree = ""; }; + D81840861E7A32890081479E /* ETNavBarTransparent.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ETNavBarTransparent.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -58,7 +58,7 @@ 373664F01E6E9BE000239537 /* ETNavBarTransparentDemo */ = { isa = PBXGroup; children = ( - 378A88961E7063C1008AE1EC /* ETNavBarTransparent */, + D81840851E7A32890081479E /* ETNavBarTransparent */, 373664F11E6E9BE000239537 /* AppDelegate.swift */, 3736650B1E6E9C8E00239537 /* MainViewController.swift */, 3736650C1E6E9C8E00239537 /* ProfileViewController.swift */, @@ -70,12 +70,13 @@ path = ETNavBarTransparentDemo; sourceTree = ""; }; - 378A88961E7063C1008AE1EC /* ETNavBarTransparent */ = { + D81840851E7A32890081479E /* ETNavBarTransparent */ = { isa = PBXGroup; children = ( - 378A88971E7063C1008AE1EC /* ETNavBarTransparent.swift */, + D81840861E7A32890081479E /* ETNavBarTransparent.swift */, ); - path = ETNavBarTransparent; + name = ETNavBarTransparent; + path = ../../ETNavBarTransparent; sourceTree = ""; }; /* End PBXGroup section */ @@ -152,7 +153,7 @@ buildActionMask = 2147483647; files = ( 3736650D1E6E9C8E00239537 /* MainViewController.swift in Sources */, - 378A88981E7063C1008AE1EC /* ETNavBarTransparent.swift in Sources */, + D81840871E7A32890081479E /* ETNavBarTransparent.swift in Sources */, 3736650E1E6E9C8E00239537 /* ProfileViewController.swift in Sources */, 373664F21E6E9BE000239537 /* AppDelegate.swift in Sources */, ); diff --git a/ETNavBarTransparentDemo/ETNavBarTransparentDemo.xcodeproj/project.xcworkspace/xcuserdata/bojuhuang.xcuserdatad/UserInterfaceState.xcuserstate b/ETNavBarTransparentDemo/ETNavBarTransparentDemo.xcodeproj/project.xcworkspace/xcuserdata/bojuhuang.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..b9cd1fe Binary files /dev/null and b/ETNavBarTransparentDemo/ETNavBarTransparentDemo.xcodeproj/project.xcworkspace/xcuserdata/bojuhuang.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/ETNavBarTransparentDemo/ETNavBarTransparentDemo.xcodeproj/xcuserdata/bojuhuang.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist b/ETNavBarTransparentDemo/ETNavBarTransparentDemo.xcodeproj/xcuserdata/bojuhuang.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist new file mode 100644 index 0000000..fe2b454 --- /dev/null +++ b/ETNavBarTransparentDemo/ETNavBarTransparentDemo.xcodeproj/xcuserdata/bojuhuang.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist @@ -0,0 +1,5 @@ + + + diff --git a/ETNavBarTransparentDemo/ETNavBarTransparentDemo.xcodeproj/xcuserdata/bojuhuang.xcuserdatad/xcschemes/ETNavBarTransparentDemo.xcscheme b/ETNavBarTransparentDemo/ETNavBarTransparentDemo.xcodeproj/xcuserdata/bojuhuang.xcuserdatad/xcschemes/ETNavBarTransparentDemo.xcscheme new file mode 100644 index 0000000..c380336 --- /dev/null +++ b/ETNavBarTransparentDemo/ETNavBarTransparentDemo.xcodeproj/xcuserdata/bojuhuang.xcuserdatad/xcschemes/ETNavBarTransparentDemo.xcscheme @@ -0,0 +1,91 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/ETNavBarTransparentDemo/ETNavBarTransparentDemo.xcodeproj/xcuserdata/bojuhuang.xcuserdatad/xcschemes/xcschememanagement.plist b/ETNavBarTransparentDemo/ETNavBarTransparentDemo.xcodeproj/xcuserdata/bojuhuang.xcuserdatad/xcschemes/xcschememanagement.plist new file mode 100644 index 0000000..9d37c6e --- /dev/null +++ b/ETNavBarTransparentDemo/ETNavBarTransparentDemo.xcodeproj/xcuserdata/bojuhuang.xcuserdatad/xcschemes/xcschememanagement.plist @@ -0,0 +1,22 @@ + + + + + SchemeUserState + + ETNavBarTransparentDemo.xcscheme + + orderHint + 0 + + + SuppressBuildableAutocreation + + 373664ED1E6E9BE000239537 + + primary + + + + + diff --git a/ETNavBarTransparentDemo/ETNavBarTransparentDemo/ETNavBarTransparent/ETNavBarTransparent.swift b/ETNavBarTransparentDemo/ETNavBarTransparentDemo/ETNavBarTransparent/ETNavBarTransparent.swift deleted file mode 100644 index fc900b0..0000000 --- a/ETNavBarTransparentDemo/ETNavBarTransparentDemo/ETNavBarTransparent/ETNavBarTransparent.swift +++ /dev/null @@ -1,271 +0,0 @@ -// -// ETNavBarTransparent.swift -// ETNavBarTransparentDemo -// -// Created by Bing on 2017/3/1. -// Copyright © 2017年 tanyunbing. All rights reserved. -// - -import UIKit - -extension UIColor { - //System default bar tint color - open class func defaultNavBarTintColor() -> UIColor { - return UIColor(red: 0, green: 0.478431, blue: 1, alpha: 1.0) - } - -} - -extension UINavigationController { - - open override func viewDidLoad() { - super.viewDidLoad() - delegate = self - } - - override open var preferredStatusBarStyle: UIStatusBarStyle { - return topViewController?.preferredStatusBarStyle ?? .default - } - - open override class func initialize(){ - - if self == UINavigationController.self { - - let needSwizzleSelectorArr = [["ori":NSSelectorFromString("_updateInteractiveTransition:"),"swi":NSSelectorFromString("et_updateInteractiveTransition:")], - ["ori":#selector(popToViewController(_:animated:)),"swi":#selector(et_popToViewController(_:animated:))], - ["ori":#selector(popToRootViewController(animated:)),"swi":#selector(et_popToRootViewController(animated:))]] - - for needSwizzleSelector in needSwizzleSelectorArr { - let originalSelector = needSwizzleSelector["ori"] - let swizzledSelector = needSwizzleSelector["swi"] - let originalMethod = class_getInstanceMethod(self.classForCoder(), originalSelector) - let swizzledMethod = class_getInstanceMethod(self.classForCoder(), swizzledSelector) - method_exchangeImplementations(originalMethod, swizzledMethod) - } - - } - - } - - - func et_updateInteractiveTransition(_ percentComplete: CGFloat) { - et_updateInteractiveTransition(percentComplete) - if let topVC = self.topViewController { - if let coor = topVC.transitionCoordinator { - //Bg Alpha - let fromAlpha = coor.viewController(forKey: .from)?.navBarBgAlpha - let toAlpha = coor.viewController(forKey: .to)?.navBarBgAlpha - let nowAlpha = fromAlpha! + (toAlpha!-fromAlpha!)*percentComplete - - self.setNeedsNavigationBackground(alpha: nowAlpha) - - //tintColor - let fromColor = coor.viewController(forKey: .from)?.navBarTintColor - let toColor = coor.viewController(forKey: .to)?.navBarTintColor - let nowColor = averageColor(fromColor: fromColor!, toColor: toColor!, percent: percentComplete) - self.navigationBar.tintColor = nowColor - } - } - } - - //Calculate the middle Color with translation percent - private func averageColor(fromColor:UIColor, toColor:UIColor, percent:CGFloat) -> UIColor { - var fromRed :CGFloat = 0.0 - var fromGreen :CGFloat = 0.0 - var fromBlue :CGFloat = 0.0 - var fromAlpha :CGFloat = 0.0 - fromColor.getRed(&fromRed, green: &fromGreen, blue: &fromBlue, alpha: &fromAlpha) - - var toRed :CGFloat = 0.0 - var toGreen :CGFloat = 0.0 - var toBlue :CGFloat = 0.0 - var toAlpha :CGFloat = 0.0 - toColor.getRed(&toRed, green: &toGreen, blue: &toBlue, alpha: &toAlpha) - - let nowRed = fromRed + (toRed-fromRed)*percent - let nowGreen = fromGreen + (toGreen-fromGreen)*percent - let nowBlue = fromBlue + (toBlue-fromBlue)*percent - let nowAlpha = fromAlpha + (toAlpha-fromAlpha)*percent - - return UIColor(red: nowRed, green: nowGreen, blue: nowBlue, alpha: nowAlpha) - } - - - func et_popToViewController(_ viewController: UIViewController, animated: Bool) -> [UIViewController]? { - setNeedsNavigationBackground(alpha: (viewController.navBarBgAlpha)) - navigationBar.tintColor = viewController.navBarTintColor - return et_popToViewController(viewController, animated: animated) - } - - func et_popToRootViewController(animated: Bool) -> [UIViewController]? { - setNeedsNavigationBackground(alpha: (viewControllers.first?.navBarBgAlpha)!) - navigationBar.tintColor = viewControllers.first?.navBarTintColor - return et_popToRootViewController(animated: animated) - } - - - fileprivate func setNeedsNavigationBackground(alpha:CGFloat) { - - - let barBackgroundView = navigationBar.subviews[0] - if let shadowView = barBackgroundView.value(forKey: "_shadowView") as? UIView { - shadowView.alpha = alpha - } - - if navigationBar.isTranslucent { - if #available(iOS 10.0, *){ - if navigationBar.backgroundImage(for: .default) == nil { - if let backgroundEffectView = barBackgroundView.value(forKey: "_backgroundEffectView") as? UIView { - backgroundEffectView.alpha = alpha - return - } - } - - }else{ - if let adaptiveBackdrop = barBackgroundView.value(forKey: "_adaptiveBackdrop") as? UIView { - if let backdropEffectView = adaptiveBackdrop.value(forKey: "_backdropEffectView") as? UIView { - backdropEffectView.alpha = alpha - return - } - } - } - - - } - - barBackgroundView.alpha = alpha - - } - - -} - -extension UINavigationController:UINavigationControllerDelegate,UINavigationBarDelegate { - - public func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) { - if let topVC = navigationController.topViewController { - if let coor = topVC.transitionCoordinator { - if #available(iOS 10.0, *) { - coor.notifyWhenInteractionChanges({ (context) in - self.dealInteractionChanges(context) - }) - } else { - coor.notifyWhenInteractionEnds({ (context) in - self.dealInteractionChanges(context) - }) - - } - } - } - } - - private func dealInteractionChanges(_ context:UIViewControllerTransitionCoordinatorContext) { - if context.isCancelled { - let cancellDuration:TimeInterval = context.transitionDuration * Double( context.percentComplete) - UIView.animate(withDuration: cancellDuration, animations: { - - let nowAlpha = (context.viewController(forKey: .from)?.navBarBgAlpha)! - self.setNeedsNavigationBackground(alpha: nowAlpha) - - self.navigationBar.tintColor = context.viewController(forKey: .from)?.navBarTintColor - }) - }else{ - let finishDuration:TimeInterval = context.transitionDuration * Double(1 - context.percentComplete) - UIView.animate(withDuration: finishDuration, animations: { - let nowAlpha = (context.viewController(forKey: .to)?.navBarBgAlpha)! - self.setNeedsNavigationBackground(alpha: nowAlpha) - - self.navigationBar.tintColor = context.viewController(forKey: .to)?.navBarTintColor - }) - } - } - - - - public func navigationBar(_ navigationBar: UINavigationBar, shouldPop item: UINavigationItem) -> Bool { - if let topVC = topViewController { - if let coor = topVC.transitionCoordinator { - if coor.initiallyInteractive { - return true - } - } - } - - - var popToVC: UIViewController? - if viewControllers.count >= (navigationBar.items?.count)! { - popToVC = viewControllers[viewControllers.count-2] - }else{ - popToVC = viewControllers[viewControllers.count-1] - } - - if popToVC != nil { - _ = self.popToViewController(popToVC!, animated: true) - return true - } - - return false - } - - public func navigationBar(_ navigationBar: UINavigationBar, shouldPush item: UINavigationItem) -> Bool { - setNeedsNavigationBackground(alpha: (topViewController?.navBarBgAlpha)!) - navigationBar.tintColor = topViewController?.navBarTintColor - return true - } - -} - - - - -extension UIViewController { - - fileprivate struct AssociatedKeys { - static var navBarBgAlpha: CGFloat = 1.0 - static var navBarTintColor: UIColor = UIColor.defaultNavBarTintColor() - } - - open var navBarBgAlpha: CGFloat { - get { - let alpha = objc_getAssociatedObject(self, &AssociatedKeys.navBarBgAlpha) as? CGFloat - if alpha == nil { - return 1.0 - }else{ - return alpha! - } - - } - set { - var alpha = newValue - if alpha > 1 { - alpha = 1 - } - if alpha < 0 { - alpha = 0 - } - - objc_setAssociatedObject(self, &AssociatedKeys.navBarBgAlpha, alpha, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) - - //Update UI - navigationController?.setNeedsNavigationBackground(alpha: alpha) - } - } - - open var navBarTintColor: UIColor { - get { - let tintColor = objc_getAssociatedObject(self, &AssociatedKeys.navBarTintColor) as? UIColor - if tintColor == nil { - return UIColor.defaultNavBarTintColor() - }else{ - return tintColor! - } - - } - set { - navigationController?.navigationBar.tintColor = newValue - objc_setAssociatedObject(self, &AssociatedKeys.navBarTintColor, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) - } - } - - -} diff --git a/ETNavBarTransparentDemo/ETNavBarTransparentDemo/ProfileViewController.swift b/ETNavBarTransparentDemo/ETNavBarTransparentDemo/ProfileViewController.swift index 4e417d0..0b99490 100644 --- a/ETNavBarTransparentDemo/ETNavBarTransparentDemo/ProfileViewController.swift +++ b/ETNavBarTransparentDemo/ETNavBarTransparentDemo/ProfileViewController.swift @@ -87,7 +87,7 @@ extension ProfileViewController: UITableViewDelegate { } navBarBgAlpha = navAlpha if navAlpha > 0.8 { - navBarTintColor = UIColor.defaultNavBarTintColor() + navBarTintColor = UIColor.defaultNavBarTintColor statusBarShouldLight = false }else{