Skip to content

SwiftUI: LongPressGesture

Last updated on April 29, 2020

LongPressGesture can be done by long pressing that prompts an action. By default, the LongPressGesture is activated after half a second and you may even choose to change the duration (seconds).

A gesture that succeeds when the user performs a long press.

Apple Documentation

In this tutorial, you’ll learn what is LongPressGesture in SwiftUI. You’ll learn:

  • How to use LongPressGesture.

Prerequisites

To follow along this tutorial, you’ll need some basic knowledge in:

  • A basic familiarity with Swift.
  • At least Xcode 11

LongPressGesture

You will need to unlock a lock within a second by long pressing the lock. That shouldn’t sound too hard to build right? If you don’t have any idea, you can follow me below.

You will create a State that holds the value of the locked status.

With the value given, you will then show accordingly. If it’s true, the lock is unlocked and vice versa.

The result should be as follow:

Do remember that the default seconds is only half a second, if you wish for a longer second, you can tweak it accordingly.

What if in between the unlocking/locking, you would want some cool animation to take place? Well, you could do that as well. You will first need to know if the user is pressing the image by creating another state.

Once you have this, you gonna have to tweak the code a little bit.

And as a result:

Where to go From Here

If you feel like you are ready to take on more challenges, feel free to check out some other tutorials that we have created:

Published inSwiftUI

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Copyright © DaddyCoding 2017-2020