Skip to content

Commit 8f49f42

Browse files
committed
Use ViewConfiguration.getScaledTouchSlop as a default value for min distance of pan handler.
1 parent 2515be4 commit 8f49f42

File tree

3 files changed

+21
-12
lines changed

3 files changed

+21
-12
lines changed

AndroidNativeExample/app/src/main/java/com/swmansion/gesturehandler/example/MainActivity.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ public void onStateChange(PinchGestureHandler handler, int newState, int oldStat
165165
}
166166
});
167167

168-
registry.registerHandlerForView(largeBlock, new PanGestureHandler())
168+
registry.registerHandlerForView(largeBlock, new PanGestureHandler(this))
169169
.setMinDy(2)
170170
.setMaxPointers(1)
171171
.setOnTouchEventListener(new OnTouchEventListener<PanGestureHandler>() {

android/lib/src/main/java/com/swmansion/gesturehandler/PanGestureHandler.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package com.swmansion.gesturehandler;
22

3+
import android.content.Context;
34
import android.view.MotionEvent;
45
import android.view.VelocityTracker;
6+
import android.view.ViewConfiguration;
57

68
public class PanGestureHandler extends GestureHandler<PanGestureHandler> {
79

810
private static float MIN_VALUE_IGNORE = Float.MAX_VALUE;
911
private static float MAX_VALUE_IGNORE = Float.MIN_VALUE;
1012

11-
private static float DEFAULT_MIN_DISTANCE = 10.0f;
1213
private static int DEFAULT_MIN_POINTERS = 1;
1314
private static int DEFAULT_MAX_POINTERS = 10;
1415

@@ -18,7 +19,7 @@ public class PanGestureHandler extends GestureHandler<PanGestureHandler> {
1819
private float mMinDeltaY = MIN_VALUE_IGNORE;
1920
private float mMaxDeltaX = MAX_VALUE_IGNORE;
2021
private float mMaxDeltaY = MAX_VALUE_IGNORE;
21-
private float mMinDistSq = DEFAULT_MIN_DISTANCE * DEFAULT_MIN_DISTANCE;
22+
private float mMinDistSq = MAX_VALUE_IGNORE;
2223
private float mMinVelocityX = MIN_VALUE_IGNORE;
2324
private float mMinVelocityY = MIN_VALUE_IGNORE;
2425
private float mMinVelocitySq = MIN_VALUE_IGNORE;
@@ -93,6 +94,13 @@ private static float getLastPointerY(MotionEvent event, boolean averageTouches)
9394
}
9495
}
9596

97+
98+
public PanGestureHandler(Context context) {
99+
ViewConfiguration vc = ViewConfiguration.get(context);
100+
int touchSlop = vc.getScaledTouchSlop();
101+
mMinDistSq = touchSlop * touchSlop;
102+
}
103+
96104
public PanGestureHandler setMinDx(float deltaX) {
97105
mMinDeltaX = deltaX;
98106
return this;

android/src/main/java/com/swmansion/gesturehandler/react/RNGestureHandlerModule.java

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.swmansion.gesturehandler.react;
22

3+
import android.content.Context;
34
import android.util.SparseArray;
45
import android.view.MotionEvent;
56

@@ -68,7 +69,7 @@ private abstract static class HandlerFactory<T extends GestureHandler>
6869

6970
public abstract String getName();
7071

71-
public abstract T create();
72+
public abstract T create(Context context);
7273

7374
public void configure(T handler, ReadableMap config) {
7475
if (config.hasKey(KEY_SHOULD_CANCEL_WHEN_OUTSIDE)) {
@@ -98,7 +99,7 @@ public String getName() {
9899
}
99100

100101
@Override
101-
public NativeViewGestureHandler create() {
102+
public NativeViewGestureHandler create(Context context) {
102103
return new NativeViewGestureHandler();
103104
}
104105

@@ -132,7 +133,7 @@ public String getName() {
132133
}
133134

134135
@Override
135-
public TapGestureHandler create() {
136+
public TapGestureHandler create(Context context) {
136137
return new TapGestureHandler();
137138
}
138139

@@ -164,7 +165,7 @@ public String getName() {
164165
}
165166

166167
@Override
167-
public LongPressGestureHandler create() {
168+
public LongPressGestureHandler create(Context context) {
168169
return new LongPressGestureHandler();
169170
}
170171

@@ -189,8 +190,8 @@ public String getName() {
189190
}
190191

191192
@Override
192-
public PanGestureHandler create() {
193-
return new PanGestureHandler();
193+
public PanGestureHandler create(Context context) {
194+
return new PanGestureHandler(context);
194195
}
195196

196197
@Override
@@ -275,7 +276,7 @@ public String getName() {
275276
}
276277

277278
@Override
278-
public PinchGestureHandler create() {
279+
public PinchGestureHandler create(Context context) {
279280
return new PinchGestureHandler();
280281
}
281282

@@ -298,7 +299,7 @@ public String getName() {
298299
}
299300

300301
@Override
301-
public RotationGestureHandler create() {
302+
public RotationGestureHandler create(Context context) {
302303
return new RotationGestureHandler();
303304
}
304305

@@ -352,7 +353,7 @@ public void createGestureHandler(
352353
for (int i = 0; i < mHandlerFactories.length; i++) {
353354
HandlerFactory handlerFactory = mHandlerFactories[i];
354355
if (handlerFactory.getName().equals(handlerName)) {
355-
GestureHandler handler = handlerFactory.create();
356+
GestureHandler handler = handlerFactory.create(getReactApplicationContext());
356357
handler.setTag(handlerTag);
357358
handler.setOnTouchEventListener(mEventListener);
358359
getOrCreateRegistry().registerHandler(handler);

0 commit comments

Comments
 (0)