https://kotlinlang.org logo
#compose
Title
# compose
v

Vivek Sharma

01/01/2022, 11:32 AM
Is there anything that can detect drag gesture started and stopped I am currently using it like this:
Copy code
Modifier.pointerInput(Unit) {
    detectTransformGestures { centroid, pan, zoom, rotation ->
        offset += pan
        scale *= zoom
    }
}
I am zooming the image and I need to know when the zoom drag is ended like we removed the fingers? Further info 👇
I want to use zooming + panning/dragging But when I use
detectTransformGestures
, I can't control
onDragEnd: () -> Unit
, there is no such param
But when I use
detectDragGestures
, I have
onDragEnd: () -> Unit
, But I can't control zooming , only I can control panning/dragging
a

Albert Chang

01/01/2022, 5:47 PM
You can use both. See my implementation.
🙏 1
m

Michael Paus

01/01/2022, 6:55 PM
I stumbled over the same issue some time ago and I think it is an omission in the API which makes things unnecessarily complicated.
v

Vivek Sharma

01/02/2022, 6:15 AM
@Michael Paus yeah I think so 😄, but there are some things to look in the above repo
a

Albert Chang

01/02/2022, 10:41 AM
IMO current API with coroutines integrated is much better compared to touch event handling in the view system as it allows you to separate logics of handling different gestures while in the view system you have to mix all the logics. If you want to detect multiple gestures, just use multiple
detect*Gestures
functions. Nothing counterintuitive.
v

Vivek Sharma

01/02/2022, 11:06 AM
I tried using it like
Copy code
Modifier.pointerInput(Unit) {
    detectTransformGestures { centroid, pan, zoom, rotation ->
        offset += pan
        scale *= zoom
    }
    detectDragGestures {
       // used onDragEnd() something here
    }
}
But this thing didn't work and only the lambda fun used first was working only
@Albert Chang I looked into your Repo, this thing looks complex but I have no other option I guess
a

Albert Chang

01/02/2022, 12:06 PM
As those are suspend functions, you need to use
coroutineScope
and
launch
as shown here. If you are familiar with coroutines, there’s nothing special here.
m

Michael Paus

01/02/2022, 12:19 PM
@Vivek Sharma Somewhere else here in kotlinlang I was told that putting multiple detect*s into a single pointerInput is wrong. One should always do it like this:
Copy code
Modifier
.pointerInput(Unit) {
    detectTransformGestures { centroid, pan, zoom, rotation ->
        offset += pan
        scale *= zoom
    }
}
.pointerInput(Unit) {
    detectDragGestures {
       // used onDragEnd() something here
    }
}
But this won’t change anything here because
detectTransformGestures
and
detectDragGestures
are mutually exclusive.
v

Vivek Sharma

01/02/2022, 12:28 PM
I also tried using this before asking here! And I found that they don't behave as we want and here only one thing was working , other was not
a

Albert Chang

01/02/2022, 12:33 PM
That’s why I’m using
Modifier.transformable()
instead of
detectTransformGestures
.
v

Vivek Sharma

01/02/2022, 1:36 PM
But can we control
onDragEnd
something like that in
Modifier.transformable()
?
a

Albert Chang

01/02/2022, 5:08 PM
You can't so I said you can use both.
@Michael Paus To be clear, detecting multiple gestures in the same
PointerInputScope
is valid as documented here.
More than one 
awaitPointerEventScope
 can run concurrently in the same 
PointerInputScope
 by using 
kotlinx.coroutines.launch
. `block`s are dispatched to in the order in which they were installed.
Also see design note here.
m

Michael Paus

01/04/2022, 10:45 AM
66 Views