因为代码较少,所以废话不多说,直接上代码
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UIButton *button;@property (nonatomic,strong) UIWindow *window;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//浮窗必须要在rootViewController创建之后加载,不然会崩溃,这就是为什么延迟的原因
[self performSelector:@selector(creatButton) withObject:nil afterDelay:0.2];
UITableView *tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds];
[tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
tableView.delegate = self;
tableView.dataSource = self;
[self.view addSubview:tableView];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 20;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 100;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
cell.textLabel.text = @"测试浮窗";
cell.textLabel.textColor = [UIColor redColor];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[_window resignKeyWindow];
_window = nil;
[self creatButton];
}
//创建浮层按钮
-(void)creatButton{
_button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 90, 90)];
_button.clipsToBounds = YES;
_button.layer.cornerRadius = 45;
_button.backgroundColor = [UIColor lightGrayColor];
[_button setTitle:@"悬浮按钮" forState:UIControlStateNormal];
[_button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[_button addTarget:self action:@selector(closeFloading) forControlEvents:UIControlEventTouchUpInside];
_window = [[UIWindow alloc]initWithFrame:CGRectMake(40, 200, 90,90)];
_window.windowLevel = UIWindowLevelAlert + 1;
_window.layer.cornerRadius = 40;
_window.layer.masksToBounds = YES;
[_window addSubview:_button];
[_window makeKeyAndVisible];
}
-(void)closeFloading{
[_window resignKeyWindow];
_window = nil;
}