iOS系统键盘上的输入框简单实现

 

 

 

 

iOS系统键盘上的输入框简单实现_第1张图片

#import

 

NS_ASSUME_NONNULL_BEGIN

 

@interface WYPSeparateSetCodeView : UIView

 

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomViewLayout;

 

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

 

@property (weak, nonatomic) IBOutlet UITextField *phoneNumTextFiled;

 

@property (weak, nonatomic) IBOutlet UITextField *codeNumTextFiled;

 

@property (weak, nonatomic) IBOutlet UIButton *sendBtn;

 

+(instancetype)wYPSeparateSetCodeView;

@end

 

NS_ASSUME_NONNULL_END

 

 

#import "WYPSeparateSetCodeView.h"

 

@implementation WYPSeparateSetCodeView

+(instancetype)wYPSeparateSetCodeView{

    return [[[NSBundle mainBundle]loadNibNamed:@"WYPSeparateSetCodeView" owner:nil options:nil]lastObject];

}

-(void)layoutSubviews{

    [super layoutSubviews];

    self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];

}

#import "WYPSeparateSetCodeView.h"

 

@property (nonatomic, strong) WYPSeparateSetCodeView * setCodeView;

 

#pragma mark - lazy

-(WYPSeparateSetCodeView *)setCodeView{

    if (!_setCodeView) {

        _setCodeView = [WYPSeparateSetCodeView wYPSeparateSetCodeView];

        _setCodeView.width = SCREEN_WIDTH;

        self.setCodeView.bottomViewLayout.constant = 0;

        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapCodeViewClick)];

        [_setCodeView addGestureRecognizer:tap];

        [_setCodeView.sendBtn addTarget:self action:@selector(sendCodeBtnClicked:) forControlEvents:UIControlEventTouchUpInside];

        

    }

    return _setCodeView;

}

    IQKeyboardManager *manager = [IQKeyboardManager sharedManager];

    manager.enable = NO;//YES是激活NO禁止这个类

 

    //监听键盘弹出或收回通知

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardChange:) name:UIKeyboardWillChangeFrameNotification object:nil];

 

    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;

    self.setCodeView.frame = keyWindow.frame;

    [keyWindow addSubview:self.setCodeView];

    self.setCodeView.hidden = YES;

//当键盘frame改变(弹出和收回)时的操作:

- (void)keyboardChange:(NSNotification *)note{

    //拿到键盘弹出的frame

    CGRect frame = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];

    //键盘弹出所需时长

    CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];

    //修改底部输入框的约束

    [UIView animateWithDuration:0.5 animations:^{

        self.setCodeView.bottomView.y = frame.origin.y - 180;

    } completion:^(BOOL finished) {

    }];

    //添加输入框弹出和收回动画

    [UIView animateWithDuration:duration animations:^{

        //立即刷新进行重新布局

        [self.setCodeView layoutIfNeeded];

    }];

}

 

//移除通知

- (void)dealloc{

    [[NSNotificationCenter defaultCenter] removeObserver:self];

}

 

-(void)createAcountCoudeView{

    self.setCodeView.hidden = NO;

    [self.setCodeView.phoneNumTextFiled becomeFirstResponder];

}

-(void)tapCodeViewClick{

    [self codeViewHiddenAndEndEditing];

}

-(void)sendCodeBtnClicked:(UIButton *)sender{

    [self codeViewHiddenAndEndEditing];

}

-(void)codeViewHiddenAndEndEditing{

    [self.setCodeView endEditing:YES];

    self.setCodeView.hidden = YES;

}

 

你可能感兴趣的:(iOS)