From 2cab50f6d2f7f59100ebd41707e3b74b9c304fbe Mon Sep 17 00:00:00 2001 From: Dan Shevlyuk Date: Fri, 24 Mar 2017 01:17:48 +0300 Subject: [PATCH 1/3] Clean-up codestyle, edit variable names and fix inceToken access --- ETNavBarTransparent/ETNavBarTransparent.swift | 116 +++++++++--------- 1 file changed, 55 insertions(+), 61 deletions(-) diff --git a/ETNavBarTransparent/ETNavBarTransparent.swift b/ETNavBarTransparent/ETNavBarTransparent.swift index b315be2..a01d506 100644 --- a/ETNavBarTransparent/ETNavBarTransparent.swift +++ b/ETNavBarTransparent/ETNavBarTransparent.swift @@ -9,7 +9,7 @@ import UIKit extension UIColor { - //System default bar tint color + // System default bar tint color open class var defaultNavBarTintColor: UIColor { return UIColor(red: 0, green: 0.478431, blue: 1, alpha: 1.0) } @@ -17,17 +17,17 @@ extension UIColor { extension DispatchQueue { - private static var _onceTracker = [String]() + private static var onceTracker = [String]() public class func once(token: String, block: () -> Void) { objc_sync_enter(self) defer { objc_sync_exit(self) } - if _onceTracker.contains(token) { + if onceTracker.contains(token) { return } - _onceTracker.append(token) + onceTracker.append(token) block() } } @@ -38,70 +38,66 @@ extension UINavigationController { return topViewController?.preferredStatusBarStyle ?? .default } - static let _onceToken = UUID().uuidString - - open override class func initialize(){ - - if self == UINavigationController.self { - - DispatchQueue.once(token: _onceToken) { - let needSwizzleSelectorArr = [ - NSSelectorFromString("_updateInteractiveTransition:"), - #selector(popToViewController), - #selector(popToRootViewController) - ] - - for selector in needSwizzleSelectorArr { - - let str = ("et_" + selector.description).replacingOccurrences(of: "__", with: "_") - /* - popToRootViewControllerAnimated: et_popToRootViewControllerAnimated:*/ - - let originalMethod = class_getInstanceMethod(self, selector) - let swizzledMethod = class_getInstanceMethod(self, Selector(str)) - method_exchangeImplementations(originalMethod, swizzledMethod) - } - } + private static let onceToken = UUID().uuidString + + open override class func initialize() { + guard self == UINavigationController.self else { return } + + DispatchQueue.once(token: onceToken) { + let needSwizzleSelectorArr = [ + NSSelectorFromString("_updateInteractiveTransition:"), + #selector(popToViewController), + #selector(popToRootViewController) + ] + + for selector in needSwizzleSelectorArr { + + let str = ("et_" + selector.description).replacingOccurrences(of: "__", with: "_") + // popToRootViewControllerAnimated: et_popToRootViewControllerAnimated: + + let originalMethod = class_getInstanceMethod(self, selector) + let swizzledMethod = class_getInstanceMethod(self, Selector(str)) + method_exchangeImplementations(originalMethod, swizzledMethod) } + } } - + func et_updateInteractiveTransition(_ percentComplete: CGFloat) { - - guard let topVC = topViewController, let coor = topVC.transitionCoordinator else { + guard let topViewController = topViewController, let coordinator = topViewController.transitionCoordinator else { et_updateInteractiveTransition(percentComplete) return } - let fromVC = coor.viewController(forKey: .from) - let toVC = coor.viewController(forKey: .to) + let fromViewController = coordinator.viewController(forKey: .from) + let toViewController = coordinator.viewController(forKey: .to) - //Bg Alpha - let fromAlpha = fromVC?.navBarBgAlpha ?? 0 - let toAlpha = toVC?.navBarBgAlpha ?? 0 - let nowAlpha = fromAlpha + (toAlpha - fromAlpha) * percentComplete + // Bg Alpha + let fromAlpha = fromViewController?.navBarBgAlpha ?? 0 + let toAlpha = toViewController?.navBarBgAlpha ?? 0 + let newAlpha = fromAlpha + (toAlpha - fromAlpha) * percentComplete - setNeedsNavigationBackground(alpha: nowAlpha) + setNeedsNavigationBackground(alpha: newAlpha) - //tintColor - let fromColor = fromVC?.navBarTintColor ?? .blue - let toColor = toVC?.navBarTintColor ?? .blue - let nowColor = averageColor(fromColor: fromColor, toColor: toColor, percent: percentComplete) - navigationBar.tintColor = nowColor + // Tint Color + let fromColor = fromViewController?.navBarTintColor ?? .blue + let toColor = toViewController?.navBarTintColor ?? .blue + let newColor = averageColor(fromColor: fromColor, toColor: toColor, percent: percentComplete) + navigationBar.tintColor = newColor et_updateInteractiveTransition(percentComplete) } - //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 + // Calculate the middle Color with translation percent + private func averageColor(fromColor: UIColor, toColor: UIColor, percent: CGFloat) -> UIColor { + var fromRed: CGFloat = 0 + var fromGreen: CGFloat = 0 + var fromBlue: CGFloat = 0 + var fromAlpha: CGFloat = 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 + var toRed: CGFloat = 0 + var toGreen: CGFloat = 0 + var toBlue: CGFloat = 0 + var toAlpha: CGFloat = 0 toColor.getRed(&toRed, green: &toGreen, blue: &toBlue, alpha: &toAlpha) let nowRed = fromRed + (toRed - fromRed) * percent @@ -125,9 +121,7 @@ extension UINavigationController { } fileprivate func setNeedsNavigationBackground(alpha: CGFloat) { - let barBackgroundView = navigationBar.subviews[0] - let valueForKey = barBackgroundView.value(forKey:) if let shadowView = valueForKey("_shadowView") as? UIView { @@ -192,15 +186,15 @@ extension UINavigationController: UINavigationBarDelegate { } if context.isCancelled { - let cancellDuration: TimeInterval = context.transitionDuration * Double(context.percentComplete) - UIView.animate(withDuration: cancellDuration, animations: { + let cancelDuration: TimeInterval = context.transitionDuration * Double(context.percentComplete) + UIView.animate(withDuration: cancelDuration) { animations(.from) - }) + } } else { let finishDuration: TimeInterval = context.transitionDuration * Double(1 - context.percentComplete) - UIView.animate(withDuration: finishDuration, animations: { + UIView.animate(withDuration: finishDuration) { animations(.to) - }) + } } } } @@ -225,7 +219,7 @@ extension UIViewController { objc_setAssociatedObject(self, &AssociatedKeys.navBarBgAlpha, alpha, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) - //Update UI + // Update UI navigationController?.setNeedsNavigationBackground(alpha: alpha) } } From b9914a329cfbad293d85c8ca7b6d08be5cf5d44d Mon Sep 17 00:00:00 2001 From: Dan Shevlyuk Date: Fri, 24 Mar 2017 01:30:35 +0300 Subject: [PATCH 2/3] Fix markdown syntax --- README.md | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 88a4044..dee44bb 100644 --- a/README.md +++ b/README.md @@ -1,25 +1,29 @@ # ETNavBarTransparent Change NavigationBar's transparency at pop gestrue and other situation -###Screenshots + +## Animation ![image](https://github.com/EnderTan/ETNavBarTransparentDemo/blob/master/navDemo.gif) -##Installation +## Installation Add the following line to your Podfile: -``` +```ruby pod 'ETNavBarTransparent' ``` + Then, run the following command: -``` +```bash $ pod install ``` -Or,Simply drag "ET_NavBarTransparent.swift" to your project +Or, simply drag `ET_NavBarTransparent.swift` to your project. + +## Usage -##Usage Change NavigationBar's transparency and tintColor where you want: -``` -    //Example: -    //Change in viewDidLoad + +```swift +    // Example: +    // Change in viewDidLoad    override func viewDidLoad() { super.viewDidLoad() @@ -27,26 +31,26 @@ Change NavigationBar's transparency and tintColor where you want: self.navBarTintColor = .white } -    //Change in scrollView scroll +    // Change in scrollView scroll    func scrollViewDidScroll(_ scrollView: UIScrollView) { if scrollView.contentOffset.y > 100 {            navBarBgAlpha = 1            navBarTintColor = UIColor.defaultNavBarTintColor() -        }else{ +        } else { navBarBgAlpha = 0 - navBarTintColor = UIColor.white + navBarTintColor = .white } } ``` -##Related articles +## Related articles [导航栏的平滑显示和隐藏 - 个人页的自我修养(1)](http://www.jianshu.com/p/454b06590cf1) -##License +## License MIT license. See LICENSE for details. -##Contact +## Contact Follow and contact me on Weibo [@日光镇](http://weibo.com/endertan) or [My Blog](http://www.jianshu.com/u/a958e552973b) From 86bfd3fe62c1b2dc1d7c8511214e4723a66021b9 Mon Sep 17 00:00:00 2001 From: Dan Shevlyuk Date: Fri, 24 Mar 2017 01:33:28 +0300 Subject: [PATCH 3/3] Change indent in Swift example --- README.md | 41 ++++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index dee44bb..91594ab 100644 --- a/README.md +++ b/README.md @@ -22,29 +22,28 @@ Or, simply drag `ET_NavBarTransparent.swift` to your project. Change NavigationBar's transparency and tintColor where you want: ```swift -    // Example: -    // Change in viewDidLoad -    override func viewDidLoad() { - super.viewDidLoad() - - self.navBarBgAlpha = 0 - self.navBarTintColor = .white +// Example: +// Change in viewDidLoad +override func viewDidLoad() { + super.viewDidLoad() + + self.navBarBgAlpha = 0 + self.navBarTintColor = .white +} + +// Change in scrollView scroll +func scrollViewDidScroll(_ scrollView: UIScrollView) { + + if scrollView.contentOffset.y > 100 { + navBarBgAlpha = 1 + navBarTintColor = UIColor.defaultNavBarTintColor() + } else { + navBarBgAlpha = 0 + navBarTintColor = .white } - -    // Change in scrollView scroll -    func scrollViewDidScroll(_ scrollView: UIScrollView) { - - if scrollView.contentOffset.y > 100 { -            navBarBgAlpha = 1 -            navBarTintColor = UIColor.defaultNavBarTintColor() -        } else { - navBarBgAlpha = 0 - navBarTintColor = .white - } - - } -``` +} +``` ## Related articles [导航栏的平滑显示和隐藏 - 个人页的自我修养(1)](http://www.jianshu.com/p/454b06590cf1)