Created
August 4, 2023 05:29
-
-
Save ryohey/12bd5fa92d41d3920d5be67626495ad0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
import UIKit | |
/// UIKit でジェスチャを取得するための透明な View | |
/// | |
/// overlay で利用する | |
/// 参考 https://stackoverflow.com/a/57943387/1567777 | |
struct ClearLongPressGestureView: UIViewRepresentable { | |
let onChanged: (Bool) -> Void | |
final class Coordinator: NSObject, UIGestureRecognizerDelegate { | |
let onChanged: (Bool) -> Void | |
init(onChanged: @escaping (Bool) -> Void) { | |
self.onChanged = onChanged | |
} | |
public func gestureRecognizer(_: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith _: UIGestureRecognizer) -> Bool { | |
true | |
} | |
@objc func didLongPress(_ gesture: UILongPressGestureRecognizer) { | |
print("didLongPress\(gesture.state.rawValue)") | |
switch gesture.state { | |
case .possible: | |
break | |
case .began: | |
onChanged(true) | |
case .ended, .cancelled, .failed: | |
onChanged(false) | |
default: | |
break | |
} | |
} | |
} | |
func makeCoordinator() -> Coordinator { | |
Coordinator(onChanged: onChanged) | |
} | |
func makeUIView(context: UIViewRepresentableContext<ClearLongPressGestureView>) -> UIView { | |
let view = UIView() | |
view.backgroundColor = .clear | |
let longPress = UILongPressGestureRecognizer(target: context.coordinator, action: #selector(Coordinator.didLongPress)) | |
longPress.minimumPressDuration = 0 | |
longPress.delaysTouchesBegan = false | |
longPress.delegate = context.coordinator | |
view.addGestureRecognizer(longPress) | |
return view | |
} | |
func updateUIView(_: UIView, context _: UIViewRepresentableContext<ClearLongPressGestureView>) {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment