iOS 键盘总结

来源:互联网 发布:工厂办公软件 编辑:程序博客网 时间:2024/06/07 10:18

  • iOS 键盘
    • 键盘类型
    • 键盘 return 按钮类型
    • 键盘外观
    • 键盘 return 按钮点击事件
    • 监听键盘弹出收回事件
      • Notifications
      • Keyboard Notification User Info Keys
      • 实现一个键盘弹出的demo

iOS 键盘

iOS 键盘是苹果自带的系统视图,是一个Window。

键盘类型

typedef enum : NSInteger {   UIKeyboardTypeDefault,   UIKeyboardTypeASCIICapable,   UIKeyboardTypeNumbersAndPunctuation,   UIKeyboardTypeURL,   UIKeyboardTypeNumberPad,   UIKeyboardTypePhonePad,   UIKeyboardTypeNamePhonePad,   UIKeyboardTypeEmailAddress,   UIKeyboardTypeDecimalPad,   UIKeyboardTypeTwitter,   UIKeyboardTypeWebSearch,   UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable } UIKeyboardType;

各种类型的图像:
UIKeyboardTypeASCIICapable
UIKeyboardTypeASCIICapable

UIKeyboardTypeNumbersAndPunctuation
UIKeyboardTypeNumbersAndPunctuation

UIKeyboardTypeURL
UIKeyboardTypeURL

UIKeyboardTypeNumberPad
UIKeyboardTypeNumberPad

UIKeyboardTypePhonePad
UIKeyboardTypePhonePad

UIKeyboardTypeNamePhonePad
UIKeyboardTypeNamePhonePad

UIKeyboardTypeEmailAddress
UIKeyboardTypeEmailAddress

UIKeyboardTypeDecimalPad
UIKeyboardTypeDecimalPad

UIKeyboardTypeTwitter
UIKeyboardTypeTwitter

UIKeyboardTypeWebSearch
UIKeyboardTypeWebSearch

键盘 return 按钮类型

typedef enum : NSInteger {   UIReturnKeyDefault,   UIReturnKeyGo,   UIReturnKeyGoogle,   UIReturnKeyJoin,   UIReturnKeyNext,   UIReturnKeyRoute,   UIReturnKeySearch,   UIReturnKeySend,   UIReturnKeyYahoo,   UIReturnKeyDone,   UIReturnKeyEmergencyCall,   UIReturnKeyContinue,} UIReturnKeyType;

键盘外观

typedef enum : NSInteger {   UIKeyboardAppearanceDefault,   UIKeyboardAppearanceDark,   UIKeyboardAppearanceLight,   UIKeyboardAppearanceAlert,} UIKeyboardAppearance;

键盘 return 按钮点击事件

UITextField 的代理方法如下:

- (BOOL)textFieldShouldReturn:(UITextField *)textField{    [textField resignFirstResponder];    return true; }

UITextView 的代理中的响应方法如下:

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{    if ([text isEqualToString:@"\n"]){ //判断输入的字是否是回车,即按下return        //在这里做你响应return键的代码        return NO; //这里返回NO,就代表return键值失效,即页面上按下return,不会出现换行,如果为yes,则输入页面会换行    }    return YES;}

监听键盘弹出收回事件

Notifications

观察键盘弹出之前的状态,object 为 nil。
UIKeyboardWillShowNotification

观察键盘弹出之后的状态
UIKeyboardDidShowNotification

观察键盘消失之前的状态
UIKeyboardWillHideNotification

观察键盘消失之后的状态
UIKeyboardDidHideNotification

观察键盘 frame 改变之前的状态
UIKeyboardWillChangeFrameNotification

观察键盘 frame 改变之后的状态
UIKeyboardDidChangeFrameNotification

Keyboard Notification User Info Keys

获得键盘开始弹出位置的坐标,返回的是NSValue对象
NSString *const UIKeyboardFrameBeginUserInfoKey;

获得键盘弹出结束之后的坐标,返回的是NSValue对象
NSString *const UIKeyboardFrameEndUserInfoKey;

获得键盘弹出或者收回的动画方式( UIViewAnimationCurve 常量),返回NSNumber对象
NSString *const UIKeyboardAnimationCurveUserInfoKey;

获得动画持续的时间(单位s),返回NSNumber对象
NSString *const UIKeyboardAnimationDurationUserInfoKey;

获得一个Bool值判断当前的键盘是不是当前应用的,返回NSNumber对象
NSString *const UIKeyboardIsLocalUserInfoKey;

实现一个键盘弹出的demo

实现效果如图:
这里写图片描述
代码片段如下:

- (void)viewDidLoad {    [super viewDidLoad];    // 对屏幕添加手势    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];    [self.view addGestureRecognizer:tap];    // 添加观察者    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyBoardAction:) name:UIKeyboardWillShowNotification object:nil];    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyBoardAction:) name:UIKeyboardDidShowNotification object:nil];    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyBoardAction:) name:UIKeyboardWillHideNotification object:nil];    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyBoardAction:) name:UIKeyboardDidHideNotification object:nil];    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyBoardAction:) name:UIKeyboardWillChangeFrameNotification object:nil];    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyBoardAction:) name:UIKeyboardDidChangeFrameNotification object:nil];}- (void)keyBoardAction:(NSNotification *)notifation {    if ([notifation.name isEqual:UIKeyboardWillShowNotification]) {        NSLog(@"键盘将要弹出");        // 获取键盘的各种信息        CGRect startFrame = [[notifation.userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey]CGRectValue];        CGRect endFrame = [[notifation.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue];        NSNumber *animationCurve = [notifation.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];        double animationTimes = [[notifation.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]doubleValue];        BOOL isCurrentViewKeyBoard = [[notifation.userInfo objectForKey:UIKeyboardIsLocalUserInfoKey]boolValue];        NSLog(@"startFrame = %@, endFrame = %@, animationCurve = %@, animationTimes = %f, isCurrentViewKeyBoard = %d", [NSValue valueWithCGRect:startFrame] , [NSValue valueWithCGRect:endFrame], animationCurve, animationTimes, (isCurrentViewKeyBoard == 1)? 1 : 0);        CGFloat hight = startFrame.size.height;        [UIView animateWithDuration:animationTimes animations:^{            self.sendTextView.transform = CGAffineTransformMakeTranslation(0, -hight);        }];    }    if ([notifation.name isEqual:UIKeyboardDidShowNotification]) {        NSLog(@"键盘已经弹出");        NSLog(@"self.sendTextView.frame = %@", [NSValue valueWithCGRect:self.sendTextView.frame]);    }    if ([notifation.name isEqual:UIKeyboardWillHideNotification]) {        NSLog(@"键盘将要收回");        double animationTimes = [[notifation.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]doubleValue];        [UIView animateWithDuration:animationTimes animations:^{            self.sendTextView.transform = CGAffineTransformMakeTranslation(0, 0);        }];    }    if ([notifation.name isEqual:UIKeyboardDidHideNotification]) {        NSLog(@"键盘已经收回");    }    if ([notifation.name isEqual:UIKeyboardWillChangeFrameNotification]) {        NSLog(@"键盘将要改变Frame");    }    if ([notifation.name isEqual:UIKeyboardDidChangeFrameNotification]) {        NSLog(@"键盘已经改变frame");    }}- (void)tapAction:(id)sender {    [self.tF resignFirstResponder];}

在一个键盘弹出和收回的过程中观察者的先后执行如图所示:
pushHeyboard

0 0
原创粉丝点击