-[ios] remove drag methods from WXRecyclerComponent

Project: http://git-wip-us.apache.org/repos/asf/incubator-weex/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-weex/commit/c6e1dbe2
Tree: http://git-wip-us.apache.org/repos/asf/incubator-weex/tree/c6e1dbe2
Diff: http://git-wip-us.apache.org/repos/asf/incubator-weex/diff/c6e1dbe2

Branch: refs/heads/0.16-dev
Commit: c6e1dbe260c63ea34a02d31fd6c104099018c9c4
Parents: 62c1270
Author: ximu <ximu...@alibaba-inc.com>
Authored: Tue Aug 1 11:06:41 2017 +0800
Committer: ximu <ximu...@alibaba-inc.com>
Committed: Tue Aug 1 11:06:41 2017 +0800

----------------------------------------------------------------------
 .../Component/Recycler/WXRecyclerComponent.m    | 226 +++----------------
 1 file changed, 30 insertions(+), 196 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/c6e1dbe2/ios/sdk/WeexSDK/Sources/Component/Recycler/WXRecyclerComponent.m
----------------------------------------------------------------------
diff --git a/ios/sdk/WeexSDK/Sources/Component/Recycler/WXRecyclerComponent.m 
b/ios/sdk/WeexSDK/Sources/Component/Recycler/WXRecyclerComponent.m
index 523bd83..1164472 100644
--- a/ios/sdk/WeexSDK/Sources/Component/Recycler/WXRecyclerComponent.m
+++ b/ios/sdk/WeexSDK/Sources/Component/Recycler/WXRecyclerComponent.m
@@ -32,6 +32,7 @@
 #import "WXMonitor.h"
 #import "NSObject+WXSwizzle.h"
 #import "WXComponent+Events.h"
+#import "WXRecyclerDragController.h"
 
 static NSString * const kCollectionCellReuseIdentifier = @"WXRecyclerCell";
 static NSString * const kCollectionHeaderReuseIdentifier = @"WXRecyclerHeader";
@@ -43,12 +44,6 @@ typedef enum : NSUInteger {
     WXRecyclerLayoutTypeGrid,
 } WXRecyclerLayoutType;
 
-typedef enum : NSUInteger {
-    WXRecyclerDragTriggerNormal,
-    WXRecyclerDragTriggerPan
-} WXRecyclerDragTriggerType;
-
-
 @interface WXCollectionView : UICollectionView
 
 @end
