From 81cff6162045e0af81812195367a7a69c7e975bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=8A=B2=E9=B9=8F?= Date: Fri, 17 Jun 2022 15:44:10 +0800 Subject: [PATCH] iOS: implements list drag feature --- doric-iOS/Pod/Classes/Shader/DoricListNode.m | 46 ++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/doric-iOS/Pod/Classes/Shader/DoricListNode.m b/doric-iOS/Pod/Classes/Shader/DoricListNode.m index a0d79315..ad106a94 100644 --- a/doric-iOS/Pod/Classes/Shader/DoricListNode.m +++ b/doric-iOS/Pod/Classes/Shader/DoricListNode.m @@ -59,6 +59,12 @@ @interface DoricListNode () @property(nonatomic, copy) NSString *onScrollFuncId; @property(nonatomic, copy) NSString *onScrollEndFuncId; @property(nonatomic, strong) DoricJSDispatcher *jsDispatcher; + +@property(nonatomic, strong) UILongPressGestureRecognizer *longPress; +@property(nonatomic, strong) NSIndexPath *initialDragIndexPath; +@property(nonatomic, strong) NSIndexPath *currentDragIndexPath; +@property(nonatomic, copy) NSString *onDraggingFuncId; +@property(nonatomic, copy) NSString *onDraggedFuncId; @end @implementation DoricListNode @@ -91,9 +97,40 @@ - (UITableView *)build { if (@available(iOS 11, *)) { it.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; } + self.longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)]; + [it addGestureRecognizer:self.longPress]; + [self.longPress setEnabled:NO]; }]; } +-(void)longPressAction:(UILongPressGestureRecognizer *)sender{ + CGPoint locationInView = [sender locationInView:self.view]; + NSIndexPath *indexPath = [self.view indexPathForRowAtPoint:locationInView]; + if (sender.state == UIGestureRecognizerStateBegan) { + if (indexPath != nil) { + self.initialDragIndexPath = indexPath; + self.currentDragIndexPath = indexPath; + } + } else if (sender.state == UIGestureRecognizerStateChanged) { + if ((indexPath != nil) && (indexPath != self.currentDragIndexPath)) { + NSString *fromValue = self.itemViewIds[@(self.currentDragIndexPath.row)]; + NSString *toValue = self.itemViewIds[@(indexPath.row)]; + self.itemViewIds[@(self.currentDragIndexPath.row)] = toValue; + self.itemViewIds[@(indexPath.row)] = fromValue; + + [self.view moveRowAtIndexPath:self.currentDragIndexPath toIndexPath:indexPath]; + if (self.onDraggingFuncId != nil) { + [self callJSResponse:self.onDraggingFuncId, @(self.currentDragIndexPath.row), @(indexPath.row), nil]; + } + self.currentDragIndexPath = indexPath; + } + } else if (sender.state == UIGestureRecognizerStateEnded) { + if (self.onDraggedFuncId != nil) { + [self callJSResponse:self.onDraggedFuncId, @(self.initialDragIndexPath.row), @(self.currentDragIndexPath.row), nil]; + } + } +} + - (void)blendView:(UITableView *)view forPropName:(NSString *)name propValue:(id)prop { if ([@"scrollable" isEqualToString:name]) { self.view.scrollEnabled = [prop boolValue]; @@ -128,6 +165,13 @@ - (void)blendView:(UITableView *)view forPropName:(NSString *)name propValue:(id dispatch_async(dispatch_get_main_queue(), ^{ [view scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:[prop unsignedIntegerValue] inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO]; }); + } else if ([@"canDrag" isEqualToString:name]) { + bool canDrag = [prop boolValue]; + [self.longPress setEnabled:canDrag]; + } else if ([@"onDragging" isEqualToString:name]) { + self.onDraggingFuncId = prop; + } else if ([@"onDragged" isEqualToString:name]) { + self.onDraggedFuncId = prop; } else { [super blendView:view forPropName:name propValue:prop]; } @@ -485,6 +529,8 @@ - (void)reset { self.loadMoreViewId = nil; self.onScrollFuncId = nil; self.onScrollEndFuncId = nil; + self.onDraggingFuncId = nil; + self.onDraggedFuncId = nil; self.loadMore = NO; }