TextInputAlertView.m 5.3 KB
//
//  TextInputAlertView.m
//  DreamSleep
//
//  Created by peter on 2022/9/26.
//

#import "TextInputAlertView.h"
#import "MKPPlaceholderTextView.h"
#import <IQKeyboardManager/IQKeyboardManager.h>

@interface TextInputAlertView ()
@property (nonatomic, strong) UIView *alertView;
@property (nonatomic, strong) MKPPlaceholderTextView *textView;
@property (nonatomic, strong) UIButton *cancelBtn;
@property (nonatomic, strong) UIButton *replyBtn;
@property (nonatomic, assign) CGFloat duration;
@end

@implementation TextInputAlertView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.dk_backgroundColorPicker = DKColorPickerWithColors(ColorFromHexA(0x161E38, .6), DSClearColor, DSWhite);
        
        [self addSubview:self.alertView];
        [self.alertView addSubview:self.cancelBtn];
        [self.alertView addSubview:self.replyBtn];
        [self.alertView addSubview:self.textView];
        
        [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.alertView).offset(25);
            make.top.equalTo(self.alertView).offset(18);
        }];
        [self.replyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.alertView).offset(-15);
            make.centerY.equalTo(self.cancelBtn);
            make.size.mas_equalTo(CGSizeMake(44, 26));
        }];
        [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.alertView).offset(15);
            make.right.equalTo(self.alertView).offset(-15);
            make.top.equalTo(self.alertView).offset(53);
            make.bottom.equalTo(self.alertView);
        }];
        
        [[IQKeyboardManager sharedManager] setEnable:NO];
        [[IQKeyboardManager sharedManager] setEnableAutoToolbar:NO];
        [[IQKeyboardManager sharedManager] setShouldResignOnTouchOutside:NO];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillAppear:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillDisappear:) name:UIKeyboardWillHideNotification object:nil];
    }
    return self;
}

- (void)keyboardWillAppear:(NSNotification *)noti {
    NSDictionary *info = [noti userInfo];
    NSValue *value = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
    self.duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    CGSize keyboardSize = [value CGRectValue].size;
    [UIView animateWithDuration:self.duration animations:^{
        self.alertView.y = kScreenHeight - self.alertView.height - keyboardSize.height;
    }];
}

- (void)keyboardWillDisappear:(NSNotification *)noti {
    [UIView animateWithDuration:self.duration animations:^{
        self.alertView.y = kScreenHeight;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)dealloc {
    [[IQKeyboardManager sharedManager] setEnable:YES];
    [[IQKeyboardManager sharedManager] setEnableAutoToolbar:YES];
    [[IQKeyboardManager sharedManager] setShouldResignOnTouchOutside:YES];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

#pragma mark - public
- (void)showTextInputAlertView {
    [DSKeyWindow addSubview:self];
    [self.textView becomeFirstResponder];
}

#pragma mark - private
- (void)cancelAction {
    [self.textView endEditing:YES];
}

- (void)replyAction {
    // 1、调用回复接口
    
    // 2、隐藏输入框
    [self.textView endEditing:YES];
}

#pragma mark - lazy
- (UIView *)alertView {
    if (!_alertView) {
        _alertView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight, kScreenWidth, 201)];
        [_alertView setCornerRadiusRect:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadius:24];
        _alertView.dk_backgroundColorPicker = DKColorPickerWithColors(DSWhite, DarkColor, DSWhite);
    }
    return _alertView;
}

- (MKPPlaceholderTextView *)textView {
    if (!_textView) {
        _textView = [MKPPlaceholderTextView new];
        _textView.font = SysFont(14);
        [_textView cornerRadius:12];
        _textView.textContainerInset = UIEdgeInsetsMake(15, 8, 33, 8);
        _textView.dk_textColorPicker = DKColorPickerWithColors(SmallTextColor, DSWhite, DSWhite);
        _textView.dk_backgroundColorPicker = DKColorPickerWithColors(ColorFromHex(0xF0F0F0), CornerViewDarkColor, DSWhite);
    }
    return _textView;
}

- (UIButton *)cancelBtn {
    if (!_cancelBtn) {
        _cancelBtn = [UIButton btnWithTitle:@"取消" font:SysFont(14)];
        [_cancelBtn dk_setTitleColorPicker:DKColorPickerWithColors(SubTitleColor, DSWhite, DSWhite) forState:UIControlStateNormal];
        [_cancelBtn addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelBtn;
}

- (UIButton *)replyBtn {
    if (!_replyBtn) {
        _replyBtn = [UIButton btnWithTitle:@"回复" titleColor:DSWhite font:SysFont(12) bgColor:BrandColor];
        [_replyBtn cornerRadius:13];
        [_replyBtn addTarget:self action:@selector(replyAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _replyBtn;
}

@end