How can I add haptic effect on a drag and drop ? I follow this tutorial: https://www.youtube.com/watch?v=UFiOCcm6zTo but I can't figure out how to add haptic when we select a view and drop it.
thanks
LazyVGrid(columns: columns, spacing: 10, content: {
ForEach(cards) { card in
CardView(numberOfColumns: CGFloat(numberOfColumns), is2ColumnsSelected: is2ColumnsSelected, size: size, card: card)
.draggable(card) {
CardView(numberOfColumns: CGFloat(numberOfColumns), is2ColumnsSelected: is2ColumnsSelected, size: size, card: card)
.frame(width: size.width/CGFloat(numberOfColumns) - 20)
.onAppear {
draggingCard = card
}
}
.dropDestination(for: Card.self) { items, location in
draggingCard = nil
return false
} isTargeted: { status in
if let draggingCard, status, draggingCard != card {
if let sourceIndex = cards.firstIndex(of: draggingCard), let destinationIndex = cards.firstIndex(of: card) {
withAnimation(.bouncy) {
let sourceCard = cards.remove(at: sourceIndex)
cards.insert(sourceCard, at: destinationIndex)
}
}
}
}
}
}
I tried to ad haptic in the draggable modifier and dropDestination modifier
CardView(numberOfColumns: CGFloat(numberOfColumns), is2ColumnsSelected: is2ColumnsSelected, size: size, card: card)
.draggable(card) {
UIImpactFeedbackGenerator(style: .medium).impactOccurred()
CardView(numberOfColumns: CGFloat(numberOfColumns), is2ColumnsSelected: is2ColumnsSelected, size: size, card: card)
.frame(width: size.width / CGFloat(numberOfColumns) - 20)
.onAppear {
draggingCard = card
}
}
.dropDestination(for: Card.self) { _, _ in
draggingCard = nil
return false
} isTargeted: { status in
if let draggingCard, status, draggingCard != card {
if let sourceIndex = cards.firstIndex(of: draggingCard), let destinationIndex = cards.firstIndex(of: card) {
withAnimation(.bouncy) {
let sourceCard = cards.remove(at: sourceIndex)
cards.insert(sourceCard, at: destinationIndex)
}
}
}
}
Use the
sensoryFeedback(_:trigger:)
modifier to add haptics. You just need to declare a new@State
as thetrigger:
, so that whenever the state changes, the haptics gets triggered.Then, all you need to do is to insert
dragTrigger.toggle()
andreorderTrigger.toggle()
at appropriate places, and add thesensoryFeedback