两种自定义系统弹出键盘上方的view

        我们在很多的应用中,都可能会遇到,在弹出的键盘上方的view,添加一些控件来作辅助功能,下面我通过2种情况来介绍:
// 屏幕的物理高度
#define  ScreenHeight  [UIScreen mainScreen].bounds.size.height

// 屏幕的物理宽度
#define  ScreenWidth   [UIScreen mainScreen].bounds.size.width

@interface HMTMainViewController ()

@property (nonatomic, strong) UIView *testView;
@property (nonatomic, strong) UITextField *testTextField;
@property (nonatomic, strong) NSNumber *duration;
@property (nonatomic, strong) NSNumber *curve;

@end

@implementation HMTMainViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"Demo";
    
    [self testCustonKeyBoardView___1];
    [self testCustonKeyBoardView___2];
}

// 自定义系统弹出键盘上方的view ---> 普遍情况
- (void)testCustonKeyBoardView___1
{
    UITextField *testTextField = [[UITextField alloc] initWithFrame:CGRectMake(60, 200, 200, 40)];
    testTextField.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:testTextField];
    
    // 自定义的view
    UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
    customView.backgroundColor = [UIColor redColor];
    testTextField.inputAccessoryView = customView;
    
    // 往自定义view中添加各种UI控件(以UIButton为例)
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(270, 5, 40, 30)];
    button.backgroundColor = [UIColor blackColor];
    [button setTitle:@"完成" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(didClickButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [customView addSubview:button];
}

- (void)didClickButtonAction
{
    NSLog(@"%s__%d__|%@",__FUNCTION__,__LINE__,@"test");
    [self.view endEditing:YES];
}

// 自定义系统弹出键盘上方的view ---> 微信,QQ聊天等效果
- (void)testCustonKeyBoardView___2
{
    self.testView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight - 40, 320, 40)];
    _testView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:_testView];
    
    self.testTextField = [[UITextField alloc] initWithFrame:CGRectMake(40, 2, 200, 36)];
    _testTextField.borderStyle = UITextBorderStyleRoundedRect;
    [_testView addSubview:_testTextField];
    
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    leftBtn.frame = CGRectMake(242, 2, 36, 36);
    [leftBtn setTitle:@"-" forState:UIControlStateNormal];
    leftBtn.titleLabel.font = [UIFont systemFontOfSize: 28.0];
    [_testView addSubview:leftBtn];
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    rightBtn.frame = CGRectMake(282, 2, 36, 36);
    rightBtn.titleLabel.font = [UIFont systemFontOfSize: 28.0];
    [rightBtn setTitle:@"+" forState:UIControlStateNormal];
    [_testView addSubview:rightBtn];
    
    
    // 通知-监听键盘弹出事件
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeKeyboardWillShowNotification:) name:UIKeyboardWillShowNotification object:nil];
    // 通知-监听键盘回收事件
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeKeyboardWillHideNotification:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)changeKeyboardWillShowNotification:(NSNotification *)notification
{
    NSDictionary *userInfo = [notification userInfo];
    // 键盘弹出后的frame的结构体对象
    NSValue *valueEndFrame = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    // 得到键盘弹出后的键盘视图所在y坐标
    CGFloat keyBoardEndY = valueEndFrame.CGRectValue.origin.y;
    // 键盘弹出的动画时间
    self.duration = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    // 键盘弹出的动画曲线
    self.curve = [userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];
    
    // 添加移动动画,使视图跟随键盘移动(动画时间和曲线都保持一致)
    [UIView animateWithDuration:[_duration doubleValue] animations:^{
        [UIView setAnimationBeginsFromCurrentState:YES];
        // 不设置这个,大家可以看看有什么区别
        [UIView setAnimationCurve:[_curve intValue]];
        _testView.center = CGPointMake(_testView.center.x, keyBoardEndY - _testView.bounds.size.height/2.0);
    }];
    /**
     *  + (void)setAnimationCurve:(UIViewAnimationCurve)curve;
     *  typedef NS_ENUM(NSInteger, UIViewAnimationCurve) {
     *  UIViewAnimationCurveEaseInOut,         // 淡入淡出,开始时慢,由慢变快,中间最快,然后变慢
     *  UIViewAnimationCurveEaseIn,            // 淡入,开始时慢然后越来越快
     *  UIViewAnimationCurveEaseOut,           // 淡出,开始快然后越来越慢
     *  UIViewAnimationCurveLinear             // 线性匀速,开始和结束是一个速度
     *  };
     */
}

- (void)changeKeyboardWillHideNotification:(NSNotification *)notification
{
    NSDictionary *userInfo = [notification userInfo];
    // 键盘弹出前的frame的结构体对象
    NSValue *valueStatrFrame = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    // 得到键盘弹出后的键盘视图所在y坐标
    CGFloat keyBoardStatrY = valueStatrFrame.CGRectValue.origin.y;
    
    [UIView animateWithDuration:[_duration doubleValue] animations:^{
        [UIView setAnimationBeginsFromCurrentState:YES];
        [UIView setAnimationCurve:[_curve intValue]];
        _testView.center = CGPointMake(_testView.center.x, keyBoardStatrY - _testView.bounds.size.height/2.0);
    }];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}


@示例图:




  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值