1. 程式人生 > >iOS系統鍵盤上的輸入框簡單實現

iOS系統鍵盤上的輸入框簡單實現

#import <UIKit/UIKit.h>

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;

}