Skip to content

Commit

Permalink
Merge pull request #10 from danshevluk/readme-fix
Browse files Browse the repository at this point in the history
Codestyle and Readme fix
  • Loading branch information
EnderTan authored Mar 24, 2017
2 parents 9d14978 + 86bfd3f commit 98c7e3a
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 92 deletions.
116 changes: 55 additions & 61 deletions ETNavBarTransparent/ETNavBarTransparent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,25 @@
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)
}
}

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()
}
}
Expand All @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
})
}
}
}
}
Expand All @@ -225,7 +219,7 @@ extension UIViewController {

objc_setAssociatedObject(self, &AssociatedKeys.navBarBgAlpha, alpha, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)

//Update UI
// Update UI
navigationController?.setNeedsNavigationBackground(alpha: alpha)
}
}
Expand Down
65 changes: 34 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,52 +1,55 @@
# 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
   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 = UIColor.white
}

```swift
// 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
}
```

}
```

##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)

0 comments on commit 98c7e3a

Please sign in to comment.