cancel
Showing results for 
Search instead for 
Did you mean: 

How to implement smartphone like drag threshold in TouchGFX?

BlazK
Associate

I’m a beginner with TouchGFX, and I’m wondering whether it’s possible to make touch input behave the same way it does on mobile smartphones. Specifically, before a drag event is detected, the finger must move a certain number of pixels.

What I’d like is this: when a press event is first detected, you check whether the user has moved their finger at least 10 pixels, and only then do you start moving the screen. However, once that threshold is reached, the screen should move starting from 1 pixel. When the user releases their finger, a release event occurs and everything is reset, so that after the next press event, the finger must again move 10 pixels before a drag event can occur.

Is it possible to implement this behavior?

 

I tried using setDragThreshold, but then the entire screen scrolls by the number of pixels specified as the threshold. Is it possible to change setDragThreshold dynamically in the view?

1 ACCEPTED SOLUTION

Accepted Solutions
BlazK
Associate

Hello,

I think I solved the problem using touchgfx::HAL::getInstance()->setDragThreshold().

I am changing the drag threshold in the screen view. When the view starts, I call touchgfx::HAL::getInstance()->setDragThreshold(7);. After you touch the screen and move 7 pixels, the handleDragEvent function is triggered, and there I call touchgfx::HAL::getInstance()->setDragThreshold(0);, setting the threshold value to zero. When a Release or Cancel event occurs, I call touchgfx::HAL::getInstance()->setDragThreshold(7); to set the threshold value back to 7.

It seems to work well, but I’m wondering whether you think this approach is okay.

Thank you in advance.

 

Best regards,

Blaž

View solution in original post

3 REPLIES 3
JohanAstrup
ST Employee

Hello @BlazK.

For reference, you can have a look at the Elevator Demo available in TouchGFX Designer.

JohanAstrup_0-1770385653414.png

A drag threshold is implemented in the main screen. I have inserted the relevant code below.

 

void MainScreenView::handleDragEvent(const DragEvent& evt)
{
    if (scrollList1.getIsScrolling() == false && abs(evt.getNewY() - evt.getOldY()) < 4)
    {
        // Ignore initial drag if too small. This is to avoid accidental scrolling
    }
    else
    {
        dragEventHappened = true;
        MainScreenViewBase::handleDragEvent(evt);
    }
}

 

However, the best method to implement the drag threshold depends on your specific requirements, so please elaborate on your needs. Should the threshold e.g. apply to all drag events, or only on specific widgets?

Best regards,
Johan

BlazK
Associate

Hello,

I think I solved the problem using touchgfx::HAL::getInstance()->setDragThreshold().

I am changing the drag threshold in the screen view. When the view starts, I call touchgfx::HAL::getInstance()->setDragThreshold(7);. After you touch the screen and move 7 pixels, the handleDragEvent function is triggered, and there I call touchgfx::HAL::getInstance()->setDragThreshold(0);, setting the threshold value to zero. When a Release or Cancel event occurs, I call touchgfx::HAL::getInstance()->setDragThreshold(7); to set the threshold value back to 7.

It seems to work well, but I’m wondering whether you think this approach is okay.

Thank you in advance.

 

Best regards,

Blaž

JohanAstrup
ST Employee

Great! I think your approach is good :)

Best regards,
Johan