iOS: implement touch api

This commit is contained in:
王劲鹏 2021-09-22 15:30:34 +08:00 committed by osborn
parent 8ad5c06ff7
commit 0421b8258f
2 changed files with 76 additions and 1 deletions

View File

@ -26,6 +26,11 @@ NS_ASSUME_NONNULL_BEGIN
@interface DoricGestureContainerNode : DoricStackNode @interface DoricGestureContainerNode : DoricStackNode
@property(nonatomic, strong) NSString *onTouchDown;
@property(nonatomic, strong) NSString *onTouchMove;
@property(nonatomic, strong) NSString *onTouchUp;
@property(nonatomic, strong) NSString *onTouchCancel;
@end @end
NS_ASSUME_NONNULL_END NS_ASSUME_NONNULL_END

View File

@ -20,8 +20,66 @@
// Created by jingpeng.wang on 2021/09/17. // Created by jingpeng.wang on 2021/09/17.
// //
#import "DoricExtensions.h"
#import "DoricGestureContainerNode.h" #import "DoricGestureContainerNode.h"
@interface DoricGestureView : UIView
@property(nonatomic, weak) DoricGestureContainerNode *node;
@end
@implementation DoricGestureView
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
[touches enumerateObjectsUsingBlock:^(UITouch * _Nonnull obj, BOOL * _Nonnull stop) {
if (self.node.onTouchDown) {
CGPoint currentLocation = [obj locationInView:self];
[self.node callJSResponse: self.node.onTouchDown, @(currentLocation.x), @(currentLocation.y), nil];
*stop = YES;
}
}];
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[super touchesMoved:touches withEvent:event];
[touches enumerateObjectsUsingBlock:^(UITouch * _Nonnull obj, BOOL * _Nonnull stop) {
if (self.node.onTouchMove) {
CGPoint currentLocation = [obj locationInView:self];
[self.node callJSResponse: self.node.onTouchMove, @(currentLocation.x), @(currentLocation.y), nil];
*stop = YES;
}
}];
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[super touchesEnded:touches withEvent:event];
[touches enumerateObjectsUsingBlock:^(UITouch * _Nonnull obj, BOOL * _Nonnull stop) {
if (self.node.onTouchUp) {
CGPoint currentLocation = [obj locationInView:self];
[self.node callJSResponse: self.node.onTouchUp, @(currentLocation.x), @(currentLocation.y), nil];
*stop = YES;
}
}];
}
- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[super touchesCancelled:touches withEvent:event];
[touches enumerateObjectsUsingBlock:^(UITouch * _Nonnull obj, BOOL * _Nonnull stop) {
if (self.node.onTouchCancel) {
CGPoint currentLocation = [obj locationInView:self];
[self.node callJSResponse: self.node.onTouchCancel, @(currentLocation.x), @(currentLocation.y), nil];
*stop = YES;
}
}];
}
@end
@interface DoricGestureContainerNode() @interface DoricGestureContainerNode()
@ -45,7 +103,10 @@ @interface DoricGestureContainerNode()
@implementation DoricGestureContainerNode @implementation DoricGestureContainerNode
- (UIView *)build { - (UIView *)build {
UIView *stackView = [super build]; UIView *stackView = [[DoricGestureView new] also:^(DoricGestureView *it) {
it.doricLayout.layoutType = DoricStack;
it.node = self;
}];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapAction:)]; UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapAction:)];
[stackView addGestureRecognizer:singleTap]; [stackView addGestureRecognizer:singleTap];
@ -63,6 +124,7 @@ - (UIView *)build {
[stackView addGestureRecognizer:pinchGesture]; [stackView addGestureRecognizer:pinchGesture];
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)]; UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
panGesture.cancelsTouchesInView = NO;
[stackView addGestureRecognizer:panGesture]; [stackView addGestureRecognizer:panGesture];
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)]; UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
@ -159,6 +221,14 @@ - (void)blendView:(UIView *)view forPropName:(NSString *)name propValue:(id)prop
self.onRotate = prop; self.onRotate = prop;
} else if ([name isEqualToString:@"onSwipe"]) { } else if ([name isEqualToString:@"onSwipe"]) {
self.onSwipe = prop; self.onSwipe = prop;
} else if ([name isEqualToString:@"onTouchDown"]) {
self.onTouchDown = prop;
} else if ([name isEqualToString:@"onTouchMove"]) {
self.onTouchMove = prop;
} else if ([name isEqualToString:@"onTouchUp"]) {
self.onTouchUp = prop;
} else if ([name isEqualToString:@"onTouchCancel"]) {
self.onTouchCancel = prop;
} else { } else {
[super blendView:view forPropName:name propValue:prop]; [super blendView:view forPropName:name propValue:prop];
} }