悬浮拖动的View

悬浮拖动的View_第1张图片
悬浮拖动停靠边缘
  • 在View上加手势即可!(次方法悬浮于本VC上,若要每个VC上显示需加在window上),核心代码如下:
//3.手势移动方法
- (void)handlePanGesture:(UIPanGestureRecognizer *)recognizer{
    //移动状态
    UIGestureRecognizerState recState =  recognizer.state;
    
    switch (recState) {
        case UIGestureRecognizerStateBegan:
            
            break;
        case UIGestureRecognizerStateChanged:
        {
            CGPoint translation = [recognizer translationInView:self.superCorrectPersonView];
            recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, recognizer.view.center.y + translation.y);
        }
            break;
        case UIGestureRecognizerStateEnded:
        {
            CGPoint stopPoint = CGPointMake(0, KHeight / 2.0);
            
            if (recognizer.view.center.x < KWidth / 2.0) {
                if (recognizer.view.center.y <= KHeight/2.0) {
                    //左上
                    if (recognizer.view.center.x  >= recognizer.view.center.y) {
                        stopPoint = CGPointMake(recognizer.view.center.x, kSuspendBtnWidth/2.0);
                    }else{
                        stopPoint = CGPointMake(kSuspendBtnWidth/2.0, recognizer.view.center.y);
                    }
                }else{
                    //左下
                    if (recognizer.view.center.x  >= KHeight - recognizer.view.center.y) {
                        stopPoint = CGPointMake(recognizer.view.center.x, KHeight - kSuspendBtnWidth/2.0);
                    }else{
                        stopPoint = CGPointMake(kSuspendBtnWidth/2.0, recognizer.view.center.y);
                    }
                }
            }else{
                if (recognizer.view.center.y <= KHeight/2.0) {
                    //右上
                    if (KWidth - recognizer.view.center.x  >= recognizer.view.center.y) {
                        stopPoint = CGPointMake(recognizer.view.center.x, kSuspendBtnWidth/2.0);
                    }else{
                        stopPoint = CGPointMake(KWidth - kSuspendBtnWidth/2.0, recognizer.view.center.y);
                    }
                }else{
                    //右下
                    if (KWidth - recognizer.view.center.x  >= KHeight - recognizer.view.center.y) {
                        stopPoint = CGPointMake(recognizer.view.center.x, KHeight - kSuspendBtnWidth/2.0);
                    }else{
        
                        stopPoint = CGPointMake(KWidth - kSuspendBtnWidth/2.0,recognizer.view.center.y);
                    }
                }
            }
            
            if (stopPoint.x - kSuspendBtnWidth/2.0 <= 0) {//左
                stopPoint = CGPointMake(kSuspendBtnWidth/2.0, stopPoint.y);
            }
            
            if (stopPoint.x + kSuspendBtnWidth/2.0 >= KWidth) {//右
                stopPoint = CGPointMake(KWidth - kSuspendBtnWidth/2.0, stopPoint.y);
            }
            
            if (stopPoint.y - kSuspendBtnWidth/2.0 <= 0) {//上
                stopPoint = CGPointMake(stopPoint.x, kSuspendBtnWidth/2.0);//+64
            }
            
            if (stopPoint.y + kSuspendBtnWidth/2.0 >= KHeight-64) {//下
                stopPoint = CGPointMake(stopPoint.x, KHeight - kSuspendBtnWidth/2.0 - 64);//
            }
            
            [UIView animateWithDuration:0.5 animations:^{
                recognizer.view.center = stopPoint;
                
            }];
        }
            break;
            
        default:
            break;
    }
    
    [recognizer setTranslation:CGPointMake(0, 0) inView:self.superCorrectPersonView];
}

你可能感兴趣的:(悬浮拖动的View)