feat:fix gesture touch event's conflict in scroller

This commit is contained in:
pengfei.zhou 2022-08-01 19:01:02 +08:00 committed by osborn
parent f4fcb981b4
commit 6e3307d400
2 changed files with 124 additions and 61 deletions

View File

@ -18,6 +18,8 @@ package pub.doric.shader;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.content.Context; import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.GestureDetector; import android.view.GestureDetector;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.view.ScaleGestureDetector; import android.view.ScaleGestureDetector;
@ -44,7 +46,7 @@ import pub.doric.utils.DoricUtils;
@DoricPlugin(name = "GestureContainer") @DoricPlugin(name = "GestureContainer")
public class GestureContainerNode extends StackNode { public class GestureContainerNode extends StackNode {
private DoricJSDispatcher jsDispatcher = new DoricJSDispatcher(); private final DoricJSDispatcher jsDispatcher = new DoricJSDispatcher();
private enum SwipeOrientation { private enum SwipeOrientation {
LEFT(0), RIGHT(1), TOP(2), BOTTOM(3); LEFT(0), RIGHT(1), TOP(2), BOTTOM(3);
@ -252,7 +254,7 @@ public class GestureContainerNode extends StackNode {
if (onPinch != null) { if (onPinch != null) {
jsDispatcher.dispatch(new Callable<AsyncResult<JSDecoder>>() { jsDispatcher.dispatch(new Callable<AsyncResult<JSDecoder>>() {
@Override @Override
public AsyncResult<JSDecoder> call() throws Exception { public AsyncResult<JSDecoder> call() {
return callJSResponse(onPinch, scaleGestureDetector.getScaleFactor()); return callJSResponse(onPinch, scaleGestureDetector.getScaleFactor());
} }
}); });
@ -307,8 +309,9 @@ public class GestureContainerNode extends StackNode {
} }
break; break;
case MotionEvent.ACTION_MOVE: case MotionEvent.ACTION_MOVE:
if (shouldRequestDisallowInterceptTouchEvent(event)) {
getParent().requestDisallowInterceptTouchEvent(true); getParent().requestDisallowInterceptTouchEvent(true);
}
if (onTouchMove != null) { if (onTouchMove != null) {
jsDispatcher.dispatch(new Callable<AsyncResult<JSDecoder>>() { jsDispatcher.dispatch(new Callable<AsyncResult<JSDecoder>>() {
@Override @Override
@ -323,6 +326,9 @@ public class GestureContainerNode extends StackNode {
break; break;
case MotionEvent.ACTION_UP: case MotionEvent.ACTION_UP:
if (shouldRequestDisallowInterceptTouchEvent(event)) {
getParent().requestDisallowInterceptTouchEvent(false);
}
if (onTouchUp != null) { if (onTouchUp != null) {
callJSResponse(onTouchUp, new JSONBuilder() callJSResponse(onTouchUp, new JSONBuilder()
.put("x", x) .put("x", x)
@ -330,8 +336,9 @@ public class GestureContainerNode extends StackNode {
.toJSONObject()); .toJSONObject());
} }
case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_CANCEL:
if (shouldRequestDisallowInterceptTouchEvent(event)) {
getParent().requestDisallowInterceptTouchEvent(false); getParent().requestDisallowInterceptTouchEvent(false);
}
if (onTouchCancel != null) { if (onTouchCancel != null) {
callJSResponse(onTouchCancel, new JSONBuilder() callJSResponse(onTouchCancel, new JSONBuilder()
.put("x", x) .put("x", x)
@ -343,10 +350,19 @@ public class GestureContainerNode extends StackNode {
} }
// handle gesture // handle gesture
boolean scaleRet = scaleGestureDetector.onTouchEvent(event); boolean scaleRet = !TextUtils.isEmpty(onPinch) && scaleGestureDetector.onTouchEvent(event);
boolean rotateRet = rotateGestureDetector.onTouchEvent(event); boolean rotateRet = !TextUtils.isEmpty(onRotate) && rotateGestureDetector.onTouchEvent(event);
boolean commonRet = gestureDetector.onTouchEvent(event); boolean commonRet = gestureDetector.onTouchEvent(event);
return (scaleRet || rotateRet || commonRet) || super.onTouchEvent(event); if (scaleRet || rotateRet) {
int pointerIndex = (event.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
int action = (event.getAction() & MotionEvent.ACTION_MASK);
if (action == MotionEvent.ACTION_POINTER_DOWN && pointerIndex == 1) {
getParent().requestDisallowInterceptTouchEvent(true);
} else if (action == MotionEvent.ACTION_POINTER_UP && pointerIndex == 1) {
getParent().requestDisallowInterceptTouchEvent(false);
}
}
return (scaleRet || rotateRet || commonRet) || super.onTouchEvent(event) || event.getAction() == MotionEvent.ACTION_DOWN;
} }
private void onSwipeLeft() { private void onSwipeLeft() {
@ -373,4 +389,10 @@ public class GestureContainerNode extends StackNode {
} }
} }
} }
private boolean shouldRequestDisallowInterceptTouchEvent(MotionEvent event) {
return !TextUtils.isEmpty(onPan)
|| !TextUtils.isEmpty(onSwipe)
|| !TextUtils.isEmpty(onTouchMove);
}
} }

View File

@ -100,7 +100,12 @@ @interface DoricGestureContainerNode()
@property(nonatomic) CGPoint startPanLocation; @property(nonatomic) CGPoint startPanLocation;
@property(nonatomic) CGFloat startRotationDegree; @property(nonatomic) CGFloat startRotationDegree;
@property(nonatomic, strong) UIGestureRecognizer *singleTapGesture;
@property(nonatomic, strong) UITapGestureRecognizer *doubleTapGesture;
@property(nonatomic, strong) UIGestureRecognizer *longPress;
@property(nonatomic, strong) UIGestureRecognizer *pinchGesture;
@property(nonatomic, strong) UIGestureRecognizer *panGesture;
@property(nonatomic, strong) UIGestureRecognizer *rotationGesture;
@end @end
#define SWIPE_UP_THRESHOLD -1000.0f #define SWIPE_UP_THRESHOLD -1000.0f
@ -114,29 +119,6 @@ - (UIView *)build {
it.doricLayout.layoutType = DoricStack; it.doricLayout.layoutType = DoricStack;
it.node = self; it.node = self;
}]; }];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapAction:)];
[stackView addGestureRecognizer:singleTap];
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapAction:)];
doubleTap.numberOfTapsRequired = 2;
[stackView addGestureRecognizer:doubleTap];
[singleTap requireGestureRecognizerToFail:doubleTap];
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
[stackView addGestureRecognizer:longPress];
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
[stackView addGestureRecognizer:pinchGesture];
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
panGesture.cancelsTouchesInView = NO;
[stackView addGestureRecognizer:panGesture];
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
[stackView addGestureRecognizer:rotation];
return stackView; return stackView;
} }
@ -245,25 +227,84 @@ -(void)rotationAction:(UIRotationGestureRecognizer *)sender{
} }
} }
- (UIGestureRecognizer *)singleTapGesture {
if (_singleTapGesture == nil) {
_singleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapAction:)];
}
return _singleTapGesture;
}
- (UITapGestureRecognizer *)doubleTapGesture {
if (_doubleTapGesture == nil) {
_doubleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapAction:)];
_doubleTapGesture.numberOfTapsRequired = 2;
}
return _doubleTapGesture;
}
- (UIGestureRecognizer *)longPress {
if (_longPress == nil) {
_longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
}
return _longPress;
}
- (UIGestureRecognizer *)pinchGesture {
if (_pinchGesture == nil) {
_pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
}
return _pinchGesture;
}
- (UIGestureRecognizer *)panGesture {
if (_panGesture == nil) {
_panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
_panGesture.cancelsTouchesInView = NO;
}
return _panGesture;
}
- (UIGestureRecognizer *)rotationGesture {
if (_rotationGesture == nil) {
_rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
}
return _rotationGesture;
}
- (void)blendView:(UIView *)view forPropName:(NSString *)name propValue:(id)prop { - (void)blendView:(UIView *)view forPropName:(NSString *)name propValue:(id)prop {
if ([name isEqualToString:@"onSingleTap"]) { if ([name isEqualToString:@"onSingleTap"]) {
self.onSingleTap = prop; self.onSingleTap = prop;
[view addGestureRecognizer:self.singleTapGesture];
} else if ([name isEqualToString:@"onDoubleTap"]) { } else if ([name isEqualToString:@"onDoubleTap"]) {
self.onDoubleTap = prop; self.onDoubleTap = prop;
[view removeGestureRecognizer:self.singleTapGesture];
[view addGestureRecognizer:self.singleTapGesture];
[view addGestureRecognizer:self.doubleTapGesture];
[self.singleTapGesture requireGestureRecognizerToFail:self.doubleTapGesture];
} else if ([name isEqualToString:@"onLongPress"]) { } else if ([name isEqualToString:@"onLongPress"]) {
self.onLongPress = prop; self.onLongPress = prop;
[view addGestureRecognizer:self.longPress];
} else if ([name isEqualToString:@"onPinch"]) { } else if ([name isEqualToString:@"onPinch"]) {
self.onPinch = prop; self.onPinch = prop;
[view addGestureRecognizer:self.pinchGesture];
} else if ([name isEqualToString:@"onPan"]) { } else if ([name isEqualToString:@"onPan"]) {
self.onPan = prop; self.onPan = prop;
[view removeGestureRecognizer:self.panGesture];
[view addGestureRecognizer:self.panGesture];
} else if ([name isEqualToString:@"onRotate"]) { } else if ([name isEqualToString:@"onRotate"]) {
self.onRotate = prop; self.onRotate = prop;
[view addGestureRecognizer:self.rotationGesture];
} else if ([name isEqualToString:@"onSwipe"]) { } else if ([name isEqualToString:@"onSwipe"]) {
self.onSwipe = prop; self.onSwipe = prop;
[view removeGestureRecognizer:self.panGesture];
[view addGestureRecognizer:self.panGesture];
} else if ([name isEqualToString:@"onTouchDown"]) { } else if ([name isEqualToString:@"onTouchDown"]) {
self.onTouchDown = prop; self.onTouchDown = prop;
} else if ([name isEqualToString:@"onTouchMove"]) { } else if ([name isEqualToString:@"onTouchMove"]) {
self.onTouchMove = prop; self.onTouchMove = prop;
[view removeGestureRecognizer:self.panGesture];
[view addGestureRecognizer:self.panGesture];
} else if ([name isEqualToString:@"onTouchUp"]) { } else if ([name isEqualToString:@"onTouchUp"]) {
self.onTouchUp = prop; self.onTouchUp = prop;
} else if ([name isEqualToString:@"onTouchCancel"]) { } else if ([name isEqualToString:@"onTouchCancel"]) {