在 SwiftUI 的 View 上显示触摸点的标记。
为 SwiftUI 的 View 设置以下内容。
Text("Hello")
.touchTrack()
或者,也可以这样编写
TouchTrackingView {
Text("Hello")
}
Text("Hello")
.touchTrack() // show touch point
.touchPointRadius(8) // radius of mark on touched point
.touchPointOffset(x: 0, y: -10) // offset of mark position
.touchPointColor(.orange) // color of mark on touched point
.touchPointBorder(true, color: .blue, width: 1) // applying a border to touched points
.touchPointShadow(true, color: .purple, radius: 3) // shadow on touched points
.touchPointDisplayMode(.recordingOnly) // display mode of touched points
.showLocationLabel(true) // show touch coordinate
也可以显示图像,如下所示
Text("Hello")
.touchPointImage(Image(systemName: "swift").resizable())
如果想将其适配到使用 UIKit 创建的整个应用,请编写以下代码。使用名为 TouchTrackingUIView
的视图。
import TouchTracker
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
let v = TouchTrackingUIView(isShowLocation: true)
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
if let window {
window.addSubview(v)
v.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
v.topAnchor.constraint(equalTo: window.topAnchor),
v.bottomAnchor.constraint(equalTo: window.bottomAnchor),
v.leftAnchor.constraint(equalTo: window.leftAnchor),
v.rightAnchor.constraint(equalTo: window.rightAnchor),
])
}
return true
}
以下配置允许您在一个窗口中接收到的触摸事件与其他窗口共享。
v.shouldPropagateEventAcrossWindows = true
在给定的配置中,即使窗口重叠,通常只有一个窗口会接收到触摸事件。 此外,如果触摸事件在一个窗口中开始,例如窗口 A,即使它移动到另一个窗口,例如窗口 B,也只会响应窗口 A。
因此,可以共享触摸事件,以便即使在这种情况下,点击点的标记也会显示在另一个窗口中。
TouchTracker 根据 MIT 许可证发布。 请参阅 LICENSE