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

96
SlimMan
0.1 2015.09.17 10:52* 字数 321

首先说明的是我这篇文章就是把这位大神的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是随着键盘一起出现的,也随着键盘一起消失。

日记本
Web note ad 2