×

iOS身份证键盘

96
Harvey002
2016.07.14 16:22* 字数 216

JYJKeyBoard

gitHub地址 https://github.com/jiangyongjian/JYJKeyBoard

JYJKeyBoard.gif

需求是灵活的,人也是灵活的。经常在项目中遇到要输入身份证,但是恶心的是,有个X。下文是自己在系统的键盘上加了个X,给用户带来完美体验。

一.监听键盘弹出隐藏,在viewDidLoad方法中

    // 注册通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

二.在keyboardWillShow:方法中做添加X,执行动画

    // 获取到最上层的window,这句代码很关键
    UIWindow *tempWindow = [[[UIApplication sharedApplication] windows] lastObject];
    [tempWindow addSubview:doneButton];

    // 添加动画
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:animationDuration];
    [UIView setAnimationCurve:animationCurve];
    doneButton.transform = CGAffineTransformTranslate(doneButton.transform, 0, -kbHeight);
    self.button.transform = CGAffineTransformTranslate(self.button.transform, 0, -kbHeight);
    [UIView commitAnimations];

三. keyboardWillHide:方法中隐藏

    self.doneButton.transform = CGAffineTransformIdentity;
    self.button.transform = CGAffineTransformIdentity;

四.最后别忘记注销通知

    //注销通知
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];

demo我放在gitHub上了,可以去下载看看。https://github.com/jiangyongjian/JYJKeyBoard

由于我没有ios7模拟器,暂时不知道在iOS7上面是否有bug,QQ 453255376, 小弟不才,出现bug希望大家踊跃联系我,把程序写的更好。有更好的想法,有不明白的问我。thanks

文章
Web note ad 1