iOS 悬浮按钮

本文介绍如何在iOS应用中创建一个悬浮按钮,通过继承UIWindow创建自定义窗口类,详细讲解了.h和.m文件的实现过程,帮助开发者实现这一功能。
摘要由CSDN通过智能技术生成

新建继承于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 =
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值