Long press activation (context menu feel)
Holding an element until a context menu or secondary mode activates — the system "pop".
import SwiftUI
struct HoldableCard: View {
@State private var activated = 0
var body: some View {
RoundedRectangle(cornerRadius: 16)
.fill(.quaternary)
.frame(height: 120)
.onLongPressGesture(minimumDuration: 0.4) {
activated += 1
// show your menu / enter edit mode here
}
// "Pop" the instant the long press is recognized
.sensoryFeedback(.impact(weight: .medium), trigger: activated)
}
}
// Note: contextMenu {} plays the system pop automatically —
// only add a manual haptic for custom long-press behaviors. let haptic = UIImpactFeedbackGenerator(style: .medium)
let longPress = UILongPressGestureRecognizer(target: self,
action: #selector(handleHold(_:)))
longPress.minimumPressDuration = 0.4
cardView.addGestureRecognizer(longPress)
@objc func handleHold(_ gr: UILongPressGestureRecognizer) {
switch gr.state {
case .possible:
haptic.prepare()
case .began:
haptic.impactOccurred() // recognition moment
// enter edit mode / show menu
default:
break
}
}