Last active
March 10, 2023 11:38
-
-
Save onivas/254332fcc6ef8a45444a9eaf39383191 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 androidx.compose.foundation.gestures.detectDragGestures | |
import androidx.compose.runtime.getValue | |
import androidx.compose.runtime.mutableStateOf | |
import androidx.compose.runtime.remember | |
import androidx.compose.runtime.setValue | |
import androidx.compose.ui.Modifier | |
import androidx.compose.ui.composed | |
import androidx.compose.ui.input.pointer.pointerInput | |
fun Modifier.interceptSwipeDown(onSwipeDown: () -> Unit): Modifier = composed { | |
var startYValue by remember { mutableStateOf(0F) } | |
var lastYValue by remember { mutableStateOf(0F) } | |
pointerInput(Unit) { | |
detectDragGestures( | |
onDragStart = { startYValue = it.y }, | |
onDragEnd = { if (startYValue < lastYValue) onSwipeDown() } | |
) { change, _ -> lastYValue = change.position.y } | |
} | |
} | |
fun Modifier.interceptSwipeUp(onSwipeUp: () -> Unit): Modifier = composed { | |
var startYValue by remember { mutableStateOf(0F) } | |
var lastYValue by remember { mutableStateOf(0F) } | |
pointerInput(Unit) { | |
detectDragGestures( | |
onDragStart = { startYValue = it.y }, | |
onDragEnd = { if (startYValue > lastYValue) onSwipeUp() } | |
) { change, _ -> lastYValue = change.position.y } | |
} | |
} | |
fun Modifier.interceptSwipe(onSwipeUp: () -> Unit, onSwipeDown: () -> Unit): Modifier = composed { | |
var startYValue by remember { mutableStateOf(0F) } | |
var lastYValue by remember { mutableStateOf(0F) } | |
pointerInput(Unit) { | |
detectDragGestures( | |
onDragStart = { startYValue = it.y }, | |
onDragEnd = { | |
if (startYValue > lastYValue) onSwipeUp() | |
else onSwipeDown() | |
} | |
) { change, _ -> lastYValue = change.position.y } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment