#pragma mark - 尺寸相关
#define HWScreenW [UIScreen mainScreen].bounds.size.width
#define HWScreenH [UIScreen mainScreen].bounds.size.height
@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableVIew;
@property(strong,nonatomic)UIWindow *window;
@property(strong,nonatomic)UIButton *button;
/** <#注释#> */
@property (nonatomic, assign) BOOL isShow;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableVIew registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cellID"];
[self loadData];
}
#pragma mark - 模拟数据请求
- (void)loadData {
NSLog(@"数据请求成功");
self.isShow = YES;
if (self.isShow) {
[self createSuspendButton];
}
}
#pragma mark - 创建悬浮的按钮
- (void)createSuspendButton {
CGFloat btnHW = 40; // 按钮宽高
CGFloat btnTop = 20; // 上间距
CGFloat btnRight = 20; // 右间距
CGFloat borderWidth = 2; // 边框宽度
_button = [UIButton buttonWithType:UIButtonTypeCustom];
[_button setTitle:@"戴" forState:UIControlStateNormal];
_button.frame = CGRectMake(0, 0, btnHW, btnHW);
[_button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[_button addTarget:self action:@selector(tryButtonClick) forControlEvents:UIControlEventTouchUpInside];
_button.backgroundColor = [UIColor clearColor];
[_button addRoundedCornersWithRadius:btnHW/2];
[_button addBorderWithWidth:borderWidth];
_window = [[UIWindow alloc]initWithFrame:CGRectMake(HWScreenW - btnHW -btnRight, 64+btnTop, btnHW, btnHW)];
_window.windowLevel = UIWindowLevelAlert + 1;
_window.backgroundColor = [UIColor clearColor];
[_window addRoundedCornersWithRadius:btnHW/2];
[_window addSubview:_button];
[_window makeKeyAndVisible];//关键语句,显示window
}
#pragma mark - 按钮点击事件
- (void)tryButtonClick {
twoViewController *ctr = [[twoViewController alloc]init];
[self.navigationController pushViewController:ctr animated:YES];
}
#pragma mark - 关闭悬浮的window
- (void)closeWindow {
[_window resignKeyWindow];
_window = nil;
}
#pragma mark - 即将消失
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self closeWindow];
}
#pragma mark - 即将显示
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if (self.isShow) {
[self createSuspendButton];
}
}
用到的分类
.h文件
#import <UIKit/UIKit.h>
@interface UIView (Tools)
/**
快速给View添加4边阴影
参数:阴影透明度,默认0
*/
- (void)addProjectionWithShadowOpacity:(CGFloat)shadowOpacity;
/**
快速给View添加4边框
参数:边框宽度
*/
- (void)addBorderWithWidth:(CGFloat)width;
/**
快速给View添加4边框
width:边框宽度
borderColor:边框颜色
*/
- (void)addBorderWithWidth:(CGFloat)width borderColor:(UIColor *)borderColor;
/**
快速给View添加圆角
参数:圆角半径
*/
- (void)addRoundedCornersWithRadius:(CGFloat)radius;
/**
快速给View添加圆角
radius:圆角半径
corners:且那几个角
类型共有以下几种:
typedef NS_OPTIONS(NSUInteger, UIRectCorner) {
UIRectCornerTopLeft,
UIRectCornerTopRight ,
UIRectCornerBottomLeft,
UIRectCornerBottomRight,
UIRectCornerAllCorners
};
使用案例:[self.mainView addRoundedCornersWithRadius:10 byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight]; // 切除了左下 右下
*/
- (void)addRoundedCornersWithRadius:(CGFloat)radius byRoundingCorners:(UIRectCorner)corners;
@end
.m文件
#import "UIView+Tools.h"
@implementation UIView (Tools)
#pragma mark - 快速添加阴影
- (void)addProjectionWithShadowOpacity:(CGFloat)shadowOpacity {
self.layer.shadowColor = [UIColor blackColor].CGColor;//shadowColor阴影颜色
self.layer.shadowOffset = CGSizeMake(0,0);//shadowOffset阴影偏移,x向右偏移2,y向下偏移6,默认(0, -3),这个跟shadowRadius配合使用
self.layer.shadowOpacity = shadowOpacity;//阴影透明度,默认0
self.layer.shadowRadius = 2;//阴影半径,默认3
}
- (void)addBorderWithWidth:(CGFloat)width {
self.layer.borderWidth = width;
self.layer.borderColor = [UIColor blackColor].CGColor;
}
- (void)addBorderWithWidth:(CGFloat)width borderColor:(UIColor *)borderColor {
self.layer.borderWidth = width;
self.layer.borderColor = borderColor.CGColor;
}
- (void)addRoundedCornersWithRadius:(CGFloat)radius {
self.layer.cornerRadius = radius;
self.layer.masksToBounds = YES;
}
- (void)addRoundedCornersWithRadius:(CGFloat)radius byRoundingCorners:(UIRectCorner)corners{
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(radius, radius)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.bounds;
maskLayer.path = maskPath.CGPath;
self.layer.mask = maskLayer;
}
@end