OC实现ios类似微信输入框跟随键盘弹出的效果

首先说明的是我这篇文章就是把这位大神的swift实现ios类似微信输入框跟随键盘弹出的效果改成了OC版,已给自己备忘用的,如果你的项目用的是OC的话,用到的时候可以直接抄我的代码,不用你自己翻译了。。。
首先直接看下效果

接下来贴代码,解释代码里都有。

import "ViewController.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIView *suspendView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UITapGestureRecognizer *tapgest = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBackAction)];
    [self.view addGestureRecognizer:tapgest];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)viewWillAppear:(BOOL)animated {
    // 添加对键盘的监控
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)tapBackAction {
    [self.view endEditing:YES];
}

- (void)keyBoardWillShow:(NSNotification *) note {
    // 获取用户信息
    NSDictionary *userInfo = [NSDictionary dictionaryWithDictionary:note.userInfo];
    // 获取键盘高度
    CGRect keyBoardBounds  = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat keyBoardHeight = keyBoardBounds.size.height;
    // 获取键盘动画时间
    CGFloat animationTime  = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    
    // 定义好动作
    void (^animation)(void) = ^void(void) {
        self.suspendView.transform = CGAffineTransformMakeTranslation(0, - keyBoardHeight);
    };
    
    if (animationTime > 0) {
        [UIView animateWithDuration:animationTime animations:animation];
    } else {
        animation();
    }
    
}

- (void)keyBoardWillHide:(NSNotification *) note {
    // 获取用户信息
    NSDictionary *userInfo = [NSDictionary dictionaryWithDictionary:note.userInfo];
    // 获取键盘动画时间
    CGFloat animationTime  = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    
    // 定义好动作
    void (^animation)(void) = ^void(void) {
        self.suspendView.transform = CGAffineTransformIdentity;
    };
    
    if (animationTime > 0) {
        [UIView animateWithDuration:animationTime animations:animation];
    } else {
        animation();
    }
}

@end

需要说明的是代码里的suspendView是直接在SB里拖的,看图



就是上面用红框框起来的那块拖到界面上的。

另外还是从上面提到的那位作者的用到的另外一个画键盘上面悬浮窗的另外一个方法,那就是重写UIResponder的inputAccessoryView方法

不过注意此方法要慎用,因为一旦在一处写过了,在其他所有界面弹出键盘时都会出现此处定义的悬浮窗,不过备忘嘛,此方法还是要贴出来

- (UIView *)inputAccessoryView {
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 30)];
    [button setTitle:@"按钮" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    button.backgroundColor = [UIColor redColor];
    
    UIView *containerView = [UIView new];
    containerView.frame = CGRectMake(0, 0, 10, 60);
    containerView.backgroundColor = [UIColor greenColor];
    [containerView addSubview:button];
    
    return containerView;
}

只需吧这段代码贴到ViewController里面,出现的效果如下:



注意那个绿色的view是随着键盘一起出现的,也随着键盘一起消失。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,305评论 4 61
  • Swift版本点击这里欢迎加入QQ群交流: 594119878最新更新日期:18-09-17 About A cu...
    ylgwhyh阅读 25,663评论 7 249
  • 欢迎来到这里,见证我的成长。么么! #^_^# 往期作品: 零基础.国画写意花鸟―NO.14 零基础.国画写意花...
    青墨QINGMO阅读 932评论 0 4
  • 昨夜入梦,梦见20年没见的你,现在的你是啥样都不知道了,为何还能梦见少年时的你?从上一年级我俩就是同桌,5年级时因...
  • 工具:GDB 分析 导致进程异常退出的这两类情况: 第一类:向进程发送信号导致进程异常退出; 第二类:代码错误导致...
    董春磊阅读 2,006评论 0 1