iOS DeveloperiOS 日常视图控件

拖动并交换两个View位置的一种思路

2018-06-29  本文已影响173人  Calabash_Boy

父老乡亲们,好久不见,想死你们了~
世界杯正当火热,每天我家天台都熙熙攘攘,人来人往,好生热闹,而我看好的球队不出所料,都一一回家了...


宝宝心里苦

闲话于此,公司最近要做一个需求,客户预定酒店的时候可以自己决定来入住哪个房间,并且和谁一个房间🤩😏,想想果然有些小激动,demo实现的效果如下:


drag.gif

在网上查询了一下,大部分都是tableViewCell的交换,并不能很好的满足这个需求,因此尝试自己写了一种思路;
Demo地址:https://github.com/Calabash-Boy/CB_DragView
如果喜欢,烦请各位大佬赏个小星星😏~

思路如下:
关键代码如下:
- (void)longPressGestureRecognized:(UILongPressGestureRecognizer *)longPress {
    
    //首先校验当前的Menu是否可以拖动
    CB_MenuView *menu = (CB_MenuView *)longPress.view;
    UILabel *dataLabel = [menu viewWithTag:110];
    if (!dataLabel.text.length) return;
    
    //当前手指的位置
    CGPoint currentPoint = [longPress locationInView:self];

    if (longPress.state == UIGestureRecognizerStateBegan) {
        //记录刚开始的时候View的位置
        //一定要把menu的坐标转换到自己的坐标系上
        CGRect rect = [menu.superview convertRect:menu.frame toView:self]; 
        self.fromCenter = CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect));
        self.fromView = menu;
        self.fromIndex = [_menuViewArray indexOfObject:menu];
        [self beginDragAnimation];
    }
    else if (longPress.state == UIGestureRecognizerStateChanged) {
        //拖动过程中移动动画View
        [UIView animateWithDuration:0.1 animations:^{
            CGPoint screenshotViewCenter = self.screenshotView.center;
            screenshotViewCenter.y = currentPoint.y;
            self.screenshotView.center = screenshotViewCenter;
        }];
    
    }
    else {
        self.toView = nil;
        CGRect rect = CGRectZero;
        //拖动结束 查看结束的位置是否处于某个Menu中 如果是 则交换位置
        for (CB_MenuView *menu in _menuViewArray) {
            rect = [menu.superview convertRect:menu.frame toView:self];
            if (CGRectContainsPoint(rect, currentPoint)) {
                self.toView = menu;
                self.toIndex = [_menuViewArray indexOfObject:menu];
                break;
            }
        }
        
        [self endDragAnimation];
    }
}
- (void)beginDragAnimation {
    if (self.screenshotView) {
        [self.screenshotView removeFromSuperview];
        self.screenshotView = nil;
    }
    //产生拖动的截图 隐藏原有截图
    UIView *screenshotView = [self.fromView screenshotViewWithShadowOpacity:0.3 shadowColor:[UIColor blackColor]];
    [self addSubview:screenshotView];
    [self bringSubviewToFront:screenshotView];
    self.screenshotView = screenshotView;
    self.screenshotView.center = self.fromCenter;
    self.fromView.hidden = YES;
    self.screenshotView.transform = CGAffineTransformMakeScale(1.03, 1.03);
}
- (void)endDragAnimation {
    if (self.toView) {
        CGRect rect = [self.toView.superview convertRect:self.toView.frame toView:self];
        self.toCenter = CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect));
        //截图 然后交换位置
        UIView *screenshotToView = [self.toView screenshotViewWithShadowOpacity:0.3 shadowColor:[UIColor blackColor]];
        [self addSubview:screenshotToView];
        [self insertSubview:screenshotToView belowSubview:self.screenshotView];
        self.toView.hidden = YES;
        screenshotToView.center = self.toCenter;
        screenshotToView.transform = CGAffineTransformMakeScale(1.03, 1.03);
        
        //此时去交换数据 更新界面
        [self exchangeData];

        //交换位置
        [UIView animateWithDuration:0.3 animations:^{
            //toView -> fromView
            screenshotToView.center = self.fromCenter;
            screenshotToView.transform = CGAffineTransformIdentity;
            
            //fromView -> toView
            self.screenshotView.transform = CGAffineTransformIdentity;
            self.screenshotView.center = self.toCenter;
            
        } completion:^(BOOL finished) {
            [screenshotToView removeFromSuperview];
            self.toView.hidden = NO;
            
            [self.screenshotView removeFromSuperview];
            self.screenshotView = nil;
            self.fromView.hidden = NO;
        }];
    } else {
        //不在任何一个位置 返回原处
        [UIView animateWithDuration:0.3 animations:^{
            self.screenshotView.transform = CGAffineTransformIdentity;
            self.screenshotView.center = self.fromCenter;
        } completion:^(BOOL finished) {
            [self.screenshotView removeFromSuperview];
            self.screenshotView = nil;
            self.fromView.hidden = NO;
            
        }];
    }
}
多说几句:
上一篇下一篇

猜你喜欢

热点阅读