You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
private static var onceTracker = [String]()
//Executes a block of code, associated with a unique token, only once. The code is thread safe and will only execute the code once even in the presence of multithreaded calls.
public class func once(token: String, block: () -> Void)
{ // 保证被 objc_sync_enter 和 objc_sync_exit 包裹的代码可以有序同步地执行
objc_sync_enter(self)
defer { // 作用域结束后执行defer中的代码
objc_sync_exit(self)
}
if onceTracker.contains(token) {
return
}
onceTracker.append(token)
block()
}
}
//===========================================================================================
// MARK: - default navigationBar barTintColor、tintColor and statusBarStyle YOU CAN CHANGE!!!
//===========================================================================================
class WRNavigationBar
{
fileprivate struct AssociatedKeys
{ // default is system attributes
static var defNavBarBarTintColor: UIColor = UIColor.white
static var defNavBarBackgroundImage: UIImage = UIImage()
static var defNavBarTintColor: UIColor = UIColor(red: 0, green: 0.478431, blue: 1, alpha: 1.0)
static var defNavBarTitleColor: UIColor = UIColor.black
static var defStatusBarStyle: UIStatusBarStyle = UIStatusBarStyle.default
static var defShadowImageHidden: Bool = false
}
class var defaultNavBarBarTintColor: UIColor {
get {
guard let def = objc_getAssociatedObject(self, &AssociatedKeys.defNavBarBarTintColor) as? UIColor else {
return AssociatedKeys.defNavBarBarTintColor
}
return def
}
set {
objc_setAssociatedObject(self, &AssociatedKeys.defNavBarBarTintColor, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}
class var defaultNavBarBackgroundImage: UIImage? {
get {
guard let def = objc_getAssociatedObject(self, &AssociatedKeys.defNavBarBackgroundImage) as? UIImage else {
return nil
}
return def
}
set {
objc_setAssociatedObject(self, &AssociatedKeys.defNavBarBackgroundImage, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}
class var defaultNavBarTintColor: UIColor {
get {
guard let def = objc_getAssociatedObject(self, &AssociatedKeys.defNavBarTintColor) as? UIColor else {
return AssociatedKeys.defNavBarTintColor
}
return def
}
set {
objc_setAssociatedObject(self, &AssociatedKeys.defNavBarTintColor, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}
class var defaultNavBarTitleColor: UIColor {
get {
guard let def = objc_getAssociatedObject(self, &AssociatedKeys.defNavBarTitleColor) as? UIColor else {
return AssociatedKeys.defNavBarTitleColor
}
return def
}
set {
objc_setAssociatedObject(self, &AssociatedKeys.defNavBarTitleColor, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}
class var defaultStatusBarStyle: UIStatusBarStyle {
get {
guard let def = objc_getAssociatedObject(self, &AssociatedKeys.defStatusBarStyle) as? UIStatusBarStyle else {
return .default
}
return def
}
set {
objc_setAssociatedObject(self, &AssociatedKeys.defStatusBarStyle, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}
class var defaultShadowImageHidden: Bool {
get {
guard let def = objc_getAssociatedObject(self, &AssociatedKeys.defShadowImageHidden) as? Bool else {
return false
}
return def
}
set {
objc_setAssociatedObject(self, &AssociatedKeys.defShadowImageHidden, newValue, .OBJC_ASSOCIATION_ASSIGN)
}
}
class var defaultBackgroundAlpha: CGFloat {
get {
return 1.0
}
}
// Calculate the middle Color with translation percent
class fileprivate func middleColor(fromColor: UIColor, toColor: UIColor, percent: CGFloat) -> UIColor
{
// get current color RGBA
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)
// get to color RGBA
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)
// calculate middle color RGBA
let newRed = fromRed + (toRed - fromRed) * percent
let newGreen = fromGreen + (toGreen - fromGreen) * percent
let newBlue = fromBlue + (toBlue - fromBlue) * percent
let newAlpha = fromAlpha + (toAlpha - fromAlpha) * percent
return UIColor(red: newRed, green: newGreen, blue: newBlue, alpha: newAlpha)
}
// Calculate the middle alpha
class fileprivate func middleAlpha(fromAlpha: CGFloat, toAlpha: CGFloat, percent: CGFloat) -> CGFloat
{
let newAlpha = fromAlpha + (toAlpha - fromAlpha) * percent
return newAlpha
}
/// add by sxiangyu 是否需要更新
class fileprivate func needUpdateNavigationBar(vc: UIViewController?) -> Bool {
if let _ = vc as? INNOBaseViewController {
return true
} else {
return false
}
}
`//
// WRNavigationBar.swift
// WRNavigationBar_swift
//
// Created by wangrui on 2017/4/19.
// Copyright © 2017年 wangrui. All rights reserved.
//
// Github地址:https://github.com/wangrui460/WRNavigationBar_swift
import UIKit
extension UINavigationBar:WRAwakeProtocol
{
fileprivate struct AssociatedKeys {
static var backgroundView: UIView = UIView()
static var backgroundImageView: UIImageView = UIImageView()
}
}
//==========================================================================
// MARK: - UINavigationController
//==========================================================================
extension UINavigationController: WRFatherAwakeProtocol
{
override open var preferredStatusBarStyle: UIStatusBarStyle {
return topViewController?.wrStatusBarStyle ?? WRNavigationBar.defaultStatusBarStyle
}
// setNeedsNavigationBarUpdate(barTintColor: newBarTintColor)
// modify by sxiangyu
if WRNavigationBar.needUpdateNavigationBar(vc: fromVC) || WRNavigationBar.needUpdateNavigationBar(vc: toVC) {
setNeedsNavigationBarUpdate(barTintColor: newBarTintColor)
}
// setNeedsNavigationBarUpdate(tintColor: newTintColor)
// modify by sxiangyu
if WRNavigationBar.needUpdateNavigationBar(vc: fromVC) {
setNeedsNavigationBarUpdate(tintColor: newTintColor)
}
}
//==========================================================================
// MARK: - deal the gesture of return
//==========================================================================
extension UINavigationController: UINavigationBarDelegate
{
public func navigationBar(_ navigationBar: UINavigationBar, shouldPop item: UINavigationItem) -> Bool
{
if let topVC = topViewController,
let coor = topVC.transitionCoordinator, coor.initiallyInteractive {
if #available(iOS 10.0, *) {
coor.notifyWhenInteractionChanges({ (context) in
self.dealInteractionChanges(context)
})
} else {
coor.notifyWhenInteractionEnds({ (context) in
self.dealInteractionChanges(context)
})
}
return true
}
}
//=============================================================================
// MARK: - store navigationBar barTintColor and tintColor every viewController
//=============================================================================
extension UIViewController: WRAwakeProtocol
{
fileprivate struct AssociatedKeys
{
static var pushToCurrentVCFinished: Bool = false
static var pushToNextVCFinished:Bool = false
}
//====================================================================================
// MARK: - Swizzling会改变全局状态,所以用 DispatchQueue.once 来确保无论多少线程都只会被执行一次
//====================================================================================
extension DispatchQueue {
}
//===========================================================================================
// MARK: - default navigationBar barTintColor、tintColor and statusBarStyle YOU CAN CHANGE!!!
//===========================================================================================
class WRNavigationBar
{
fileprivate struct AssociatedKeys
{ // default is system attributes
static var defNavBarBarTintColor: UIColor = UIColor.white
static var defNavBarBackgroundImage: UIImage = UIImage()
static var defNavBarTintColor: UIColor = UIColor(red: 0, green: 0.478431, blue: 1, alpha: 1.0)
static var defNavBarTitleColor: UIColor = UIColor.black
static var defStatusBarStyle: UIStatusBarStyle = UIStatusBarStyle.default
static var defShadowImageHidden: Bool = false
}
}
extension WRNavigationBar
{
class func isIphoneX() -> Bool {
return UIScreen.main.bounds.equalTo(CGRect(x: 0, y: 0, width: 375, height: 812)) ||
UIScreen.main.bounds.equalTo(CGRect(x: 0, y: 0, width: 812, height: 375)) ||
UIScreen.main.bounds.equalTo(CGRect(x: 0, y: 0, width: 414, height: 896)) ||
UIScreen.main.bounds.equalTo(CGRect(x: 0, y: 0, width: 896, height: 414))
}
class func navBarBottom() -> Int {
return self.isIphoneX() ? 88 : 64;
}
class func tabBarHeight() -> Int {
return self.isIphoneX() ? 83 : 49;
}
class func screenWidth() -> Int {
return Int(UIScreen.main.bounds.size.width)
}
class func screenHeight() -> Int {
return Int(UIScreen.main.bounds.size.height)
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 1. 定义 WRAwakeProtocol 协议
public protocol WRAwakeProtocol: class {
static func wrAwake()
}
public protocol WRFatherAwakeProtocol: class
{ // 1.1 定义 WRFatherAwakeProtocol ()
static func fatherAwake()
}
class NothingToSeeHere
{
static func harmlessFunction(){
// let typeCount = Int(objc_getClassList(nil, 0))
// let types = UnsafeMutablePointer<AnyClass?>.allocate(capacity: typeCount)
// let autoreleaseintTypes = AutoreleasingUnsafeMutablePointer(types)
// objc_getClassList(autoreleaseintTypes, Int32(typeCount)) //获取所有的类
// for index in 0 ..< typeCount {
// (types[index] as? WRAwakeProtocol.Type)?.wrAwake() //如果该类实现了SelfAware协议,那么调用 awake 方法
// (types[index] as? WRFatherAwakeProtocol.Type)?.fatherAwake()
// }
// types.deallocate(capacity: typeCount)
}
// 2. 让APP启动时只执行一次 harmlessFunction 方法
extension UIApplication
{
private static let runOnce:Void = { //使用静态属性以保证只调用一次(该属性是个方法)
NothingToSeeHere.harmlessFunction()
}()
}
// 3. 自定义类实现 WRAwakeProtocol 协议,重写 wrAwake 方法
// 自定义类实现 WRFatherAwakeProtocol 协议,重写 fatherAwake 方法
`
主要修改
`class fileprivate func needUpdateNavigationBar(vc: UIViewController?) -> Bool {
这里面的方法,我这边是有个公共的baseVC ,所以这样判断,大家可以可以设置黑名单列表之类的,在这里面判断。主要模仿OC的方式。
The text was updated successfully, but these errors were encountered: