新建继承于UIWindow的类
.h文件如下
typedef void(^ClickBlock)(NSInteger i);
@interface GSLFloatingView : UIWindow
@property (nonatomic, copy) ClickBlock clickBlock;
//重要:所有图片都要是圆形的,程序里并没有自动处理成圆形
// warning: frame的长宽必须相等
- (instancetype)initWithFrame:(CGRect)frame mainImageName:(NSString *)mainImageName imagesAndTitle:(NSDictionary *)imagesAndTitle backgroundColor:(UIColor *)bgColor;
// 显示(默认)
- (void)showWindow;
// 隐藏
- (void)dissmissWindow;
.m 实现文件
#import "GSLFloatingView.h"
#define WIDTH self.frame.size.width
#define HEIGHT self.frame.size.height
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
#define animateDuration 0.3 //位置改变动画时间
#define showDuration 0.1 //展开动画时间
#define statusChangeDuration 3.0 //状态改变时间
#define normalAlpha 0.8 //正常状态时背景alpha值
#define sleepAlpha 0.3 //隐藏到边缘时的背景alpha值
#define myBorderWidth 1.0 //外框宽度
#define marginWith 5 //间隔
@interface GSLFloatingView ()
@property (nonatomic, assign) CGFloat frameWidth;
@property (nonatomic, assign) BOOL isShowTab;
@property (nonatomic, strong) UIPanGestureRecognizer *panGesture;
@property (nonatomic, strong) UITapGestureRecognizer *tapGesture;
@property (nonatomic, strong) UIButton *mainImageButton;
@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) NSDictionary *imagesAndTitle;
@property (nonatomic, strong) UIColor *bgColor;
@end
@implementation GSLFloatingView
- (instancetype)initWithFrame:(CGRect)frame mainImageName:(NSString *)mainImageName imagesAndTitle:(NSDictionary *)imagesAndTitle backgroundColor:(UIColor *)bgColor{
if (self = [super initWithFrame:frame]) {
// 断言NSAssert()是一个宏,用于开发阶段调试程序中的Bug,通过为NSAssert()传递条件表达式来断定是否属于Bug,满足条件返回真值,程序继续运行,如果返回假值,则抛出异常,并且可以自定义异常描述。
// Xcode 已经默认将release环境下的断言取消了, 免除了忘记关闭断言造成的程序不稳定. 所以不用担心 在开发时候大胆使用。
NSAssert(mainImageName != nil, @"mainImageName can't be nil !");
NSAssert(imagesAndTitle != nil, @"imagesAndTitle can't be nil !");
_isShowTab = NO;
self.backgroundColor = [UIColor clearColor];
self.windowLevel = UIWindowLevelAlert + 1;
self.rootViewController = [[UIViewController alloc] init];
[self makeKeyAndVisible];
_bgColor = bgColor;
_frameWidth = frame.size.width;
_imagesAndTitle = imagesAndTitle;
_contentView = [[UIView alloc] initWithFrame:CGRectMake(_frameWidth, 0, imagesAndTitle.count * (_frameWidth + 5), _frameWidth)];
_contentView.alpha = 0;
[self addSubview:_contentView];
//添加按钮
[self setButtons];
_mainImageButton =