|
11 | 11 |
|
12 | 12 | import SwiftUI |
13 | 13 |
|
14 | | -struct CameraInputBridgeView: UIViewRepresentable { |
| 14 | +struct CameraInputBridgeView { |
15 | 15 | let cameraManager: CameraManager |
16 | | - private var inputView: UICameraInputView = .init() |
17 | | - |
18 | | - init(_ cameraManager: CameraManager) { self.cameraManager = cameraManager } |
19 | | -} |
20 | | -extension CameraInputBridgeView { |
21 | | - func makeUIView(context: Context) -> some UIView { |
22 | | - inputView.cameraManager = cameraManager |
23 | | - return inputView.view |
24 | | - } |
25 | | - func updateUIView(_ uiView: UIViewType, context: Context) {} |
26 | | -} |
27 | | -extension CameraInputBridgeView: Equatable { |
28 | | - static func == (lhs: Self, rhs: Self) -> Bool { true } |
| 16 | + let inputView: UIView = .init() |
29 | 17 | } |
30 | 18 |
|
31 | 19 |
|
32 | | -// MARK: - UIViewController |
33 | | -fileprivate class UICameraInputView: UIViewController { |
34 | | - var cameraManager: CameraManager! |
| 20 | +// MARK: - PROTOCOLS CONFORMANCE |
35 | 21 |
|
36 | 22 |
|
37 | | - override func viewDidLoad() { |
38 | | - super.viewDidLoad() |
39 | 23 |
|
| 24 | +// MARK: UIViewRepresentable |
| 25 | +extension CameraInputBridgeView: UIViewRepresentable { |
| 26 | + func makeUIView(context: Context) -> some UIView { |
40 | 27 | setupCameraManager() |
41 | | - setupTapGesture() |
42 | | - setupPinchGesture() |
43 | | - } |
44 | | - override func viewDidLayoutSubviews() { |
45 | | - super.viewDidLayoutSubviews() |
46 | | - |
47 | | - cameraManager.fixCameraRotation() |
| 28 | + setupTapGesture(context) |
| 29 | + setupPinchGesture(context) |
| 30 | + return inputView |
48 | 31 | } |
| 32 | + func updateUIView(_ uiView: UIViewType, context: Context) {} |
| 33 | + func makeCoordinator() -> Coordinator { .init(self) } |
49 | 34 | } |
50 | | - |
51 | | -// MARK: - Setup |
52 | | -private extension UICameraInputView { |
| 35 | +private extension CameraInputBridgeView { |
53 | 36 | func setupCameraManager() { |
54 | | - cameraManager.setup(in: view) |
| 37 | + cameraManager.setup(in: inputView) |
55 | 38 | } |
56 | | - func setupTapGesture() { |
57 | | - let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTapGesture)) |
58 | | - view.addGestureRecognizer(tapRecognizer) |
| 39 | + func setupTapGesture(_ context: Context) { |
| 40 | + let tapRecognizer = UITapGestureRecognizer(target: context.coordinator, action: #selector(context.coordinator.onTapGesture)) |
| 41 | + inputView.addGestureRecognizer(tapRecognizer) |
59 | 42 | } |
60 | | - func setupPinchGesture() { |
61 | | - let pinchRecognizer = UIPinchGestureRecognizer(target: self, action: #selector(handlePinchGesture)) |
62 | | - view.addGestureRecognizer(pinchRecognizer) |
| 43 | + func setupPinchGesture(_ context: Context) { |
| 44 | + let pinchRecognizer = UIPinchGestureRecognizer(target: context.coordinator, action: #selector(context.coordinator.onPinchGesture)) |
| 45 | + inputView.addGestureRecognizer(pinchRecognizer) |
63 | 46 | } |
64 | 47 | } |
65 | 48 |
|
66 | | -// MARK: - Gestures |
67 | | - |
68 | | -// MARK: Tap |
69 | | -private extension UICameraInputView { |
70 | | - @objc func handleTapGesture(_ tap: UITapGestureRecognizer) { |
71 | | - let touchPoint = tap.location(in: view) |
72 | | - setCameraFocus(touchPoint) |
73 | | - } |
| 49 | +// MARK: Equatable |
| 50 | +extension CameraInputBridgeView: Equatable { |
| 51 | + static func ==(lhs: Self, rhs: Self) -> Bool { true } |
74 | 52 | } |
75 | | -private extension UICameraInputView { |
76 | | - func setCameraFocus(_ touchPoint: CGPoint) { |
77 | | - do { try cameraManager.setCameraFocus(touchPoint) } |
78 | | - catch {} |
| 53 | + |
| 54 | + |
| 55 | +// MARK: - LOGIC |
| 56 | +extension CameraInputBridgeView { class Coordinator: NSObject { init(_ parent: CameraInputBridgeView) { self.parent = parent } |
| 57 | + let parent: CameraInputBridgeView |
| 58 | +}} |
| 59 | + |
| 60 | +// MARK: On Tap |
| 61 | +extension CameraInputBridgeView.Coordinator { |
| 62 | + @objc func onTapGesture(_ tap: UITapGestureRecognizer) { |
| 63 | + do { |
| 64 | + let touchPoint = tap.location(in: parent.inputView) |
| 65 | + try parent.cameraManager.setCameraFocus(touchPoint) |
| 66 | + } catch {} |
79 | 67 | } |
80 | 68 | } |
81 | 69 |
|
82 | | -// MARK: Pinch |
83 | | -private extension UICameraInputView { |
84 | | - @objc func handlePinchGesture(_ pinch: UIPinchGestureRecognizer) { if pinch.state == .changed { |
85 | | - let desiredZoomFactor = cameraManager.attributes.zoomFactor + atan2(pinch.velocity, 33) |
86 | | - changeZoomFactor(desiredZoomFactor) |
| 70 | +// MARK: On Pinch |
| 71 | +extension CameraInputBridgeView.Coordinator { |
| 72 | + @objc func onPinchGesture(_ pinch: UIPinchGestureRecognizer) { if pinch.state == .changed { |
| 73 | + do { |
| 74 | + let desiredZoomFactor = parent.cameraManager.attributes.zoomFactor + atan2(pinch.velocity, 33) |
| 75 | + try parent.cameraManager.changeZoomFactor(desiredZoomFactor) |
| 76 | + } catch {} |
87 | 77 | }} |
88 | 78 | } |
89 | | -private extension UICameraInputView { |
90 | | - func changeZoomFactor(_ desiredZoomFactor: CGFloat) { |
91 | | - do { try cameraManager.changeZoomFactor(desiredZoomFactor) } |
92 | | - catch {} |
93 | | - } |
94 | | -} |
0 commit comments