提供了一个树的垂直结构绘制,可以查看树的节点信息,并支持控制台调试视图和层等。
安装
需要 iOS >= 9.0
和 Swift5.0
,使用 Cocoapods
pod 'VerticalTree'
只安装核心功能
pod 'VerticalTree/Core'
安装视图、层、viewController的prettyPrint扩展
pod 'VerticalTree/PrettyExtension'
文件结构
─┬─ "VerticalTree"
├─┬─ "Core": basic functions
│ ├─── VerticalTreeProtocol
│ ├─── VerticalTreeProtocolExtension
│ └─── VerticalTreeNodeWrapper
├─┬─ "UI": draw a graph of VerticalTree which is foldable
│ ├─── VerticalTreeCell
│ ├─── VerticalTreeIndexView
│ └─── VerticalTreeListController
└─┬─ "PrettyExtension": pretty print in xcode console
└─── VerticalTreePrettyPrint
核心协议
/// base node
public protocol BaseNode {
associatedtype T: BaseNode
var parent: T? { get }
var childs: [T] { get }
}
/// base treeNode structure and position
public protocol IndexPathNode: BaseNode {
var indexPath: IndexPath { get }
}
/// Node protocol
public protocol VerticalTreeNode: IndexPathNode where Self.T == Self {
/// indexViewLegnth
var length: TreeNodeLength { get }
/// info description
var info: Infomation { get }
var isFold: Bool { set get }
}
UIView示例
UIView
是一个树结构
- VerticalTree在tableview中
- VerticalTree在控制台日志中
使用
1. 在TableView中绘制VerticalTree
例如一个UITableViewCell结构
// in ViewController
let treeVC = VerticalTreeListController(source: NodeWrapper(obj: view))
// then show the treeVC
配置节点信息
在NodeWrapper的方法中
/// config current node’s property value and recurrence apply the same rule in childNodes if you want
///
/// - Parameters:
/// - inChild: recurrence config in child or just config current
/// - config: rules
/// - Returns: self
@discardableResult
public func changeProperties(inChild: Bool = true, config: (NodeWrapper<Obj>) -> Void) -> Self {}
查看图片:修改UIViewController的Wrapper
// default to change all subnode in the same rules unless inChild set false
let wrapper = NodeWrapper(obj: keyWindow.rootController).changeProperties {
$0.isFold = false // all node set unfold in default
$0.nodeDescription = "more infomation that you see now"
}
2. 在控制台中输出Text VerticalTree
使用UIView作为遵循IndexPathNode协议的示例
查看demo中的其他扩展,如CALayer,UIViewController
extension UIView: BaseNode {
public var parent: UIView? {
return superview
}
public var childs: [UIView] {
return subviews
}
}
获取作为一个根节点的视图wrapper
// in ViewController
let rootNode = NodeWrapper(obj: view)
// print node structure in text
print(rootNode.subTreePrettyText())
使用UIView的扩展作为更简单的方式,请查看这里 VerticalTree/PrettyText
extension BaseNode where Self: NSObject, Self == Self.T {
/// print
public func treePrettyPrint(inDebug: Bool = false) {...}
/// baseTree‘s structure
public func treePrettyText(inDebug: Bool = false) -> String {...}
/// get ofTop‘s structure & highlight position of self
public func treePrettyText(ofTop: Self, inDebug: Bool = false) { ... }
// get the baseTree of rootNode
public var getTreeRoot: Self { ... }
}
- 以根节点打印当前视图
view.treePrettyPrint()
- 打印视图的Window结构
view.rootNode.treePrettyPrint()
- 显示更多详细信息
view.treePrettyPrint(inDebug: true)
BTW
- 使用LLDB调试视图、层和控制器的层级结构
- 视图和层
- 方法-1:
po yourObj.value(forKey: "recursiveDescription")!
- 方法-2:
expression -l objc -O -- [
recursiveDescription]
- 方法-1:
- 控制器
- 方法-1:
po yourController.value(forKey: "_printHierarchy")!
- 方法-2:
expression -l objc -O -- [
_printHierarchy]
- 方法-1:
- 视图和层
作者
XcodeYang, [email protected]
协议
VerticalTree可在MIT协议下使用。有关更多信息,请参阅LICENSE文件。