@@ -91,20 +86,12 @@ typedef enum : NSUInteger {
 
 @end
 
-@interface WXRecyclerComponent () <UICollectionViewDataSource, 
UICollectionViewDelegate, WXMultiColumnLayoutDelegate, 
WXRecyclerUpdateControllerDelegate, WXCellRenderDelegate, 
WXHeaderRenderDelegate>
+@interface WXRecyclerComponent () <UICollectionViewDataSource, 
UICollectionViewDelegate, WXMultiColumnLayoutDelegate, 
WXRecyclerUpdateControllerDelegate, WXCellRenderDelegate, 
WXHeaderRenderDelegate, WXRecyclerDragControllerDelegate>
 
 @property (nonatomic, strong, readonly) WXRecyclerDataController 
*dataController;
 @property (nonatomic, strong, readonly) WXRecyclerUpdateController 
*updateController;
 @property (nonatomic, weak, readonly) UICollectionView *collectionView;
-@property (nonatomic, strong) UILongPressGestureRecognizer *currentLongPress;
-@property (nonatomic, strong) NSIndexPath  *startIndexPath;
-@property (nonatomic, strong) NSIndexPath  *dragingIndexPath;
-@property (nonatomic, strong) NSIndexPath  *targetIndexPath;
-@property (nonatomic, strong) NSMutableArray *excludedAry;
-@property (nonatomic, strong) UICollectionViewCell *dragingCell;
-@property (nonatomic, assign) BOOL isDragable;
-@property (nonatomic, assign) BOOL isDragAnchor;
-@property (nonatomic, assign) WXRecyclerDragTriggerType dragTriggerType;
+@property (nonatomic, strong) WXRecyclerDragController *dragController;
 
 @end
 
@@ -122,14 +109,6 @@ typedef enum : NSUInteger {
     if (self = [super initWithRef:ref type:type styles:styles 
attributes:attributes events:events weexInstance:weexInstance]) {
         [self _fillPadding];
         
-        if ([attributes[@"draggable"] boolValue]) {
-            if([attributes[@"dragTriggerType"]  isEqual: @"pan"]){
-                _dragTriggerType = WXRecyclerDragTriggerPan;
-            }
-            _isDragable = YES;
-        }else{
-            _isDragable = NO;
-        }
         
         if ([type isEqualToString:@"waterfall"] || (attributes[@"layout"] && 
[attributes[@"layout"] isEqualToString:@"multi-column"])) {
             // TODO: abstraction
@@ -150,6 +129,17 @@ typedef enum : NSUInteger {
         _updateController = [WXRecyclerUpdateController new];
         _updateController.delegate = self;
         [self fixFlicker];
+        
+        _dragController = [WXRecyclerDragController new];
+        _dragController.delegate = self;
+        if ([attributes[@"draggable"] boolValue]) {
+            if([attributes[@"dragTriggerType"]  isEqual: @"pan"]){
+                _dragController.dragTriggerType = WXRecyclerDragTriggerPan;
+            }
+            _dragController.isDragable = YES;
+        }else{
+            _dragController.isDragable = NO;
+        }
     }
     
     return self;
@@ -181,20 +171,8 @@ typedef enum : NSUInteger {
     [_collectionView registerClass:[WXCollectionViewCell class] 
forCellWithReuseIdentifier:kCollectionCellReuseIdentifier];
     [_collectionView registerClass:[UICollectionReusableView class] 
forSupplementaryViewOfKind:kCollectionSupplementaryViewKindHeader 
withReuseIdentifier:kCollectionHeaderReuseIdentifier];
     
-    _isDragAnchor = NO;
-    
-    _excludedAry = [[NSMutableArray alloc] init];
-    [_collectionView registerClass:[WXCollectionViewCell class] 
forCellWithReuseIdentifier:kCollectionCellReuseIdentifier];
-    [_collectionView registerClass:[UICollectionReusableView class] 
forSupplementaryViewOfKind:kCollectionSupplementaryViewKindHeader 
withReuseIdentifier:kCollectionHeaderReuseIdentifier];
-    
-    _currentLongPress = [[UILongPressGestureRecognizer alloc] 
initWithTarget:self action:@selector(longPressMethod:)];
-    _currentLongPress.minimumPressDuration = 0.3f;
-    [_collectionView addGestureRecognizer:_currentLongPress];
-    
-    _dragingCell = [[WXCollectionViewCell alloc] initWithFrame:CGRectMake(0, 
0, 100, 100/2.0f)];
-    _dragingCell.hidden = true;
-    [_collectionView addSubview:_dragingCell];
-    
+    _dragController.dragingCell = [[WXCollectionViewCell alloc] 
initWithFrame:CGRectMake(0, 0, 100, 100/2.0f)];
+    _dragController.collectionView = _collectionView;
     
     [self performUpdatesWithCompletion:^(BOOL finished) {
         
@@ -220,11 +198,11 @@ typedef enum : NSUInteger {
         
         if ([attributes[@"draggable"] boolValue]) {
             if([attributes[@"dragTriggerType"]  isEqual: @"pan"]){
-                _dragTriggerType = WXRecyclerDragTriggerPan;
+                _dragController.dragTriggerType = WXRecyclerDragTriggerPan;
             }
-            _isDragable = YES;
+            _dragController.isDragable = YES;
         }else{
-            _isDragable = NO;
+            _dragController.isDragable = NO;
         }
         
         if (attributes[@"columnWidth"]) {
@@ -403,7 +381,7 @@ typedef enum : NSUInteger {
     
     cellView.wx_component = contentView.wx_component;
     
-    [self goThroughAnchor:cellView.wx_component indexPath:indexPath];
+    [self.dragController goThroughAnchor:cellView.wx_component 
indexPath:indexPath];
     
     if (contentView.superview == cellView.contentView) {
         return cellView;
@@ -707,164 +685,20 @@ typedef enum : NSUInteger {
     });
 }
 
-#pragma mark - dragMethod
-- (void)longPressMethod:(UILongPressGestureRecognizer*)gesture
-{
-    if (_isDragable) {
-        switch (gesture.state) {
-            case UIGestureRecognizerStateBegan:
-                [self dragBegin:gesture];
-                break;
-            case UIGestureRecognizerStateChanged:
-                [self dragChanged:gesture];
-                break;
-            case UIGestureRecognizerStateEnded:
-                [self dragEnd:gesture];
-                break;
-            default:
-                break;
-        }
-    }
-}
+#define mark dragControllerDelegate
 
-- (void)dragBegin:(UILongPressGestureRecognizer *)gesture{
-    
-    CGPoint point = [gesture locationInView:_collectionView];
-    
-    _startIndexPath = [self getDragingIndexPathWithPoint:point];
-    if (!_startIndexPath) {
-        return;
+- (void)updateDataSource{
+    NSMutableArray *oldComponents = [[NSMutableArray alloc] 
initWithArray:self.dataController.sections[self.dragController.startIndexPath.section].cellComponents];
+    if(oldComponents.count > 1){
+        WXCellComponent *startComponent = 
self.dataController.sections[self.dragController.startIndexPath.section].cellComponents[self.dragController.startIndexPath.item];
+        [oldComponents removeObject:startComponent];
+        [oldComponents insertObject:startComponent 
atIndex:self.dragController.targetIndexPath.item];
+        
self.dataController.sections[self.dragController.startIndexPath.section].cellComponents
 = oldComponents;
     }
-    
-    [self fireEvent:@"dragstart" params:@{@"fromIndex":[NSString 
stringWithFormat:@"%ld",(long)_startIndexPath.row]}];
-    
-    _dragingIndexPath = [self getDragingIndexPathWithPoint:point];
-    if (!_dragingIndexPath) {
-        return;
-    }
-    
-    [_collectionView bringSubviewToFront:_dragingCell];
-    _dragingCell.frame = [_collectionView 
cellForItemAtIndexPath:_dragingIndexPath].frame;
-    _dragingCell.hidden = false;
-    [UIView animateWithDuration:0.3 animations:^{
-        [_dragingCell setTransform:CGAffineTransformMakeScale(1.2, 1.2)];
-    }];
 }
 
-- (void)dragChanged:(UILongPressGestureRecognizer *)gesture{
-    
-    if (!_startIndexPath) {
-        return;
-    }
-    CGPoint point = [gesture locationInView:_collectionView];
-    _dragingCell.center = point;
-    _targetIndexPath = [self getTargetIndexPathWithPoint:point];
-    
-    if (_targetIndexPath && _dragingIndexPath && (_targetIndexPath.section == 
_startIndexPath.section)){
-        [_collectionView moveItemAtIndexPath:_dragingIndexPath 
toIndexPath:_targetIndexPath];
-        _dragingIndexPath = _targetIndexPath;
-    }
-}
-
-- (void)dragEnd:(UILongPressGestureRecognizer *)gesture{
-    
-    if (!_startIndexPath || !_dragingIndexPath) {
-        return;
-    }
-    
-    [self fireEvent:@"dragend" params:@{@"toIndex":[NSString 
stringWithFormat:@"%ld",(long)_dragingIndexPath.row],@"fromIndex":[NSString 
stringWithFormat:@"%ld",(long)_startIndexPath.row]}];
-    
-    CGRect endFrame = [_collectionView 
cellForItemAtIndexPath:_dragingIndexPath].frame;
-    
-    __weak typeof(self) weakSelf = self;
-    [UIView animateWithDuration:0.3 animations:^{
-        [weakSelf.dragingCell setTransform:CGAffineTransformMakeScale(1.0, 
1.0)];
-        weakSelf.dragingCell.frame = endFrame;
-    } completion:^(BOOL finished) {
-        weakSelf.dragingCell.hidden = YES;
-        NSMutableArray *oldComponents = [[NSMutableArray alloc] 
initWithArray:weakSelf.dataController.sections[weakSelf.startIndexPath.section].cellComponents];
-        if(oldComponents.count > 1){
-            WXCellComponent *startComponent = 
weakSelf.dataController.sections[weakSelf.startIndexPath.section].cellComponents[weakSelf.startIndexPath.item];
-            [oldComponents removeObject:startComponent];
-            [oldComponents insertObject:startComponent 
atIndex:weakSelf.targetIndexPath.item];
-            
weakSelf.dataController.sections[weakSelf.startIndexPath.section].cellComponents
 = oldComponents;
-        }
-    }];
-}
-
-- (NSIndexPath *)getDragingIndexPathWithPoint:(CGPoint)point{
-    NSIndexPath *dragingIndexPath = nil;
-    for (NSIndexPath *indexPath in [_collectionView 
indexPathsForVisibleItems]){
-        if (CGRectContainsPoint([_collectionView 
cellForItemAtIndexPath:indexPath].frame,point)) {
-            dragingIndexPath = indexPath;
-            break;
-        }
-    }
-    
-    BOOL isExcluded = NO;
-    if (dragingIndexPath) {
-        for (NSIndexPath *indexPath in _excludedAry) {
-            if (indexPath.row == dragingIndexPath.row) {
-                isExcluded = YES;
-            }
-        }
-    }
-    return isExcluded?nil:dragingIndexPath;
-}
-
-- (NSIndexPath *)getTargetIndexPathWithPoint:(CGPoint)point{
-    NSIndexPath *targetIndexPath = nil;
-    for (NSIndexPath *indexPath in _collectionView.indexPathsForVisibleItems) {
-        if (CGRectContainsPoint([_collectionView 
cellForItemAtIndexPath:indexPath].frame, point)) {
-            targetIndexPath = indexPath;
-        }
-    }
-    return targetIndexPath;
-}
-
-
-- (void)goThroughAnchor:(WXComponent *)wxComponent indexPath:(NSIndexPath 
*)indexPath{
-    if (wxComponent.attributes && [wxComponent.attributes[@"dragExcluded"] 
boolValue]){
-        [_excludedAry addObject:indexPath];
-        NSSet *set = [NSSet setWithArray:_excludedAry];
-        [_excludedAry removeAllObjects];
-        [_excludedAry addObjectsFromArray:[set allObjects]];
-    }
-    
-    //遍历获取锚点
-    NSMutableArray *subviewComponents = [[NSMutableArray alloc] init];
-    [subviewComponents addObjectsFromArray:wxComponent.subcomponents];
-    WXComponent *anchorComponent;
-    for (int i = 0 ; i < subviewComponents.count ; i++){
-        WXComponent *compoent = subviewComponents[i];
-        if (compoent.attributes[@"dragAnchor"]) {
-            anchorComponent = compoent;
-            _isDragAnchor = YES;
-            break;
-        }
-        
-        if (compoent.subcomponents && compoent.subcomponents.count && 
compoent.subcomponents.count > 0) {
-            [subviewComponents addObjectsFromArray:compoent.subcomponents];
-        }
-    }
-    
-    if (anchorComponent) {
-        //去除全局UILongPressGestureRecognizer手势
-        if (_currentLongPress) {
-            [self.collectionView removeGestureRecognizer:_currentLongPress];
-            _currentLongPress = nil;
-        }
-        
-        //添加锚点的手势
-        if (_dragTriggerType == WXRecyclerDragTriggerPan) {
-            UIPanGestureRecognizer *panGestureRecognizer = 
[[UIPanGestureRecognizer alloc] initWithTarget:self 
action:@selector(longPressMethod:)];
-            [anchorComponent.view addGestureRecognizer:panGestureRecognizer];
-            
-        }else{
-            UILongPressGestureRecognizer *longPressGestureRecognizer = 
[[UILongPressGestureRecognizer alloc] initWithTarget:self 
action:@selector(longPressMethod:)];
-            [anchorComponent.view 
addGestureRecognizer:longPressGestureRecognizer];
-        }
-    }
+- (void)dragFireEvent:(NSString *)eventName params:(NSDictionary *)params{
+    [self fireEvent:eventName params:params];
 }
 
 - (void)fixedFlickerSelector

Reply via email to