效果图:
项目中经常用到这种监听键盘弹出,并且自定义键盘上部的view,
一.创建一个继续自NSObject的STEAction对象
STEAction.h
#import <Foundation/Foundation.h>
@interface STEAction : NSObject
@property (nonnull, nonatomic, copy) void (^aHandler)(STEAction *_Nullable action);
@property (nullable,nonatomic,readonly) NSString * imageName;
@property (nullable, nonatomic, readonly) NSString *title;
@property (nonatomic, getter=isEnabled) BOOL enabled;
+ (nonnull instancetype)actionWithTitle:(nullable NSString *)title imageName:(nullable NSString *)imageName handler:(void (^ __nullable)(STEAction *_Nullable action))handler;
@end
STEAction.m
#import "STEAction.h"
@implementation STEAction
+ (nonnull instancetype)actionWithTitle:(nullable NSString *)title imageName:(nullable NSString *)imageName handler:(void (^ __nullable)(STEAction *_Nullable action))handler
{
return [[[self class]alloc]initWithTitle:title imageName:imageName handler:handler];
}
- (nonnull instancetype)initWithTitle:(nullable NSString *)title imageName:(nullable NSString *)imageName handler:(void (^ __nullable)(STEAction *_Nullable action))handler
{
self = [super init];
if (self)
{
self.aHandler = handler;
_title = title;
_imageName = imageName;
}
return self;
}
@end
一.监听 keyboardWillChangeFrame
STEBaseToolsBar.h
#import <UIKit/UIKit.h>
@class STEAction;
@interface STEBaseToolsBar : UIView
//头文件 暴露一些方法,方便自定义子类调用,
- (void)addObserVer;
- (void)keyboardWillChangeFrame:(NSNotification *_Nullable)note;
- (void)addAction:(nonnull STEAction*)action;
@property (nonnull, nonatomic, strong) NSMutableArray *actionArray;
@end
STEBaseToolsBar.m
#import "STEBaseToolsBar.h"
#import "STEAction.h"
@interface STEBaseToolsBar()
@property (nonatomic, assign) CGFloat keyBoradHeight;
@end
@implementation STEBaseToolsBar
#pragma mark - 键盘通知
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)awakeFromNib
{
[super awakeFromNib];
[self addObserVer];
}
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
[self addObserVer];
}
return self;
}
#pragma mark - 键盘处理
- (void)keyboardWillChangeFrame:(NSNotification *)note {
// 取出键盘最终的frame
CGRect rect = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
// 取出键盘弹出需要花费的时间
double duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
// 修改transform
[UIView animateWithDuration:duration animations:^{
CGFloat ty = [UIScreen mainScreen].bounds.size.height - rect.origin.y;
self.transform = CGAffineTransformMakeTranslation(0, - ty);
}];
}
- (void)addObserVer
{
[[NSNotificationCenter defaultCenter]removeObserver:self];
// 监听键盘通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
}
- (void)layoutSubviews
{
[super layoutSubviews];
//布局之前将之前的 按钮移除
for (UIView *view in self.subviews) {
if ([view isKindOfClass:[UIButton class]] && view.tag >1000 )
{
[view removeFromSuperview];
}
}
if ([self.actionArray count] <= 0) {
return;
}
CGFloat cWidth = CGRectGetWidth(self.bounds);
CGFloat cHeight = CGRectGetHeight(self.bounds);
__weak __typeof(self) ws = self;
NSUInteger count = self.actionArray.count;
CGFloat buttonWidth = cWidth/count;
//从底部40处添加按钮
CGFloat originY = cHeight - 40;
//遍历事件数组
[self.actionArray enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if (obj && [obj isKindOfClass:[STEAction class]]) {
STEAction *action = (STEAction*)obj;
CGRect frame = CGRectMake(idx*buttonWidth, originY, buttonWidth, 40);
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.tag = idx + 1001;
button.frame = frame;
[button setImage:[UIImage imageNamed:action.imageName] forState:UIControlStateNormal];
//按钮绑定点击事件
[button addTarget:ws action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[ws addSubview:button];
}
}];
}
- (void)buttonClick:(UIButton*)sender
{
if ((sender.tag - 1001) < [self.actionArray count])
{
//按钮点击之后 ,自身消失,将action的aHandleBlock 传递过去.
STEAction *action = self.actionArray[sender.tag - 1001];
if (action.aHandler) {
action.aHandler(action);
}
}
}
- (void)addAction:(STEAction *)action
{
if (action) {
[self.actionArray addObject:action];
}
}
- (NSMutableArray*)actionArray
{
if (_actionArray) {
return _actionArray;
}
_actionArray = [[NSMutableArray alloc]init];
return _actionArray;
}
@end