自定义输入提醒框(OC)

自定义输入提醒框(OC)_第1张图片
2017-01-10 14_53_04.gif
#import 
/** 定义返回结果的Block */
typedef void(^AlertResult)(NSInteger index);

@interface inputAlertView : UIView
/** 点击的结果Block */
@property (nonatomic, copy) AlertResult resultIndex;

- (instancetype)initWithTitle:(NSString *)title cancelTitle:(NSString *)cancelTitle sureTitle:(NSString *)sureTitle;

- (void)show;
@end
#import "inputAlertView.h"
#import "UIView+FCExtension.h"
/* AlertView的宽度 */
#define kInputAlertViewW 300
#define kInputAlertViewH 200
/* 间距 */
#define kSpace 10.0
#define SCREENHEIGHT [UIScreen mainScreen].bounds.size.height
#define SCREENWIDTH [UIScreen mainScreen].bounds.size.width

@interface inputAlertView()
/** 弹窗 */
@property (retain, nonatomic) UIView *inputAlertView;
/** 标题 */
@property (retain, nonatomic) UILabel *titleLbl;
/** 确认按钮 */
@property (nonatomic, retain) UIButton *sureBtn;
/** 取消按钮 */
@property (nonatomic, retain) UIButton *cancleBtn;
/** 横线 */
@property (nonatomic, retain) UIView *hLineView;
/** 竖线 */
@property (nonatomic, retain) UIView *vLineView;
@property (nonatomic, strong) UITextField *textField1;
@property (nonatomic, strong) UITextField *textField2;
@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSString *cancelTitle;
@property (nonatomic, strong) NSString *sureTitle;

@end

@implementation inputAlertView

#pragma mark - 懒加载
- (UILabel *)titleLbl
{
    if (!_titleLbl) {
        _titleLbl = [[UILabel alloc] init];
        _titleLbl.text = self.title;
        _titleLbl.textAlignment = NSTextAlignmentCenter;
        _titleLbl.font = [UIFont boldSystemFontOfSize:16];
    }
    return _titleLbl;
}

- (UIView *)inputAlertView
{
    if (!_inputAlertView) {
        _inputAlertView = [[UIView alloc] init];
        _inputAlertView.backgroundColor = [UIColor whiteColor];
        _inputAlertView.layer.cornerRadius = 10.0;
        _inputAlertView.clipsToBounds = YES;
    }
    return _inputAlertView;
    
}

