浮动

作者: 高乔人 | 来源:发表于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];
}

相关文章

  • 浮动、清除浮动、闭合浮动

    1、浮动元素有什么特征?对父容器、其他浮动元素、普通元素、文字分别有什么影响? 浮动元素会脱离正常的文档流,不占据...

  • 浮动与清除浮动

    原文地址:浮动与清除浮动 浮动 浮动的概念 浮动元素会脱离文档流并向左/向右浮动,直到碰到父元素或者另一个浮动元素...

  • 浮动、清除浮动

    一、浮动属性有哪些属性值 float • left 元素向左浮动• right ...

  • 浮动,清除浮动

    一、浮动元素有什么特征?对其他浮动元素、普通元素、文字分别有什么影响? 浮动元素会浮动到左或右依次排列,直到空间不...

  • 布局浮动的问题

    浮动的问题 什么是浮动?浮动(float)的副作用清除浮动两种清除浮动的办法如下:

  • 清除浮动

    清除浮动和闭合浮动 区别:清除浮动虽然排版正确,但是,浮动元素的父元素的高度为空; 闭合浮动:闭合浮动后元素高度正...

  • sdsdsdsd

    sdsdsddssdds浮动幅度东方饭店浮动幅度sdsdsddssdds浮动幅度东方饭店浮动幅度sdsdsddss...

  • 前端开发入门到实战:css 浮动布局,清除浮动

    浮动的特性: (1)浮动元素有左浮动(float:left)和右浮动(float:right)两种 (2)浮动的元...

  • 前端开发入门到实战:css 浮动布局,清除浮动

    浮动的特性: (1)浮动元素有左浮动(float:left)和右浮动(float:right)两种 (2)浮动的元...

  • 记12月9日作业

    浮动元素有什么特征? 浮动元素 浮动元素脱离正常的文档流; 设置浮动元素之后,不浮动的元素则不会感知到浮动元素的存...

网友评论

      本文标题:浮动

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