48 lines
1.5 KiB
Swift
48 lines
1.5 KiB
Swift
import SwiftUI
|
|
import UIKit
|
|
|
|
struct HoldRecordingControl: UIViewRepresentable {
|
|
let label: String
|
|
let enabled: Bool
|
|
let onPress: () -> Void
|
|
let onRelease: () -> Void
|
|
let onAccessibilityActivate: () -> Void
|
|
|
|
func makeUIView(context: Context) -> Control { Control() }
|
|
|
|
func updateUIView(_ view: Control, context: Context) {
|
|
view.accessibilityLabel = label
|
|
view.accessibilityHint = "使用旁白时,轻点两下开始,再轻点两下结束。"
|
|
view.isEnabled = enabled
|
|
view.onPress = onPress
|
|
view.onRelease = onRelease
|
|
view.onAccessibilityActivate = onAccessibilityActivate
|
|
}
|
|
|
|
final class Control: UIControl {
|
|
var onPress: (() -> Void)?
|
|
var onRelease: (() -> Void)?
|
|
var onAccessibilityActivate: (() -> Void)?
|
|
|
|
init() {
|
|
super.init(frame: .zero)
|
|
isAccessibilityElement = true
|
|
accessibilityTraits = .button
|
|
isMultipleTouchEnabled = false
|
|
addTarget(self, action: #selector(pressed), for: .touchDown)
|
|
addTarget(self, action: #selector(released), for: [.touchUpInside, .touchUpOutside, .touchCancel])
|
|
}
|
|
|
|
required init?(coder: NSCoder) { nil }
|
|
|
|
@objc private func pressed() { if isEnabled { onPress?() } }
|
|
@objc private func released() { onRelease?() }
|
|
|
|
override func accessibilityActivate() -> Bool {
|
|
guard isEnabled, let onAccessibilityActivate else { return false }
|
|
onAccessibilityActivate()
|
|
return true
|
|
}
|
|
}
|
|
}
|