ios -  在UISCrollview拖动上面的view到另一个view的位置,实现scrollview上面的View自动布局

 裴潇艳 发布于 2022-10-28 12:49

这是长按tableview的头视图的手势方法,代码如下

    UILongPressGestureRecognizer *longPress = (UILongPressGestureRecognizer *)sender;
    
    //获得长按点在scrollview中的坐标
    CGPoint location = [longPress locationInView:self];

    //获取长按点在window中的坐标
    CGPoint inWindow = [longPress locationInView:self.window];
    
    // 拖拽headView的判断
    CGPoint pointOfHeadView = CGPointMake(0, 0);
    UIView *headview;
    
    for (UIView *view in self.listHeaderArray) {
        
        CGPoint point = [self convertPoint:location toView:view];
        
        if ([view pointInside:point withEvent:nil]) {
            pointOfHeadView = point;
            
            headview = view;
        }
        
    }
   
        static UIView *snapView;
        static float offsetXoflistView;
        static float offsetYoflistView;
        static CADisplayLink *link;
        static float rollBeginMaxX;
        static float rollBeginMinX;
    UIGestureRecognizerState state = longPress.state;
        switch (state) {
            case UIGestureRecognizerStateBegan:{
            
                for (UIView *listview in self.arrlists) {
                    
                    if (listview == headview.superview) {
                        
                        _superOFHeadView = listview;
                    }
                    
                }
                
// 截图操作,截取点击所在的listView
                snapView = [self customSnapshoFromView:_superOFHeadView];
                snapView.alpha = 1.0;
// 转化坐标
                __block CGPoint listViewCentreInWindow = [self convertPoint:_superOFHeadView.center toView:self.window];
                
                NSLog(@"listView的window坐标:%f---%f",listViewCentreInWindow.x,listViewCentreInWindow.y);
//计算该点与手势在window上的偏移量
                offsetXoflistView = listViewCentreInWindow.x - inWindow.x;
//                offsetYoflistView = listViewCentreInWindow.y - inWindow.y;
                snapView.center = listViewCentreInWindow;
//手指移动发生的动画
                [self.window addSubview:snapView];
                [UIView animateWithDuration:0.5 animations:^{
                    listViewCentreInWindow.y = inWindow.y;
                    NSLog(@"改变的listView的Y%f",listViewCentreInWindow.y);
                    NSLog(@"listView的中心点在window上的Y:%f",listViewCentreInWindow.y);
                    snapView.center = listViewCentreInWindow;
                    snapView.transform = CGAffineTransformMakeScale(1.0, 1.0);
                    snapView.layer.shadowColor = [UIColor blackColor].CGColor;
                    snapView.layer.shadowOpacity = 0.8f;
                    snapView.layer.shadowRadius = 4.f;
                    
                    snapView.layer.shadowOffset = CGSizeMake(4,4);
                    
                    _superOFHeadView.hidden = YES;

                    
                    
                } completion:^(BOOL finished) {
                    
                    
                }];

//判断截图横向滚动的边界
                if (CGRectGetMaxX(snapView.frame)>ScreenWidth) {
                    
                    rollBeginMaxX = CGRectGetMaxX(snapView.frame);
                    
                    
                }else{
                
                
                    rollBeginMaxX = ScreenWidth;
                }
                if (CGRectGetMinX(snapView.frame)<0) {
                    
                    rollBeginMinX = CGRectGetMinX(snapView.frame);
                    
                }else{
                
                    rollBeginMinX = 0;
                
                }
            }
                break;

            case UIGestureRecognizerStateChanged:{
                
    //  跟随手指移动的截图 offsetXoflistView = listViewCentreInWindow.x - inWindow.x;
                    
                    snapView.center = CGPointMake(inWindow.x+offsetXoflistView, inWindow.y+offsetYoflistView);
                    //判断截图横向滚动的边界
                    if (CGRectGetMaxX(snapView.frame)<=ScreenWidth) {
                        
                        rollBeginMaxX = ScreenWidth;
                        
                        
                    }
                    if (CGRectGetMinX(snapView.frame)>=0) {
                        
                        rollBeginMinX = 0;
                        
                    }
    //  当截图超过屏幕
                    if (CGRectGetMaxX(snapView.frame)>rollBeginMaxX) {
                        //     起始点
                        NSInteger startIndex = [self StartIndexOfView:superOFHeadView singArray:self.arrlists];
                        CGPoint pointOfsnapView = [self.window convertPoint:snapView.center toView:self];
                        
                        //     目标点
                        NSInteger endIndexPoint = [self indexOfPoint:pointOfsnapView withUiview:snapView singArray:self.arrlists];
                        NSLog(@"%f,%f",snapView.center.x,snapView.center.y);
                        NSLog(@"开始下标:::%ld",(long)startIndex);
                        NSLog(@"结束下标:::%ld",(long)endIndexPoint);
                        ////      向后拖动
                        if (startIndex < endIndexPoint && endIndexPoint < _arrlists.count) {
                            
                            UIView *endView = self.arrlists[endIndexPoint];
                            
                              // 把截图转化成scrollView上的坐标
                        CGPoint snapViewInscrollView = [self convertPoint:snapView.center toView:self];
                            snapViewInscrollView.x = endView.yyCenterX;

                            for (NSInteger j = startIndex; j < endIndexPoint; j++) {
                                
                                UIView *singView1 = self.arrlists[j];
                                UIView *singView2 = self.arrlists[j+1];
                                
                                [UIView animateWithDuration:0.5 animations:^{
                                    
                                    singView2.yyCenterX = singView1.yyCenterX;
                                    
                                }];
                            }
                            [self.arrlists removeObject:superOFHeadView];
                            [self.arrlists insertObject:snapView atIndex:endIndexPoint];
                            
                        }
                        
                        
                        if (!link) {
    // 初始化一个CADisplayLink 一秒调用60次
                            link = [CADisplayLink displayLinkWithTarget:self selector:@selector(adjustOffSet)];
                            [link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
    //根据截图的范围与边界的关系,调整滚动的快慢参数
                            if ((CGRectGetMaxX(snapView.frame) - rollBeginMaxX)/5 < 1) {
                                _incrementHeadView = 1;
                            }
                            else if((CGRectGetMaxX(snapView.frame) - rollBeginMaxX)/5 > 20) {
                                _incrementHeadView = 5;
                            }
                            else {
                                _incrementHeadView = (CGRectGetMaxX(snapView.frame) - rollBeginMaxX)/5;
                            }
                            
                        }
    
                    }
                    else if (CGRectGetMinX(snapView.frame) < rollBeginMinX) {//向左滚动
                        if (!link) {
                            link = [CADisplayLink displayLinkWithTarget:self selector:@selector(minusContentOffset)];
                            [link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
                        }
                        
                        if (-(CGRectGetMinX(snapView.frame) - rollBeginMinX)/5 < 1) {
                            _increment = 1;
                        }
                        else if(-(CGRectGetMinX(snapView.frame) - rollBeginMinX)/5 > 20) {
                            _increment = 5;
                        }
                        else {
                            _increment = -(CGRectGetMinX(snapView.frame) - rollBeginMinX)/5;
                        }
                    }
                    else { //截图大小范围没有超过左右边界时,滚动停止
                        [link invalidate];
                        link = nil;
                        [link invalidate];
                        link = nil;
                    }
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有