- (UIButton *)sureBtn
{
    if (!_sureBtn) {
        _sureBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        _sureBtn.backgroundColor = [UIColor whiteColor];
        _sureBtn.tag = 2;
        [_sureBtn setTitle:self.sureTitle forState:UIControlStateNormal];
        [_sureBtn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sureBtn;
}

- (UIButton *)cancleBtn
{
    if (!_cancleBtn) {
        _cancleBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        _cancleBtn.backgroundColor = [UIColor whiteColor];
        _cancleBtn.tag = 1;
        [_cancleBtn setTitle:self.cancelTitle forState:UIControlStateNormal];
        [_cancleBtn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancleBtn;
}

- (UITextField *)textField1
{
    if (!_textField1) {
        _textField1 = [[UITextField alloc] init];
        _textField1.placeholder = @"请输入账户名...";
        _textField1.borderStyle = UITextBorderStyleRoundedRect;
    }
    return _textField1;
}

- (UITextField *)textField2
{
    if (!_textField2) {
        _textField2 = [[UITextField alloc] init];
        _textField2.placeholder = @"请输入密码...";
        _textField2.borderStyle = UITextBorderStyleRoundedRect;
    }
    return _textField2;
}

- (UIView *)hLineView
{
    if (!_hLineView) {
        _hLineView = [[UIView alloc] init];
        _hLineView.backgroundColor = [UIColor lightGrayColor];
    }
    return _hLineView;
}

- (UIView *)vLineView
{
    if (!_vLineView) {
        _vLineView = [[UIView alloc] init];
        _vLineView.backgroundColor = [UIColor lightGrayColor];
    }
    return _vLineView;
}


#pragma mark - 初始化
- (instancetype)initWithTitle:(NSString *)title cancelTitle:(NSString *)cancelTitle sureTitle:(NSString *)sureTitle
{
    if (self = [super init]) {
        self.cancelTitle = cancelTitle;
        self.sureTitle = sureTitle;
        self.title = title;
        [self setupUI];
        [self observeKeyboard];
    }
    return self;
}

#pragma mark - 设置UI
- (void)setupUI
{
    self.frame = [UIScreen mainScreen].bounds;
    self.backgroundColor = [UIColor colorWithWhite:0.8 alpha:0.6];
    
    [self addSubview:self.inputAlertView];
    self.inputAlertView.frame = CGRectMake(0, 0, kInputAlertViewW, kInputAlertViewH);
    self.inputAlertView.layer.position = self.center;
    
    [self.inputAlertView addSubview:self.titleLbl];
    self.titleLbl.frame = CGRectMake(0, 0, kInputAlertViewW, 40);
    
    [self.inputAlertView addSubview:self.textField1];
    self.textField1.frame = CGRectMake(kSpace, CGRectGetMaxY(self.titleLbl.frame) + kSpace, kInputAlertViewW - 2 * kSpace, 40);
    
    [self.inputAlertView addSubview:self.textField2];
    self.textField2.frame = CGRectMake(kSpace, CGRectGetMaxY(self.textField1.frame) + kSpace, kInputAlertViewW - 2 * kSpace, 40);
    
    [self.inputAlertView addSubview:self.hLineView];
    self.hLineView.frame = CGRectMake(0, CGRectGetMaxY(self.textField2.frame) + 15, kInputAlertViewW, 1);
    
    [self.inputAlertView addSubview:self.cancleBtn];
    self.cancleBtn.frame = CGRectMake(0, CGRectGetMaxY(self.hLineView.frame), kInputAlertViewW / 2, 47);
    
    [self.inputAlertView addSubview:self.vLineView];
    self.vLineView.frame = CGRectMake(kInputAlertViewW / 2, CGRectGetMaxY(self.hLineView.frame), 1, CGRectGetHeight(self.cancleBtn.frame));
    
    [self.inputAlertView addSubview:self.sureBtn];
    self.sureBtn.frame = CGRectMake(kInputAlertViewW / 2 + 1, CGRectGetMaxY(self.hLineView.frame), kInputAlertViewW / 2 - 1, 47);
}


#pragma mark - 监听键盘起落
- (void)observeKeyboard
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
}

- (void)keyboardWillChangeFrame:(NSNotification *)note
{
    // 拿到键盘最终的frame
//    CGRect keyFrame = [note.userInfo[UIKeyboardIsLocalUserInfoKey] CGRectValue];
    // 拿到键盘起落时间
    CGFloat duration = [note.userInfo[UIKeyboardAnimationCurveUserInfoKey] doubleValue];
    [UIView animateWithDuration:duration animations:^{
        self.inputAlertView.fc_y = 150;
    }];
}

#pragma mark - 显示视图
- (void)show
{
    UIWindow *rootWindow = [UIApplication sharedApplication].keyWindow;
    [rootWindow addSubview:self];
    [self creatShowAnimation];
}

/** 创建显示框的动画 */
- (void)creatShowAnimation
{
    self.inputAlertView.layer.position = self.center;
    self.inputAlertView.transform = CGAffineTransformMakeScale(0.20, 0.20);
    [UIView animateWithDuration:0.35 delay:0.02 usingSpringWithDamping:0.4 initialSpringVelocity:10 options:UIViewAnimationOptionCurveLinear animations:^{
        self.inputAlertView.transform = CGAffineTransformMakeScale(1.0, 1.0);
    } completion:^(BOOL finished) {
        
    }];
}

#pragma mark - 点击事件
- (void)buttonClick:(UIButton *)but
{
    // 回调
    if (self.resultIndex) {
        self.resultIndex(but.tag);
    }
    [UIView animateWithDuration:0.55 delay:0.1 usingSpringWithDamping:0.4 initialSpringVelocity:10 options:UIViewAnimationOptionCurveLinear animations:^{
        [self endEditing:YES];
        self.inputAlertView.fc_y = SCREENHEIGHT;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
@end
- (IBAction)inputViewShow {
    
    inputAlertView *input = [[inputAlertView alloc] initWithTitle:@"请登录" cancelTitle:@"取消" sureTitle:@"登录"];
    [input show];
    input.resultIndex = ^(NSInteger index) {
        NSLog(@"%ld",index);
    };
}

你可能感兴趣的:(自定义输入提醒框(OC))