NASuspendButton.h
#import
@interface NASuspendButton : UIView
/** 单例方式创建 */
+ (instancetype)shareInstance;
/** 普通方式创建 */
+ (instancetype)suspendButton;
/** 展示悬浮按钮 */
- (void)showSuspendButton;
/** 隐藏悬浮按钮 */
- (void)hideSuspendButton;
@end
NASuspendButton.m
#import "NASuspendButton.h"
#import "UIButton+DDYStyle.h"
#import "UIView+DDYExtension.h"
static NASuspendButton *_suspendBtn;
@interface NASuspendButton ()
@property (nonatomic, strong) UIButton *keyBtn;
@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UIButton *switchBtn;
@property (nonatomic, strong) UIButton *sayBtn;
@property (nonatomic, strong) UIButton *soundBtn;
@property (nonatomic, strong) UIButton *receiverBtn;
@property (nonatomic, assign) BOOL canHandleClick;
@end
@implementation NASuspendButton
+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_suspendBtn = [super allocWithZone:zone];
});
return _suspendBtn;
}
+ (instancetype)shareInstance
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_suspendBtn = [[self alloc] init];
});
return _suspendBtn;
}
+ (instancetype)suspendButton
{
return [[self alloc] initWithFrame:[UIScreen mainScreen].bounds];
}
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame])
{
[self setupContentView];
[self layoutContentView];
_canHandleClick = YES;
}
return self;
}
- (void)setupContentView
{
_keyBtn = [self btnWithImg:@"home_mac_little" action:@selector(handleKeyBtnClick:)];
[_keyBtn addTarget:self action:@selector(dragMoving:withEvent:) forControlEvents:UIControlEventTouchDragInside | UIControlEventTouchDragOutside];
_keyBtn.ddy_y = QNSCREENH/2;
_bgView = [[UIView alloc] initWithFrame:CGRectMake(_keyBtn.ddy_right+10, _keyBtn.ddy_y, 30+40*4, 40)];
_bgView.hidden = YES;
_switchBtn = [self btnWithImg:@"home_switch" action:@selector(handleSwitchBtnClick:)];
_sayBtn = [self btnWithImg:@"home_say" action:@selector(handleSayBtnClick:)];
_soundBtn = [self btnWithImg:@"home_sound" action:@selector(handleSoundBtnClick:)];
[_soundBtn setImage:[UIImage imageNamed:@"home_no_sound"] forState:UIControlStateSelected];
_receiverBtn = [self btnWithImg:@"home_receiver" action:@selector(handleReceiverBtnClick:)];
[self addSubview:_keyBtn];
[self addSubview:_bgView];
[_bgView addSubview:_switchBtn];
[_bgView addSubview:_sayBtn];
[_bgView addSubview:_soundBtn];
[_bgView addSubview:_receiverBtn];
}
#pragma mark 布局
- (void)layoutContentView
{
if (_keyBtn.ddy_y <= 25)
{
_keyBtn.ddy_y = 25;
}
if (_keyBtn.ddy_bottom > QNSCREENH)
{
_keyBtn.ddy_bottom = QNSCREENH;
}
if (_keyBtn.ddy_x<(QNSCREENW/2))
{
_keyBtn.ddy_x = 0;
_bgView.ddy_x = _keyBtn.ddy_right + 10;
}
else
{
_keyBtn.ddy_right = QNSCREENW;
_bgView.ddy_right = _keyBtn.ddy_x - 10;
}
_bgView.ddy_y = _keyBtn.ddy_y;
}
#pragma mark 展开所有button
- (void)expandButton
{
_switchBtn.ddy_x = 0;
_sayBtn.ddy_x = _switchBtn.ddy_right + 10;
_soundBtn.ddy_x = _sayBtn.ddy_right + 10;
_receiverBtn.ddy_x = _soundBtn.ddy_right + 10;
_switchBtn.alpha = 1;
_sayBtn.alpha = 1;
_soundBtn.alpha = 1;
_receiverBtn.alpha = 1;
}
#pragma mark 折叠所有button
- (void)foldButton
{
CGFloat x = (_keyBtn.ddy_x<(QNSCREENW/2)) ? (-50) : (_bgView.ddy_w + 10);
_switchBtn.ddy_x = x;
_sayBtn.ddy_x = x;
_soundBtn.ddy_x = x;
_receiverBtn.ddy_x = x;
_switchBtn.alpha = 0;
_sayBtn.alpha = 0;
_soundBtn.alpha = 0;
_receiverBtn.alpha = 0;
}
#pragma mark 创建按钮
- (UIButton *)btnWithImg:(NSString *)img action:(SEL)action
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 40, 40);
[button setImage:[UIImage imageNamed:img] forState:UIControlStateNormal];
[button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
return button;
}
#pragma mark 拖拽
- (void)dragMoving:(UIControl *)control withEvent:event
{
_canHandleClick = NO;
if (!_keyBtn.selected)
{
control.center = [[[event allTouches] anyObject] locationInView:self];
}
}
#pragma mark 显示悬浮按钮
- (void)showSuspendButton
{
[LC_KEYWINDOW addSubview:self];
self.hidden = NO;
}
#pragma mark 隐藏悬浮按钮
- (void)hideSuspendButton
{
self.hidden = YES;
[self removeFromSuperview];
}
#pragma mark - 事件响应
#pragma mark
- (void)handleKeyBtnClick:(UIButton *)sender
{
[self layoutContentView];
if (_canHandleClick)
{
if ((_keyBtn.selected = !_keyBtn.selected))
{
_bgView.hidden = NO;
[self foldButton];
[UIView animateWithDuration:0.3 animations:^{
[self expandButton];
}];
}
else
{
[self expandButton];
[UIView animateWithDuration:0.3 animations:^{
[self foldButton];
} completion:^(BOOL finished) {
_bgView.hidden = YES;
}];
}
}
_canHandleClick = YES;
}
#pragma mark 切换大小屏
- (void)handleSwitchBtnClick:(UIButton *)sender
{
}
#pragma mark 语音按钮点击响应
- (void)handleSayBtnClick:(UIButton *)sender
{
}
#pragma mark 声音打开或关闭
- (void)handleSoundBtnClick:(UIButton *)sender
{
}
#pragma mark
- (void)handleReceiverBtnClick:(UIButton *)sender
{
}
#pragma mark 防止响应链中断造成下面按钮不响应
- (id)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
id hitView = [super hitTest:point withEvent:event];
if (hitView == self) return nil;
else return hitView;
}
@end