浮动

作者: 高乔人 | 来源:发表于2020-10-15 15:03 被阅读0次

    我这里是基于Masonry布局的

    #import "floatView.h"
    #import "UIView+Extension.h"
    #import "Masonry.h"
    @interface floatView ()
    @property (nonatomic,strong)UIButton *spButton;
    ///拖动暂停位置
    //@property(nonatomic,assign) CGPoint stopPoint;
    ///注释
    @property(nonatomic,strong)UIView *otherView;
    @end
    

    1.创建按钮

    - (UIButton *)spButton{
        if (!_spButton) {
            self.spButton = [UIButton buttonWithType:UIButtonTypeCustom];
            [self.spButton setImage:[UIImage imageNamed:@"shijianshangbao"]  forState:UIControlStateNormal];
            self.spButton.frame = CGRectMake([UIScreen mainScreen].bounds.size.width-71,300,66,66);;
            self.spButton.backgroundColor = UIColor.brownColor;
            self.spButton.tag = 0;
            self.spButton.layer.cornerRadius = 8;
            [self addSubview:self.spButton];
        }
        return _spButton;
    }
    

    2.给按钮添加手势

    -(void)initAddEventBtn{
        [self.spButton addTarget:self action:@selector(addEvent:) forControlEvents:UIControlEventTouchUpInside];
        //添加手势
        UIPanGestureRecognizer *panRcognize=[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)];
        [panRcognize setMinimumNumberOfTouches:1];
        [panRcognize setEnabled:YES];
        [panRcognize delaysTouchesEnded];
        [panRcognize cancelsTouchesInView];
        [self.spButton addGestureRecognizer:panRcognize];
    }
    

    处理按钮点击处相对于屏幕的位置

    - (void)addEvent:(UIButton *)btn{
        btn.selected = !btn.selected;
        UIWindow * window= [[[UIApplication sharedApplication] delegate] window];
        CGRect rect = [btn convertRect: btn.bounds toView:window];
            if (rect.origin.x>SCREEN_WIDTH/2) {
                [self dealOtherViewOnRight:btn];
            }else if (rect.origin.x<SCREEN_WIDTH/2){
                [self dealOtherViewOnLeft:btn];
            }
    }
    

    浮动按钮在屏幕右边时

    - (void)dealOtherViewOnRight:(UIButton *)btn{
        UIWindow * window= [[[UIApplication sharedApplication] delegate] window];
        CGRect rect = [btn convertRect: btn.bounds toView:window];
        if (btn.selected == YES) {
            self.otherView.height = rect.size.height;
            self.otherView.x = rect.origin.x;
            self.otherView.width = 0;
            self.otherView.centerY = btn.centerY;
            [UIView animateWithDuration:0.5 animations:^{
                self.otherView.frame = CGRectMake(rect.origin.x-200, rect.origin.y, 200, rect.size.height);
            }];
        }else{
            [UIView animateWithDuration:0.5 animations:^{
                self.otherView.frame = CGRectMake(rect.origin.x, rect.origin.y, 0, rect.size.height);
            }];
        }
    }
    

    浮动按钮在屏幕左边时

    - (void)dealOtherViewOnLeft:(UIButton *)btn{
        CGRect rect = btn.frame;
        if (btn.selected == YES) {
            self.otherView.height = rect.size.height;
            self.otherView.x = rect.origin.x+rect.size.width;
            self.otherView.width = 0;
            self.otherView.centerY = btn.centerY;
            [UIView animateWithDuration:0.5 animations:^{
                self.otherView.frame = CGRectMake(rect.origin.x+rect.size.width, rect.origin.y, 200, rect.size.height);
            }];
        }else{
            [UIView animateWithDuration:0.5 animations:^{
                self.otherView.frame = CGRectMake(rect.origin.x+rect.size.width, rect.origin.y, 0, rect.size.height);
            }];
        }
    }
    

    重点来了,移动位置的处理

    - (void)handlePanGesture:(UIPanGestureRecognizer *)recognizer
    {
        //移动状态
        UIGestureRecognizerState recState =  recognizer.state;
        
        switch (recState) {
            case UIGestureRecognizerStateBegan:
                
                break;
            case UIGestureRecognizerStateChanged:
            {
                CGPoint translation = [recognizer translationInView:[self getCurrentVC].navigationController.view];
                recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, recognizer.view.center.y + translation.y);
            }
                break;
            case UIGestureRecognizerStateEnded:
            {
                CGPoint stopPoint = CGPointMake(0, SCREEN_HEIGHT / 2.0);
                
                if (recognizer.view.center.x < SCREEN_WIDTH / 2.0) {
                    if (recognizer.view.center.y <= SCREEN_HEIGHT/2.0) {
                        //左上
                        if (recognizer.view.center.x  >= recognizer.view.center.y) {
                            stopPoint = CGPointMake(recognizer.view.center.x, self.spButton.width/2.0);
                        }else{
                            stopPoint = CGPointMake(self.spButton.width/2.0, recognizer.view.center.y);
                        }
                    }else{
                        //左下
                        if (recognizer.view.center.x  >= SCREEN_HEIGHT - recognizer.view.center.y) {
                            stopPoint = CGPointMake(recognizer.view.center.x, SCREEN_HEIGHT - self.spButton.width/2.0);
                        }else{
                            stopPoint = CGPointMake(self.spButton.width/2.0, recognizer.view.center.y);
                        }
                    }
                }else{
                    if (recognizer.view.center.y <= SCREEN_HEIGHT/2.0) {
                        //右上
                        if (SCREEN_WIDTH - recognizer.view.center.x  >= recognizer.view.center.y) {
                            stopPoint = CGPointMake(recognizer.view.center.x, self.spButton.width/2.0);
                        }else{
                            stopPoint = CGPointMake(SCREEN_WIDTH - self.spButton.width/2.0, recognizer.view.center.y);
                        }
                    }else{
                        //右下
                        if (SCREEN_WIDTH - recognizer.view.center.x  >= SCREEN_HEIGHT - recognizer.view.center.y) {
                            stopPoint = CGPointMake(recognizer.view.center.x, SCREEN_HEIGHT - self.spButton.width/2.0);
                        }else{
                            stopPoint = CGPointMake(SCREEN_WIDTH - self.spButton.width/2.0,recognizer.view.center.y);
                        }
                    }
                }
                
                //如果按钮超出屏幕边缘
                if (stopPoint.y + self.spButton.width+40>= SCREEN_HEIGHT) {
                    stopPoint = CGPointMake(stopPoint.x, SCREEN_HEIGHT - self.spButton.width/2.0-49);
                    NSLog(@"超出屏幕下方了!!"); //这里注意iphoneX的适配。。X的SCREEN高度算法有变化。
                }
                if (stopPoint.x - self.spButton.width/2.0 <= 0) {
                    stopPoint = CGPointMake(self.spButton.width/2.0+5, stopPoint.y);
                    NSLog(@"超出屏幕左边了!!");
                }
                if (stopPoint.x + self.spButton.width/2.0 >= SCREEN_WIDTH) {
    //                break;
                    stopPoint = CGPointMake(SCREEN_WIDTH - self.spButton.width/2.0-5, stopPoint.y);
                    NSLog(@"超出屏幕右边了!!");
                }
                if (stopPoint.y - self.spButton.width/2.0 <= 0) {
                    stopPoint = CGPointMake(stopPoint.x, self.spButton.width/2.0);
                }
                [UIView animateWithDuration:0.5 animations:^{
                    recognizer.view.center = stopPoint;
                }];
            }
                break;
                
            default:
                break;
        }
        
        [recognizer setTranslation:CGPointMake(0, 0) inView:self];
    }
    

    相关文章

      网友评论

          本文标题:浮动

          本文链接:https://www.haomeiwen.com/subject/ejxopktx.html