Making widget follow finger slide

After seeing tutorials on making a side sliding panel, I decided it would be best to just make my own since I had ideas of how I wanted it customized, my problem now is, I’m having a hard time making the panel move with the user’s finger. On Android phones, a user can slide the sliding panel and it follows the user’s finger within certain limits, that’s what I’m trying to create. 

Here’s the code I’ve come up with so far:

function slideTouch (event) if event.phase == "moved" then dist = ( event.x - event.xStart ) --distance finger has slided object.x = object.x + dist --move object by that distance return true end

The reason I did it like this rather than

object.x = event.x

Was because I want it to look like the panel is being moved from the point of contact, rather than being moved by the center but my code doesn’t work right. Sliding just the tiniest bit causes the panel to slide a large distance. 

I’d really like some help wit this, thank you!

That’s because for every move event handled, you’re incresing the object’s x position by the entire distance of the user’s swipe. One way of solving this would be to store the original x position of the object when the swipe starts. Something like this:

local function slideTouch (event) if (event.phase == "began") then object.startX = object.x elseif (event.phase == "moved") then dist = event.x - event.xStart object.x = object.startX + dist end return true end

Thank you! that worked!

That’s because for every move event handled, you’re incresing the object’s x position by the entire distance of the user’s swipe. One way of solving this would be to store the original x position of the object when the swipe starts. Something like this:

local function slideTouch (event) if (event.phase == "began") then object.startX = object.x elseif (event.phase == "moved") then dist = event.x - event.xStart object.x = object.startX + dist end return true end

Thank you! that worked!