This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
Doric/doric-iOS/Pod/Classes/Shader/DoricGestureContainerNode.m

317 lines
11 KiB
Mathematica
Raw Normal View History

2021-09-22 10:28:13 +08:00
/*
* Copyright [2021] [Doric.Pub]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//
// DoricGestureContainerNode.m
// Doric
//
// Created by jingpeng.wang on 2021/09/17.
//
2021-09-22 15:30:34 +08:00
#import "DoricExtensions.h"
2021-09-22 10:28:13 +08:00
#import "DoricGestureContainerNode.h"
2021-09-22 15:30:34 +08:00
@interface DoricGestureView : UIView
2021-09-22 15:30:34 +08:00
@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) {
2021-09-22 15:30:34 +08:00
if (self.node.onTouchDown) {
CGPoint currentLocation = [obj locationInView:self];
[self.node callJSResponse:self.node.onTouchDown, @{@"x": @(currentLocation.x), @"y": @(currentLocation.y)}, nil];
2021-09-22 15:30:34 +08:00
*stop = YES;
}
}];
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[super touchesMoved:touches withEvent:event];
[touches enumerateObjectsUsingBlock:^(UITouch *_Nonnull obj, BOOL *_Nonnull stop) {
2021-09-22 15:30:34 +08:00
if (self.node.onTouchMove) {
CGPoint currentLocation = [obj locationInView:self];
2021-09-23 11:05:40 +08:00
if (!self.node.jsDispatcher) {
self.node.jsDispatcher = [DoricJSDispatcher new];
}
__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];
2021-09-23 11:05:40 +08:00
}];
2021-09-22 15:30:34 +08:00
*stop = YES;
}
}];
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[super touchesEnded:touches withEvent:event];
[touches enumerateObjectsUsingBlock:^(UITouch *_Nonnull obj, BOOL *_Nonnull stop) {
2021-09-22 15:30:34 +08:00
if (self.node.onTouchUp) {
CGPoint currentLocation = [obj locationInView:self];
[self.node callJSResponse:self.node.onTouchUp, @{@"x": @(currentLocation.x), @"y": @(currentLocation.y)}, nil];
2021-09-22 15:30:34 +08:00
*stop = YES;
}
}];
}
- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[super touchesCancelled:touches withEvent:event];
[touches enumerateObjectsUsingBlock:^(UITouch *_Nonnull obj, BOOL *_Nonnull stop) {
2021-09-22 15:30:34 +08:00
if (self.node.onTouchCancel) {
CGPoint currentLocation = [obj locationInView:self];
[self.node callJSResponse:self.node.onTouchCancel, @{@"x": @(currentLocation.x), @"y": @(currentLocation.y)}, nil];
2021-09-22 15:30:34 +08:00
*stop = YES;
}
}];
}
@end
2021-09-22 10:28:13 +08:00
@interface DoricGestureContainerNode ()
2021-09-22 10:28:13 +08:00
@property(nonatomic, strong) NSString *onSingleTap;
@property(nonatomic, strong) NSString *onDoubleTap;
@property(nonatomic, strong) NSString *onLongPress;
@property(nonatomic, strong) NSString *onPinch;
@property(nonatomic, strong) NSString *onPan;
@property(nonatomic, strong) NSString *onRotate;
@property(nonatomic, strong) NSString *onSwipe;
@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;
2021-09-22 10:28:13 +08:00
@end
#define SWIPE_UP_THRESHOLD -1000.0f
#define SWIPE_DOWN_THRESHOLD 1000.0f
#define SWIPE_LEFT_THRESHOLD -1000.0f
#define SWIPE_RIGHT_THRESHOLD 1000.0f
@implementation DoricGestureContainerNode
- (UIView *)build {
2021-09-22 15:30:34 +08:00
UIView *stackView = [[DoricGestureView new] also:^(DoricGestureView *it) {
it.doricLayout.layoutType = DoricStack;
it.node = self;
}];
2021-09-22 10:28:13 +08:00
return stackView;
}
- (void)singleTapAction:(UITapGestureRecognizer *)sender {
2021-09-23 16:06:07 +08:00
if (self.onSingleTap) {
2021-09-22 10:28:13 +08:00
[self callJSResponse:self.onSingleTap, nil];
2021-09-23 16:06:07 +08:00
}
2021-09-22 10:28:13 +08:00
}
- (void)doubleTapAction:(UITapGestureRecognizer *)sender {
2021-09-23 16:06:07 +08:00
if (self.onDoubleTap) {
2021-09-22 10:28:13 +08:00
[self callJSResponse:self.onDoubleTap, nil];
2021-09-23 16:06:07 +08:00
}
2021-09-22 10:28:13 +08:00
}
- (void)longPressAction:(UILongPressGestureRecognizer *)sender {
2021-09-22 10:28:13 +08:00
if (sender.state == UIGestureRecognizerStateBegan) {
2021-09-23 16:06:07 +08:00
if (self.onLongPress) {
2021-09-22 10:28:13 +08:00
[self callJSResponse:self.onLongPress, nil];
2021-09-23 16:06:07 +08:00
}
2021-09-22 10:28:13 +08:00
}
}
- (void)pinchAction:(UIPinchGestureRecognizer *)sender {
2021-09-23 11:05:40 +08:00
if (self.onPinch) {
if (!self.jsDispatcher) {
self.jsDispatcher = [DoricJSDispatcher new];
}
__weak typeof(self) __self = self;
[self.jsDispatcher dispatch:^DoricAsyncResult * {
__strong typeof(__self) self = __self;
return [self callJSResponse:self.onPinch, @(sender.scale), nil];
}];
}
2021-09-22 10:28:13 +08:00
}
- (void)panAction:(UIPanGestureRecognizer *)sender {
if (sender.state == UIGestureRecognizerStateBegan) {
2021-09-22 10:28:13 +08:00
self.startPanLocation = [sender locationInView:self.view];
}
CGPoint currentLocation = [sender locationInView:self.view];
2021-09-22 10:28:13 +08:00
CGFloat dx = self.startPanLocation.x - currentLocation.x;
CGFloat dy = self.startPanLocation.y - currentLocation.y;
2021-09-22 10:28:13 +08:00
self.startPanLocation = currentLocation;
2021-09-23 11:05:40 +08:00
if (self.onPan) {
if (!self.jsDispatcher) {
self.jsDispatcher = [DoricJSDispatcher new];
2021-09-22 10:28:13 +08:00
}
2021-09-23 11:05:40 +08:00
__weak typeof(self) __self = self;
[self.jsDispatcher dispatch:^DoricAsyncResult * {
__strong typeof(__self) self = __self;
return [self callJSResponse:self.onPan, @(dx), @(dy), nil];
}];
}
2021-09-23 11:05:40 +08:00
// detect the swipe gesture
if (sender.state == UIGestureRecognizerStateEnded) {
CGPoint vel = [sender velocityInView:sender.view];
if (vel.x < SWIPE_LEFT_THRESHOLD) {
2021-09-23 16:06:07 +08:00
if (self.onSwipe) {
2021-09-23 11:05:40 +08:00
[self callJSResponse:self.onSwipe, @(0), nil];
2021-09-23 16:06:07 +08:00
}
2021-09-23 11:05:40 +08:00
} else if (vel.x > SWIPE_RIGHT_THRESHOLD) {
2021-09-23 16:06:07 +08:00
if (self.onSwipe) {
2021-09-23 11:05:40 +08:00
[self callJSResponse:self.onSwipe, @(1), nil];
2021-09-23 16:06:07 +08:00
}
2021-09-23 11:05:40 +08:00
} else if (vel.y < SWIPE_UP_THRESHOLD) {
2021-09-23 16:06:07 +08:00
if (self.onSwipe) {
2021-09-23 11:05:40 +08:00
[self callJSResponse:self.onSwipe, @(2), nil];
2021-09-23 16:06:07 +08:00
}
2021-09-23 11:05:40 +08:00
} else if (vel.y > SWIPE_DOWN_THRESHOLD) {
2021-09-23 16:06:07 +08:00
if (self.onSwipe) {
2021-09-23 11:05:40 +08:00
[self callJSResponse:self.onSwipe, @(3), nil];
2021-09-23 16:06:07 +08:00
}
2021-09-23 11:05:40 +08:00
} else {
// TODO:
// Here, the user lifted the finger/fingers but didn't swipe.
// If you need you can implement a snapping behaviour, where based on the location of your targetView,
// you focus back on the targetView or on some next view.
// It's your call
}
}
2021-09-22 10:28:13 +08:00
}
- (void)rotationAction:(UIRotationGestureRecognizer *)sender {
if (sender.state == UIGestureRecognizerStateBegan) {
2021-09-22 10:28:13 +08:00
self.startRotationDegree = sender.rotation;
}
2021-09-22 10:28:13 +08:00
CGFloat diffRotation = sender.rotation - self.startRotationDegree;
self.startRotationDegree = sender.rotation;
2021-09-23 11:05:40 +08:00
if (self.onRotate) {
if (!self.jsDispatcher) {
self.jsDispatcher = [DoricJSDispatcher new];
}
__weak typeof(self) __self = self;
[self.jsDispatcher dispatch:^DoricAsyncResult * {
__strong typeof(__self) self = __self;
return [self callJSResponse:self.onRotate, @(diffRotation), nil];
}];
}
2021-09-22 10:28:13 +08:00
}
- (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;
}
2021-09-22 10:28:13 +08:00
- (void)blendView:(UIView *)view forPropName:(NSString *)name propValue:(id)prop {
if ([name isEqualToString:@"onSingleTap"]) {
self.onSingleTap = prop;
[view addGestureRecognizer:self.singleTapGesture];
2021-09-22 10:28:13 +08:00
} 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];
2021-09-22 10:28:13 +08:00
} else if ([name isEqualToString:@"onLongPress"]) {
self.onLongPress = prop;
[view addGestureRecognizer:self.longPress];
2021-09-22 10:28:13 +08:00
} else if ([name isEqualToString:@"onPinch"]) {
self.onPinch = prop;
[view addGestureRecognizer:self.pinchGesture];
2021-09-22 10:28:13 +08:00
} else if ([name isEqualToString:@"onPan"]) {
self.onPan = prop;
[view removeGestureRecognizer:self.panGesture];
[view addGestureRecognizer:self.panGesture];
2021-09-22 10:28:13 +08:00
} else if ([name isEqualToString:@"onRotate"]) {
self.onRotate = prop;
[view addGestureRecognizer:self.rotationGesture];
2021-09-22 10:28:13 +08:00
} else if ([name isEqualToString:@"onSwipe"]) {
self.onSwipe = prop;
[view removeGestureRecognizer:self.panGesture];
[view addGestureRecognizer:self.panGesture];
2021-09-22 15:30:34 +08:00
} else if ([name isEqualToString:@"onTouchDown"]) {
self.onTouchDown = prop;
} else if ([name isEqualToString:@"onTouchMove"]) {
self.onTouchMove = prop;
[view removeGestureRecognizer:self.panGesture];
[view addGestureRecognizer:self.panGesture];
2021-09-22 15:30:34 +08:00
} else if ([name isEqualToString:@"onTouchUp"]) {
self.onTouchUp = prop;
} else if ([name isEqualToString:@"onTouchCancel"]) {
self.onTouchCancel = prop;
2021-09-22 10:28:13 +08:00
} else {
[super blendView:view forPropName:name propValue:prop];
}
}
@end