iOS 全局懸浮按鈕

1.創(chuàng)建按鈕,加在UIApplication 的keyWindow上;

@property(strong,nonatomic)UIWindow *window;
@property(strong,nonatomic)UIButton *button;
-(void)createButton{
    if (!_button) {
        _window = [[UIApplication sharedApplication] keyWindow];
        _window.backgroundColor = [UIColor whiteColor];
        [_window addSubview:self.button];
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:
        self action:@selector(locationChange:)];
        pan.delaysTouchesBegan = YES;
        [_button addGestureRecognizer:pan];
    }
}

2.button的懶加載

-(UIButton*)button{
    if (!_button) {
        _button = [UIButton buttonWithType:UIButtonTypeCustom];
        _button.backgroundColor = [UIColor redColor];
        _button.frame = CGRectMake(0, 200, 50, 66);//初始在屏幕上的位置
        //_button.layer.cornerRadius = 33;
        //_button.layer.masksToBounds = YES;
        //[_button addTarget:self action:@selector(removeButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return _button;
}

3.改變位置

-(void)locationChange:(UIPanGestureRecognizer*)p{
    CGFloat HEIGHT=_button.frame.size.height;
    CGFloat WIDTH=_button.frame.size.width;
    CGPoint panPoint = [p locationInView:[UIApplication sharedApplication].windows[0]];
    if(p.state == UIGestureRecognizerStateChanged){
        _button.center = CGPointMake(panPoint.x, panPoint.y);
    }
    else if(p.state == UIGestureRecognizerStateEnded){
        if(panPoint.x <= KScreenWidth/2) {
            if(panPoint.y <= 40+HEIGHT/2 && panPoint.x >= 20+WIDTH/2)  {
                [UIView animateWithDuration:0.2 animations:^{
                    _button.center = CGPointMake(panPoint.x, HEIGHT/2);
                }];
            } else if(panPoint.y >= KScreenHeight-HEIGHT/2-40 && panPoint.x >= 20+WIDTH/2){
                [UIView animateWithDuration:0.2 animations:^{
                    _button.center = CGPointMake(panPoint.x, KScreenHeight-HEIGHT/2);
                }];
            }  else if (panPoint.x < WIDTH/2+15 && panPoint.y > KScreenHeight-HEIGHT/2){
                [UIView animateWithDuration:0.2 animations:^{
                    _button.center = CGPointMake(WIDTH/2, KScreenHeight-HEIGHT/2);
                }];
            }  else{
                CGFloat pointy = panPoint.y < HEIGHT/2 ? HEIGHT/2 :panPoint.y;
                [UIView animateWithDuration:0.2 animations:^{
                    _button.center = CGPointMake(WIDTH/2, pointy);
                }];
            }
        } else if(panPoint.x > KScreenWidth/2)
        {
            if(panPoint.y <= 40+HEIGHT/2 && panPoint.x < KScreenWidth-WIDTH/2-20 ) {
                [UIView animateWithDuration:0.2 animations:^{
                    _button.center = CGPointMake(panPoint.x, HEIGHT/2);
                }];
            }else if(panPoint.y >= KScreenHeight-40-HEIGHT/2 && panPoint.x < KScreenWidth-WIDTH/2-20) {
                [UIView animateWithDuration:0.2 animations:^{
                    _button.center = CGPointMake(panPoint.x, KScreenHeight-HEIGHT/2);
                }];
            }else if (panPoint.x > KScreenWidth-WIDTH/2-15 && panPoint.y < HEIGHT/2) {
                [UIView animateWithDuration:0.2 animations:^{
                    _button.center = CGPointMake(KScreenWidth-WIDTH/2, HEIGHT/2);
                }];
            } else{
                CGFloat pointy = panPoint.y > KScreenHeight-HEIGHT/2 ? KScreenHeight-HEIGHT/2 :panPoint.y;
                [UIView animateWithDuration:0.2 animations:^{
                    _button.center = CGPointMake(KScreenWidth-WIDTH/2, pointy);
                }];
            }
        }
    }
}

移除button

- (void)removeButton
{
    [_button removeFromSuperview];
    _button = nil;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容