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

@ -34,10 +34,10 @@ @implementation DoricGestureView
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
[touches enumerateObjectsUsingBlock:^(UITouch * _Nonnull obj, BOOL * _Nonnull stop) {
[touches enumerateObjectsUsingBlock:^(UITouch *_Nonnull obj, BOOL *_Nonnull stop) {
if (self.node.onTouchDown) {
CGPoint currentLocation = [obj locationInView:self];
[self.node callJSResponse: self.node.onTouchDown, @{@"x": @(currentLocation.x), @"y": @(currentLocation.y)}, nil];
[self.node callJSResponse:self.node.onTouchDown, @{@"x": @(currentLocation.x), @"y": @(currentLocation.y)}, nil];
*stop = YES;
}
}];
@ -46,7 +46,7 @@ - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[super touchesMoved:touches withEvent:event];
[touches enumerateObjectsUsingBlock:^(UITouch * _Nonnull obj, BOOL * _Nonnull stop) {
[touches enumerateObjectsUsingBlock:^(UITouch *_Nonnull obj, BOOL *_Nonnull stop) {
if (self.node.onTouchMove) {
CGPoint currentLocation = [obj locationInView:self];
if (!self.node.jsDispatcher) {
@ -55,7 +55,7 @@ - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
__weak typeof(self) __self = self;
[self.node.jsDispatcher dispatch:^DoricAsyncResult * {
__strong typeof(__self) self = __self;
return [self.node callJSResponse: self.node.onTouchMove, @{@"x": @(currentLocation.x), @"y": @(currentLocation.y)}, nil];
return [self.node callJSResponse:self.node.onTouchMove, @{@"x": @(currentLocation.x), @"y": @(currentLocation.y)}, nil];
}];
*stop = YES;
}
@ -65,10 +65,10 @@ - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[super touchesEnded:touches withEvent:event];
[touches enumerateObjectsUsingBlock:^(UITouch * _Nonnull obj, BOOL * _Nonnull stop) {
[touches enumerateObjectsUsingBlock:^(UITouch *_Nonnull obj, BOOL *_Nonnull stop) {
if (self.node.onTouchUp) {
CGPoint currentLocation = [obj locationInView:self];
[self.node callJSResponse: self.node.onTouchUp, @{@"x": @(currentLocation.x), @"y": @(currentLocation.y)}, nil];
[self.node callJSResponse:self.node.onTouchUp, @{@"x": @(currentLocation.x), @"y": @(currentLocation.y)}, nil];
*stop = YES;
}
}];
@ -77,10 +77,10 @@ - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[super touchesCancelled:touches withEvent:event];
[touches enumerateObjectsUsingBlock:^(UITouch * _Nonnull obj, BOOL * _Nonnull stop) {
[touches enumerateObjectsUsingBlock:^(UITouch *_Nonnull obj, BOOL *_Nonnull stop) {
if (self.node.onTouchCancel) {
CGPoint currentLocation = [obj locationInView:self];
[self.node callJSResponse: self.node.onTouchCancel, @{@"x": @(currentLocation.x), @"y": @(currentLocation.y)}, nil];
[self.node callJSResponse:self.node.onTouchCancel, @{@"x": @(currentLocation.x), @"y": @(currentLocation.y)}, nil];
*stop = YES;
}
}];
@ -88,7 +88,7 @@ - (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
@end
@interface DoricGestureContainerNode()
@interface DoricGestureContainerNode ()
@property(nonatomic, strong) NSString *onSingleTap;
@property(nonatomic, strong) NSString *onDoubleTap;
@ -100,7 +100,12 @@ @interface DoricGestureContainerNode()
@property(nonatomic) CGPoint startPanLocation;
@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
#define SWIPE_UP_THRESHOLD -1000.0f
@ -114,45 +119,22 @@ - (UIView *)build {
it.doricLayout.layoutType = DoricStack;
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;
}
-(void)singleTapAction:(UITapGestureRecognizer *)sender{
- (void)singleTapAction:(UITapGestureRecognizer *)sender {
if (self.onSingleTap) {
[self callJSResponse:self.onSingleTap, nil];
}
}
-(void)doubleTapAction:(UITapGestureRecognizer *)sender{
- (void)doubleTapAction:(UITapGestureRecognizer *)sender {
if (self.onDoubleTap) {
[self callJSResponse:self.onDoubleTap, nil];
}
}
-(void)longPressAction:(UILongPressGestureRecognizer *)sender{
- (void)longPressAction:(UILongPressGestureRecognizer *)sender {
if (sender.state == UIGestureRecognizerStateBegan) {
if (self.onLongPress) {
[self callJSResponse:self.onLongPress, nil];
@ -160,7 +142,7 @@ -(void)longPressAction:(UILongPressGestureRecognizer *)sender{
}
}
-(void)pinchAction:(UIPinchGestureRecognizer *)sender{
- (void)pinchAction:(UIPinchGestureRecognizer *)sender {
if (self.onPinch) {
if (!self.jsDispatcher) {
self.jsDispatcher = [DoricJSDispatcher new];
@ -173,7 +155,7 @@ -(void)pinchAction:(UIPinchGestureRecognizer *)sender{
}
}
-(void)panAction:(UIPanGestureRecognizer *)sender{
- (void)panAction:(UIPanGestureRecognizer *)sender {
if (sender.state == UIGestureRecognizerStateBegan) {
self.startPanLocation = [sender locationInView:self.view];
}
@ -226,7 +208,7 @@ -(void)panAction:(UIPanGestureRecognizer *)sender{
}
}
-(void)rotationAction:(UIRotationGestureRecognizer *)sender{
- (void)rotationAction:(UIRotationGestureRecognizer *)sender {
if (sender.state == UIGestureRecognizerStateBegan) {
self.startRotationDegree = sender.rotation;
}
@ -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 {
if ([name isEqualToString:@"onSingleTap"]) {
self.onSingleTap = prop;
[view addGestureRecognizer:self.singleTapGesture];
} else if ([name isEqualToString:@"onDoubleTap"]) {
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"]) {
self.onLongPress = prop;
[view addGestureRecognizer:self.longPress];
} else if ([name isEqualToString:@"onPinch"]) {
self.onPinch = prop;
[view addGestureRecognizer:self.pinchGesture];
} else if ([name isEqualToString:@"onPan"]) {
self.onPan = prop;
[view removeGestureRecognizer:self.panGesture];
[view addGestureRecognizer:self.panGesture];
} else if ([name isEqualToString:@"onRotate"]) {
self.onRotate = prop;
[view addGestureRecognizer:self.rotationGesture];
} else if ([name isEqualToString:@"onSwipe"]) {
self.onSwipe = prop;
[view removeGestureRecognizer:self.panGesture];
[view addGestureRecognizer:self.panGesture];
} else if ([name isEqualToString:@"onTouchDown"]) {
self.onTouchDown = prop;
} else if ([name isEqualToString:@"onTouchMove"]) {
self.onTouchMove = prop;
[view removeGestureRecognizer:self.panGesture];
[view addGestureRecognizer:self.panGesture];
} else if ([name isEqualToString:@"onTouchUp"]) {
self.onTouchUp = prop;
} else if ([name isEqualToString:@"onTouchCancel"]) {