Skip to content

Commit d3631c4

Browse files
authored
[Android] Correct Rotation handler (#4079)
## Description Follow up for #4078 I've noticed that when `Rotation` does not activate, but pointers are released, `onFinalize` is called with `true`. This happens because we try to move it to `END` state, but our internal logic does not send `onDeactivate`. Code in `onHandle` that I removed was effectively dead, as `ACTION_UP` was handled in rotation detector - the only possible branch was `else` statement which fails handler if only one pointer was present. ## Test plan <details> <summary>Tested on Transformations example and the following code:</summary> ```tsx import { StyleSheet, View } from 'react-native'; import { GestureDetector, GestureHandlerRootView, usePinchGesture, useRotationGesture, } from 'react-native-gesture-handler'; export default function App() { const g = useRotationGesture({ onBegin: () => console.log('onBegin'), onActivate: () => console.log('onActivate'), onUpdate: (e) => console.log('onUpdate', e.rotation), onDeactivate: () => console.log('onDeactivate'), onFinalize: (_, s) => console.log('onFinalize', s), }); return ( <GestureHandlerRootView style={styles.container}> <GestureDetector gesture={g}> <View style={styles.box} /> </GestureDetector> </GestureHandlerRootView> ); } const styles = StyleSheet.create({ container: { flex: 1, alignItems: 'center', justifyContent: 'center', }, box: { width: 200, height: 200, backgroundColor: 'blue', borderRadius: 12, }, }); ``` </details>
1 parent 9a6c609 commit d3631c4

1 file changed

Lines changed: 10 additions & 7 deletions

File tree

packages/react-native-gesture-handler/android/src/main/java/com/swmansion/gesturehandler/core/RotationGestureHandler.kt

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ class RotationGestureHandler : GestureHandler() {
3535
override fun onRotationBegin(detector: RotationGestureDetector) = true
3636

3737
override fun onRotationEnd(detector: RotationGestureDetector) {
38-
end()
38+
if (state == STATE_ACTIVE) {
39+
end()
40+
} else {
41+
fail()
42+
}
3943
}
4044
}
4145

@@ -64,12 +68,11 @@ class RotationGestureHandler : GestureHandler() {
6468
anchorX = point.x
6569
anchorY = point.y
6670
}
67-
if (sourceEvent.actionMasked == MotionEvent.ACTION_UP) {
68-
if (state == STATE_ACTIVE) {
69-
end()
70-
} else {
71-
fail()
72-
}
71+
72+
// ACTION_UP is already handled in rotationGestureDetector.onTouchEvent (and effectively in onRotationEnd)
73+
// if more than one pointer was used
74+
if (sourceEvent.actionMasked == MotionEvent.ACTION_UP && state == STATE_BEGAN) {
75+
fail()
7376
}
7477
}
7578

0 commit comments

Comments
 (0)