LoggingViewKit
LoggingViewKit 是一个用于记录用户点击操作的框架。
在项目中包含
Carthage
LoggingViewKit 通过 Carthage 提供。要安装它,只需将以下行添加到您的 Cartfile 即可
github "HituziANDO/LoggingViewKit"
CocoaPods
LoggingViewKit 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中
pod "LoggingViewKit"
Swift Package Manager
LoggingViewKit 通过 Swift Package Manager 提供。要使用 Xcode 安装它,请指定 LoggingViewKit 的 git URL。
https://github.com/HituziANDO/LoggingViewKit
导入框架
Swift
import LoggingViewKit
Objective-C
#import <LoggingViewKit/LoggingViewKit.h>
用法
-
以编程方式编写点击事件
在下面的代码中,将
buttonPressed
方法设置为 UIButton 的动作方法。Swift
@objc func buttonPressed(_ sender: Any) { // Records a click event. let attr = LGVLoggingAttribute(view: sender, name: "SampleButton", loggingEnabled: true) attr.info = ["more-info": "test"] LGVLoggingViewService.shared().click(attr) }
Objective-C
- (void)buttonPressed:(id)sender { // Records a click event. LGVLoggingAttribute *attr = [LGVLoggingAttribute attributeWithView:sender name:@"SampleButton" loggingEnabled:YES]; attr.info = @{ @"more-info": @"test" }; [[LGVLoggingViewService sharedService] click:attr]; }
-
开始记录
Swift
LGVLoggingViewService.shared().startRecording()
Objective-C
[[LGVLoggingViewService sharedService] startRecording];
-
停止记录
Swift
LGVLoggingViewService.shared().stopRecording()
Objective-C
[[LGVLoggingViewService sharedService] stopRecording];
-
读取所有日志
Swift
let logs = LGVLoggingViewService.shared().allLogs()
Objective-C
NSArray<LGVLog *> *logs = [[LGVLoggingViewService sharedService] allLogs];
-
删除所有日志
Swift
LGVLoggingViewService.shared().deleteAllLogs()
Objective-C
[[LGVLoggingViewService sharedService] deleteAllLogs];
更多信息,请查看我的示例项目。
使用Storyboard
如果您使用Storyboard,可以在Storyboard中设置UI类,例如 LGVButton
。
LoggingViewKit 默认有以下 UI 类。
- 按钮
- 标签
- 分段控件
- 滑块
- 步进器
- 开关
- 视图
示例日志
LoggingViewKit 记录的日志如下。
{
ID = 47;
eventType = "click";
absoluteClickX = "124.3333282470703";
absoluteClickY = "189.6666564941406";
clickX = "108.3333282470703";
clickY = "145.6666564941406";
createdAt = "2018-12-25 23:02:13 +0000";
info = {
newValue = 2;
};
key = "7F34859D-2164-4B4B-B896-EA9D3D826C92";
name = SampleSegmentedControl;
}
打印视图层次结构
LoggingViewKit 可以将指定视图的层次结构打印到 Xcode 控制台。示例日志如下。
2019-04-02 12:11:59.876292+0900 LoggingViewSwiftSample[8616:19026371] ===ViewHierarchy===
UIView
LGVView(loggingName: (null))
LGVButton(loggingName: SampleButton)
LGVSwitch(loggingName: SampleSwitch)
UISwitchModernVisualElement
UIView
UIView
UIView
UIView
UIView
UIImageView
UIImageView
UIImageView
LGVSegmentedControl(loggingName: SampleSegmentedControl)
UISegment
UISegmentLabel
UIImageView
UISegment
UISegmentLabel
UIImageView
UISegment
UISegmentLabel
UIImageView
UISegment
UISegmentLabel
UIImageView
LGVStepper(loggingName: SampleStepper)
_UIStepperButton
_UIStepperButton
UIImageView
LGVLabel(loggingName: SampleLabel)
LGVView(loggingName: SampleView)
LGVButton(loggingName: TestButton)
LGVSlider(loggingName: SampleSlider)
用法
Swift
override func viewDidLoad() {
super.viewDidLoad()
#if DEBUG
// Dumps hierarchy of the root view.
LGVViewHierarchy.dump(view)
#endif
}
[注意] 建议使用 #if DEBUG ~ #endif
包围。然后 LoggingViewKit 仅在调试版本中记录日志。
如何启用 DEBUG 标志
- 打开构建设置 > Swift 编译器 - 自定义标志 > 其他 Swift 标志部分
- 在调试行中添加
-DDEBUG
标志
Objective-C
- (void)viewDidLoad {
[super viewDidLoad];
#ifdef DEBUG
// Dumps the hierarchy of the root view.
[LGVViewHierarchy dump:self.view];
#endif
}
[注意] 建议使用 #ifdef DEBUG ~ #endif
包围。然后 LoggingViewKit 仅在调试版本中记录日志。