Skip to content

Commit

Permalink
加入是否默认启动功能
Browse files Browse the repository at this point in the history
  • Loading branch information
DuckDeck committed Nov 7, 2020
1 parent 2096c99 commit f509537
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
14 changes: 11 additions & 3 deletions Sources/ViewChaos.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ enum ChaosFeature:Int{
}

public protocol SelfAware:class {
static func awake()
static func awake(defaultActive:Bool)
}

class NothingToSeeHere{
Expand All @@ -22,7 +22,7 @@ class NothingToSeeHere{
let autoreleaseintTypes = AutoreleasingUnsafeMutablePointer<AnyClass>(types)
objc_getClassList(autoreleaseintTypes, Int32(typeCount))
for index in 0 ..< typeCount{
(types[index] as? SelfAware.Type)?.awake()
(types[index] as? SelfAware.Type)?.awake(defaultActive: true)
}
types.deallocate()
}
Expand All @@ -40,13 +40,19 @@ extension UIApplication{
}

public class ViewChaosStart: SelfAware {
public static func awake() {
static var alreadyStart = false
public static func awake(defaultActive:Bool = false) {
#if DEBUG
if alreadyStart{
return
}
print("启动ViewChaos")
Chaos.hookMethod(UIWindow.self, originalSelector: #selector(UIWindow.makeKeyAndVisible), swizzleSelector: #selector(UIWindow.vcMakeKeyAndVisible))
Chaos.hookMethod(UIView.self, originalSelector: #selector(UIView.willMove(toSuperview:)), swizzleSelector: #selector(UIView.vcWillMoveToSuperview(_:)))
Chaos.hookMethod(UIView.self, originalSelector: #selector(UIView.willRemoveSubview(_:)), swizzleSelector: #selector(UIView.vcWillRemoveSubview(_:)))
Chaos.hookMethod(UIView.self, originalSelector: #selector(UIView.didAddSubview(_:)), swizzleSelector: #selector(UIView.vcDidAddSubview(_:)))
Chaos.defaultActive = defaultActive
alreadyStart = true
#endif
}
}
Expand Down Expand Up @@ -93,6 +99,7 @@ extension UIWindow:UIActionSheetDelegate {
self.addSubview(viewChaos)
self.addObserver(self, forKeyPath: "rootViewController", options: NSKeyValueObservingOptions.new, context: nil)
viewChaos.addViewInfoView()
viewChaos.isHidden = !Chaos.defaultActive
let def = UserDefaults.standard
def.set(true, forKey: "ShakeEnable")
def.synchronize()
Expand Down Expand Up @@ -777,6 +784,7 @@ class ViewChaosObject: NSObject {


class Chaos {
static var defaultActive = false;
fileprivate static let sharedInstance = Chaos()
class var staredChaos:Chaos {
return sharedInstance
Expand Down
2 changes: 1 addition & 1 deletion ViewChaosDemo/ViewChaosDemo/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
#if DEBUG
ViewChaosStart.awake()
ViewChaosStart.awake(defaultActive: true)
#endif
window = UIWindow(frame: UIScreen.main.bounds)
print(UIScreen.main.bounds)
Expand Down

0 comments on commit f509537

Please sign in to comment.