iOS: implement flowlayout drag

This commit is contained in:
王劲鹏 2022-08-25 14:47:11 +08:00 committed by osborn
parent 9ccce783cf
commit 3a28941c04

View File

@ -173,6 +173,13 @@ @interface DoricFlowLayoutNode () <UICollectionViewDataSource, UICollectionViewD
@property(nonatomic, copy) NSString *onScrollEndFuncId;
@property(nonatomic, strong) DoricJSDispatcher *jsDispatcher;
@property(nonatomic, assign) NSInteger loadAnchor;
@property(nonatomic, strong) UILongPressGestureRecognizer *longPress;
@property(nonatomic, strong) NSIndexPath *initialDragIndexPath;
@property(nonatomic, strong) NSIndexPath *currentDragIndexPath;
@property(nonatomic, copy) NSString *beforeDraggingFuncId;
@property(nonatomic, copy) NSString *onDraggingFuncId;
@property(nonatomic, copy) NSString *onDraggedFuncId;
@end
@implementation DoricFlowLayoutNode
@ -202,9 +209,44 @@ - (UICollectionView *)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 indexPathForItemAtPoint:locationInView];
if (sender.state == UIGestureRecognizerStateBegan) {
if (indexPath != nil) {
self.initialDragIndexPath = indexPath;
self.currentDragIndexPath = indexPath;
if (self.beforeDraggingFuncId != nil) {
[self callJSResponse:self.beforeDraggingFuncId, @(indexPath.row), nil];
}
}
} 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 moveItemAtIndexPath: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:(UICollectionView *)view forPropName:(NSString *)name propValue:(id)prop {
if ([@"scrollable" isEqualToString:name]) {
self.view.scrollEnabled = [prop boolValue];
@ -249,6 +291,15 @@ - (void)blendView:(UICollectionView *)view forPropName:(NSString *)name propValu
self.onScrollFuncId = prop;
} else if ([@"onScrollEnd" isEqualToString:name]) {
self.onScrollEndFuncId = prop;
} else if ([@"canDrag" isEqualToString:name]) {
bool canDrag = [prop boolValue];
[self.longPress setEnabled:canDrag];
} else if ([@"beforeDragging" isEqualToString:name]) {
self.beforeDraggingFuncId = prop;
} else if ([@"onDragging" isEqualToString:name]) {
self.onDraggingFuncId = prop;
} else if ([@"onDragged" isEqualToString:name]) {
self.onDraggedFuncId = prop;
} else {
[super blendView:view forPropName:name propValue:prop];
}
@ -542,6 +593,9 @@ - (void)reset {
self.loadMoreViewId = nil;
self.onScrollFuncId = nil;
self.onScrollEndFuncId = nil;
self.beforeDraggingFuncId = nil;
self.onDraggingFuncId = nil;
self.onDraggedFuncId = nil;
self.loadMore = NO;
}