控制器添加悬浮窗

分享了在控制器中添加可滑动悬浮窗的三种实现方式,提供了源码链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

源码地址:https://pan.baidu.com/s/1mi9SOGC

一,添加一个可滑动的悬浮窗我这里会介绍三种方法。

1.直接在一个控制器中,可以初始化一个视图,


//
//  ViewController.m
//  浮动控件
//
//  Created by LJX on 2017/10/9.
//  Copyright © 2017年 LJX. All rights reserved.
//

#import "ViewController.h"
#import "UIView+Extension.h"
#import "MYButton.h"
#import "AppDelegate.h"
@interface ViewController ()
@property (nonatomic, strong) UIView *activeView;
@property (nonatomic, assign) CGFloat x1;
@property (nonatomic, assign) CGFloat y1;
@property (nonatomic, strong) MYButton *btn;
@end
@implementation ViewController
-(MYButton *)btn{
    if (!_btn) {
        _btn = [[MYButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width-60, self.view.frame.size.height-60, 50, 50)];
    }
    return _btn;
}
-(UIView *)activeView{
    if (!_activeView) {
        _activeView = [[UIView alloc] initWithFrame:CGRectMake(self.view.frame.size.width-60, self.view.frame.size.height-60, 50, 50)];
        _activeView.layer.cornerRadius = 25;
        _activeView.backgroundColor = [UIColor greenColor];
    }
    return _activeView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.activeView];
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(viewPangest:)];
    [self.activeView addGestureRecognizer:pan];
}
-(void)viewPangest:(UIPanGestureRecognizer *)pan{
    if (pan.state == UIGestureRecognizerStateChanged) {
        CGPoint point = [pan translationInView:self.activeView];
        self.activeView.x = self.activeView.frame.origin.x+(point.x-self.x1);
        self.activeView.y = self.activeView.frame.origin.y+(point.y-self.y1);
        NSLog(@"point.x == %f  point.y == %f",point.x,point.y);
        self.x1 = point.x;
        self.y1 = point.y;
    }else if (pan.state == UIGestureRecognizerStateEnded){
        self.x1 = self.y1 = 0.0;
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


@end


2,自定义一个按钮,悬浮在所有控制器上面。自定义一个base控制器,在里面添加按钮。其他控制器都继承自base控制器。子控制器添加控件选择插入的方法

[self.view insertSubview:self.btn belowSubview:self.activeView];

自定义的按钮

//
//  MYButton.m
//  浮动控件
//
//  Created by LJX on 2017/10/9.
//  Copyright © 2017年 LJX. All rights reserved.
//

#import "MYButton.h"
#import "UIView+Extension.h"
@interface MYButton()
@property (nonatomic, assign) CGFloat x1;
@property (nonatomic, assign) CGFloat y1;
@end
@implementation MYButton
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.layer.cornerRadius = frame.size.width/2;
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(viewPangest:)];
        self.backgroundColor = [UIColor yellowColor];
        [self addGestureRecognizer:pan];
    }
    return self;
}
-(void)viewPangest:(UIPanGestureRecognizer *)pan{
    
    if (pan.state == UIGestureRecognizerStateChanged) {
        if (self.x <5) {
            NSLog(@"self.x == %f self.y == %f",self.x,self.y);
            self.x ++;
            return;
        }else if (self.y<5){
            self.y ++;
            return;
        }else if (self.y>self.superview.height-self.height-5){
            self.y --;
            return;
        }else if (self.x>self.superview.width-self.width-5){
            self.x --;
            return;
        }
        CGPoint point = [pan translationInView:self];
        self.x = self.frame.origin.x+(point.x-self.x1);
        self.y = self.frame.origin.y+(point.y-self.y1);
        NSLog(@"point.x == %f  point.y == %f",point.x,point.y);
        self.x1 = point.x;
        self.y1 = point.y;
    }else if (pan.state == UIGestureRecognizerStateEnded){
        self.x1 = self.y1 = 0.0;
    }
    
}


@end

3,第三种,自定义一个window,初始化在appledate。

自定义的window

//
//  MYWindow.h
//  浮动控件
//
//  Created by LJX on 2017/10/9.
//  Copyright © 2017年 LJX. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface MYWindow : UIWindow{
    UIButton *_button;
}
@property (nonatomic, assign) CGFloat x1;
@property (nonatomic, assign) CGFloat y1;
@end


//
//  MYWindow.m
//  浮动控件
//
//  Created by LJX on 2017/10/9.
//  Copyright © 2017年 LJX. All rights reserved.
//

#import "MYWindow.h"
#import "UIView+Extension.h"
@implementation MYWindow
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.windowLevel = UIWindowLevelAlert + 1;
        //这句话很重要
        [self makeKeyAndVisible];
        _button = [UIButton buttonWithType:UIButtonTypeCustom];
        _button.backgroundColor = [UIColor grayColor];
        _button.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
        _button.layer.cornerRadius = frame.size.width/2;
//        [_button addTarget:self action:@selector(choose) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_button];
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(changePostion:)];
        [_button addGestureRecognizer:pan];
    }
    return self;
}
-(void)changePostion:(UIPanGestureRecognizer *)pan{
    if (pan.state == UIGestureRecognizerStateChanged) {
        _button.enabled = NO;
         CGPoint point = [pan translationInView:self];
        if (self.x <5) {
            NSLog(@"self.x == %f self.y == %f",self.x,self.y);
            self.x ++;
            return;
        }else if (self.y<5){
            self.y ++;
            return;
        }else if (self.y>[UIScreen mainScreen].bounds.size.height-self.height-5){
            self.y --;
            return;
        }else if (self.x>[UIScreen mainScreen].bounds.size.width-self.width-5){
            self.x --;
            return;
        }
       
        self.x = self.frame.origin.x+(point.x-self.x1);
        self.y = self.frame.origin.y+(point.y-self.y1);
        NSLog(@"point.x == %f  point.y == %f",point.x,point.y);
        self.x1 = point.x;
        self.y1 = point.y;
    }else if (pan.state == UIGestureRecognizerStateEnded){
        self.x1 = self.y1 = 0.0;
        NSLog(@"self.x == %f self.y == %f",self.x,self.y);
        _button.enabled = YES;
    }
}

@end

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.rootViewController = [[ViewController alloc] init];
    [self.window makeKeyAndVisible];
    //要延迟三秒再初始化控件,否则会奔溃
    [self performSelector:@selector(setNew) withObject:nil afterDelay:3];
    return YES;
}
-(void)setNew
{
    _myWindow = [[MYWindow alloc] initWithFrame:CGRectMake(20, 20, 60, 60)];
}




                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值