Swift UITapGestureRecognizer 未调用
Swift UITapGestureRecognizer not calling
一个非常简单的问题,但比它应该解决的要难得多:我的手势很简单,根本就没有打电话。我正在使用显示在 zstack 内部的 uiviewrepresentable。如果我直接将 .tapgesture{} 添加到 CameraView() ,它就可以正常工作。但是我需要得到点击位置
public struct CameraView: UIViewRepresentable {
@EnvironmentObject var ue: UserEvents
public func makeUIView(context: Context) -> UIView {
let view = UIView(frame: UIScreen.main.bounds)
let focusGesture = UITapGestureRecognizer(target: self, action: #selector(context.coordinator.tapFocus(_:)))
self.ue.cameraPreview = AVCaptureVideoPreviewLayer(session: ue.session)
self.ue.cameraPreview.frame = view.frame
self.ue.cameraPreview.videoGravity = ue.videoGravity
self.ue.session.startRunning()
view.isUserInteractionEnabled = true
view.layer.addSublayer(self.ue.cameraPreview)
focusGesture.numberOfTapsRequired = 1
view.addGestureRecognizer(focusGesture)
return view
}
public func updateUIView(_ uiView: UIViewType, context: Context) { }
public func makeCoordinator() -> Self.Coordinator {
return Coordinator()
}
public class Coordinator: NSObject {
@objc public func tapFocus(_ sender: UITapGestureRecognizer) {
print("tap")
}
}
}
目标应该是 coordinator
(它是一个持久实体,不同于瞬态视图),而不是 self
。
let focusGesture = UITapGestureRecognizer(target: context.coordinator, action: #selector(context.coordinator.tapFocus(_:)))
一个非常简单的问题,但比它应该解决的要难得多:我的手势很简单,根本就没有打电话。我正在使用显示在 zstack 内部的 uiviewrepresentable。如果我直接将 .tapgesture{} 添加到 CameraView() ,它就可以正常工作。但是我需要得到点击位置
public struct CameraView: UIViewRepresentable {
@EnvironmentObject var ue: UserEvents
public func makeUIView(context: Context) -> UIView {
let view = UIView(frame: UIScreen.main.bounds)
let focusGesture = UITapGestureRecognizer(target: self, action: #selector(context.coordinator.tapFocus(_:)))
self.ue.cameraPreview = AVCaptureVideoPreviewLayer(session: ue.session)
self.ue.cameraPreview.frame = view.frame
self.ue.cameraPreview.videoGravity = ue.videoGravity
self.ue.session.startRunning()
view.isUserInteractionEnabled = true
view.layer.addSublayer(self.ue.cameraPreview)
focusGesture.numberOfTapsRequired = 1
view.addGestureRecognizer(focusGesture)
return view
}
public func updateUIView(_ uiView: UIViewType, context: Context) { }
public func makeCoordinator() -> Self.Coordinator {
return Coordinator()
}
public class Coordinator: NSObject {
@objc public func tapFocus(_ sender: UITapGestureRecognizer) {
print("tap")
}
}
}
目标应该是 coordinator
(它是一个持久实体,不同于瞬态视图),而不是 self
。
let focusGesture = UITapGestureRecognizer(target: context.coordinator, action: #selector(context.coordinator.tapFocus(_:)))