Provide alternatives for gestures
On Android, the GestureDetector and OnGestureListener objects are a common way to detect gestures.
A gesture should not be the only way to trigger actions. Make sure to provide a second way, such as a button, to trigger the same action.
kotlin
val scaleGestureDetector = ScaleGestureDetector(
this,
object : ScaleGestureDetector.SimpleOnScaleGestureListener() {
override fun onScale(detector: ScaleGestureDetector): Boolean {
// Provide alternative
return super.onScale(detector)
}
}
)
view.setOnTouchListener { _, event ->
scaleGestureDetector.onTouchEvent(event)
} In Jetpack Compose, you can use pointerInput.pointerInput(kotlin.Any,kotlin.coroutines.SuspendFunction1)) modifier to handle common type of gestures.
A gesture should not be the only way to trigger actions. Make sure to provide a second way, such as a button, to trigger the same action.
kotlin
Box(
modifier = Modifier
.pointerInput(Unit) {
detectTransformGestures { _, _, zoom, _ ->
// Provide alternative
}
}
) {
// Scalable Box content ...
} On iOS, the UIGestureRecognizer and UIGestureRecognizerDelegate objects are a common way to detect gestures.
A gesture should not be the only way to trigger actions. Make sure to provide a second way, such as a button, to trigger the same action.
swift
let gesture = UIPinchGestureRecognizer(
target: self,
action: #selector(onPinch(_:))
)
addGestureRecognizer(gesture)
@objc private func onPinch(_ sender: UIPinchGestureRecognizer) {
// Provide alternative
} In SwiftUI, gestures are a common way of interaction with the user interface. You can handle gestures by applying gesture modifiers directly to your views. This allows you to detect and respond to common gestures such as taps, drags, pinches, and more.
A gesture should not be the only way to trigger actions. Make sure to provide a second way, such as a button, to trigger the same action.
swift
@State private var scale: CGFloat = 1.0
var body: some View {
VStack {
Image("appt_logo")
.scaleEffect(scale) // Apply the scale effect
HStack {
Button(action: {
// Alternative way to trigger the same action
self.scale(factor: 0.1)
}) {
Text("Enlarge")
}
Button(action: {
// Alternative way to trigger the same action
self.scale(factor: -0.1)
}) {
Text("Shrink")
}
}
}
.gesture(
MagnificationGesture()
.onChanged { value in
self.scale = value
}
)
private func scale(factor: CGFloat) {
self.scale += factor
}
} In Flutter, the GestureDetector class is a common way to detect gestures.
A gesture should not be the only way to trigger actions. Make sure to provide a second way, such as a button, to trigger the same action.
dart
double _baseScaleFactor = 1;
double _scaleFactor = 1;
GestureDetector(
onScaleStart: (details) {
_baseScaleFactor = _scaleFactor;
},
onScaleUpdate: (details) {
setState(() {
_scaleFactor = _baseScaleFactor * details.scale;
// Provide alternative
});
},
child: ...
),
); In React Native, the Gesture Responder System is a common way to detect gestures.
A gesture should not be the only way to trigger actions. Make sure to provide a second way, such as a button, to trigger the same action.
jsx
import { PinchGestureHandler, State } from 'react-native-gesture-handler'
const PinchableView = () => {
scale = new Animated.Value(1)
onPinchEvent = Animated.event([{
nativeEvent: { scale: this.scale }
}], {
useNativeDriver: true
})
onPinchStateChange = event => {
if (event.nativeEvent.oldState === State.ACTIVE) {
// Provide alternative
Animated.spring(this.scale, {
toValue: 1,
useNativeDriver: true
}).start()
}
}
return (
<PinchGestureHandler
onGestureEvent={this.onPinchEvent}
onHandlerStateChange={this.onPinchStateChange}>
<View style={{
transform: [{ scale: this.scale }]
}} />
</PinchGestureHandler>
)
} In MAUI, you can use several types of Gesture Recognizers to detect user interactions with a UI component. The available gestures are:
csharp
var label = new Label();
var dragGestureRecognizer = new DragGestureRecognizer();
dragGestureRecognizer.CanDrag = true;
dragGestureRecognizer.DropCompleted += DragGestureRecognizer_DropCompleted;
dragGestureRecognizer.DragStarting += DragGestureRecognizer_DragStarting;
label.GestureRecognizers.Add(dragGestureRecognizer);
var dropGestureRecognizer = new DropGestureRecognizer();
dropGestureRecognizer.AllowDrop = true;
dropGestureRecognizer.DragLeave += DropGestureRecognizer_DragLeave;
dropGestureRecognizer.DragOver += DropGestureRecognizer_DragOver;
label.GestureRecognizers.Add(dropGestureRecognizer);
var panGestureRecognizer = new PanGestureRecognizer();
panGestureRecognizer.PanUpdated += PanGestureRecognizer_PanUpdated;
label.GestureRecognizers.Add(panGestureRecognizer);
var pinchGestureRecognizer = new PinchGestureRecognizer();
pinchGestureRecognizer.PinchUpdated += PinchGestureRecognizer_PinchUpdated;
label.GestureRecognizers.Add(pinchGestureRecognizer);
var pointerGestureRecognizer = new PointerGestureRecognizer();
pointerGestureRecognizer.PointerEntered += PointerGestureRecognizer_PointerEntered;
pointerGestureRecognizer.PointerExited += PointerGestureRecognizer_PointerExited;
pointerGestureRecognizer.PointerMoved += PointerGestureRecognizer_PointerMoved;
pointerGestureRecognizer.PointerPressed += PointerGestureRecognizer_PointerPressed;
label.GestureRecognizers.Add(pointerGestureRecognizer);
var swipeGestureRecognizer = new SwipeGestureRecognizer();
swipeGestureRecognizer.Direction = SwipeDirection.Left;
swipeGestureRecognizer.Swiped += SwipeGestureRecognizer_Swiped;
label.GestureRecognizers.Add(swipeGestureRecognizer);
var tapGestureRecognizer = new TapGestureRecognizer();
tapGestureRecognizer.NumberOfTapsRequired = 2;
tapGestureRecognizer.Tapped += TapGestureRecognizer_Tapped;
label.GestureRecognizers.Add(tapGestureRecognizer); csharp
<Label
Text="Hello">
<Label.GestureRecognizers>
<DragGestureRecognizer CanDrag="True" DropCompleted="DragGestureRecognizer_DropCompleted" DragStarting="DragGestureRecognizer_DragStarting" />
<DropGestureRecognizer AllowDrop="True" DragLeave="DropGestureRecognizer_DragLeave" DragOver="DropGestureRecognizer_DragOver" />
<PanGestureRecognizer PanUpdated="PanGestureRecognizer_PanUpdated" />
<PinchGestureRecognizer PinchUpdated="PinchGestureRecognizer_PinchUpdated" />
<PointerGestureRecognizer PointerEntered="PointerGestureRecognizer_PointerEntered" PointerExited="PointerGestureRecognizer_PointerExited" PointerMoved="PointerGestureRecognizer_PointerMoved" PointerPressed="PointerGestureRecognizer_PointerPressed" />
<SwipeGestureRecognizer Direction="Left" Swiped="SwipeGestureRecognizer_Swiped" />
<TapGestureRecognizer NumberOfTapsRequired="2" Tapped="TapGestureRecognizer_Tapped" />
</Label.GestureRecognizers>
</Label> With Xamarin, it is common to detect gestures using one of the recognizers defined in Xamarin Forms Gestures.
A gesture should not be the only way to trigger actions. Make sure to provide a second way, such as a button, to trigger the same action.
csharp
var pinchGesture = new PinchGestureRecognizer();
pinchGesture.PinchUpdated += (sender, event) => {
// Provide alternative
};
view.GestureRecognizers.Add(pinchGesture);