HomeiOS Developmentios - `onLongPressGesture` minimumDuration parameter is ignored in SwiftUI

ios – `onLongPressGesture` minimumDuration parameter is ignored in SwiftUI


Use a simultaneousGesture:

.onTapGesture {
    print("Faucet")
}
.simultaneousGesture(
    LongPressGesture(minimumDuration: 0.1).onEnded { _ in
        print("Lengthy Press")
    }
)

This may set off the faucet gesture in case you press for lower than some system-defined length (the utmost length that the system thinks a “faucet” ought to final), and can set off the lengthy press gesture in case you press for not less than 0.1 seconds.

Because of this typically each gestures get triggered – once you press for lower than the system-defined length, however greater than 0.1 seconds.

If you do not need that, you may implement the faucet gesture with a LongPressGesture too, simply with a really quick minimal length. Then, use completely(earlier than:) to deal with the battle.

let faucet = LongPressGesture(minimumDuration: 0.0001).onEnded({ _ in
    print("Faucet")
    })

let longPress = LongPressGesture(minimumDuration: 0.1).onEnded({ _ in
    print("Lengthy Press")
    })

YourView()
     // Give longPress extra priority over faucet
    .gesture(longPress.completely(earlier than: faucet))

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments