190 lines
7.4 KiB
Objective-C
190 lines
7.4 KiB
Objective-C
//
|
|
// SendMessageView.m
|
|
// xplan-ios
|
|
//
|
|
// Created by zu on 2021/11/28.
|
|
//
|
|
|
|
#import "SendMessageView.h"
|
|
#import <Masonry/Masonry.h>
|
|
#import <NIMSDK/NIMSDK.h>
|
|
#import "ThemeColor.h"
|
|
#import "XPMacro.h"
|
|
#import "UIImage+Utils.h"
|
|
|
|
@interface SendMessageView ()
|
|
|
|
@property (nonatomic, strong) UIStackView *stackView;
|
|
@property (nonatomic, strong) UIView *inputBackgroundView;
|
|
@property (nonatomic, strong) UITextField *inputView;
|
|
@property (nonatomic, strong) UIButton *sendButton;
|
|
|
|
@end
|
|
|
|
#define VIEW_HEIGHT (kSafeAreaBottomHeight + 50)
|
|
|
|
@implementation SendMessageView
|
|
|
|
- (void)dealloc {
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
|
}
|
|
|
|
- (instancetype)init {
|
|
self = [super initWithFrame:CGRectMake(0, KScreenHeight - VIEW_HEIGHT, KScreenWidth, VIEW_HEIGHT)];
|
|
if (self) {
|
|
[self addNotification];
|
|
[self initViews];
|
|
[self initLayout];
|
|
}
|
|
return self;
|
|
}
|
|
|
|
- (void)sendMessage:(UIButton *)sender {
|
|
if (self.delegate && [self.delegate respondsToSelector:@selector(sendTextMessage:)]) {
|
|
[self.delegate sendTextMessage:self.inputView.text];
|
|
}
|
|
self.inputView.text = @"";
|
|
self.sendButton.enabled = self.inputView.text.length > 0;
|
|
}
|
|
|
|
- (void)keyboardWillShow:(NSNotification *)notification {
|
|
[self.superview bringSubviewToFront:self];
|
|
NSDictionary *info = [notification userInfo];
|
|
CGRect keyboardFrame = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
|
|
NSNumber *curve = [info objectForKey:UIKeyboardAnimationCurveUserInfoKey];
|
|
CGFloat duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
|
|
|
|
CGRect newFrame = self.frame;
|
|
newFrame.origin.y = self.superview.frame.size.height - keyboardFrame.size.height - 50;
|
|
|
|
UIViewAnimationOptions options = (UIViewAnimationOptions)curve << 16;
|
|
[UIView animateWithDuration:duration delay:0.0 options:options animations:^{
|
|
self.frame = newFrame;
|
|
[self mas_remakeConstraints:^(MASConstraintMaker *make) {
|
|
make.left.right.equalTo(self.superview);
|
|
make.bottom.equalTo(self.superview).offset(-keyboardFrame.size.height + kSafeAreaBottomHeight);
|
|
make.height.equalTo(@(VIEW_HEIGHT));
|
|
}];
|
|
} completion:nil];
|
|
}
|
|
|
|
- (void)keyboardWillHidden:(NSNotification *)notification {
|
|
NSDictionary *info = [notification userInfo];
|
|
NSNumber *curve = [info objectForKey:UIKeyboardAnimationCurveUserInfoKey];
|
|
CGFloat duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
|
|
|
|
CGRect newFrame = self.frame;
|
|
newFrame.origin.y = self.superview.frame.size.height - VIEW_HEIGHT;
|
|
|
|
UIViewAnimationOptions options = (UIViewAnimationOptions)curve << 16;
|
|
[UIView animateWithDuration:duration delay:0.0 options:options animations:^{
|
|
self.frame = newFrame;
|
|
[self mas_remakeConstraints:^(MASConstraintMaker *make) {
|
|
make.bottom.left.right.equalTo(self.superview);
|
|
make.height.equalTo(@(VIEW_HEIGHT));
|
|
}];
|
|
} completion:nil];
|
|
}
|
|
|
|
-(void)textFieldEditChanged:(NSNotification *)notification{
|
|
self.sendButton.enabled = self.inputView.text.length > 0;
|
|
}
|
|
|
|
- (void)initViews {
|
|
self.backgroundColor = [ThemeColor appCellBackgroundColor];
|
|
[self addSubview:self.stackView];
|
|
[self.inputBackgroundView addSubview:self.inputView];
|
|
[self.stackView addArrangedSubview:self.inputBackgroundView];
|
|
[self.stackView addArrangedSubview:self.sendButton];
|
|
}
|
|
|
|
- (void)initLayout {
|
|
[self.stackView mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.left.mas_equalTo(self).offset(15);
|
|
make.right.mas_equalTo(self).offset(-15);
|
|
make.top.mas_equalTo(self);
|
|
make.bottom.mas_equalTo(self).offset(-kSafeAreaBottomHeight);
|
|
}];
|
|
|
|
[self.sendButton mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.width.mas_equalTo(50);
|
|
make.top.bottom.mas_equalTo(self.stackView).inset(10);
|
|
}];
|
|
|
|
[self.inputBackgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.height.mas_equalTo(36);
|
|
make.left.mas_equalTo(self.stackView);
|
|
make.right.mas_equalTo(self.sendButton.mas_left).offset(-15);
|
|
}];
|
|
|
|
[self.inputView mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.left.mas_equalTo(self.inputBackgroundView).offset(8);
|
|
make.right.mas_equalTo(self.inputBackgroundView).offset(-8);
|
|
make.centerY.mas_equalTo(self.inputBackgroundView);
|
|
}];
|
|
|
|
[self mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.height.mas_equalTo(VIEW_HEIGHT);
|
|
}];
|
|
}
|
|
|
|
- (void)addNotification {
|
|
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHidden:) name:UIKeyboardWillHideNotification object:nil];
|
|
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
|
|
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldEditChanged:) name:UITextFieldTextDidChangeNotification object:self.inputView];
|
|
}
|
|
|
|
- (UIStackView *)stackView {
|
|
if (!_stackView) {
|
|
_stackView = [[UIStackView alloc] init];
|
|
_stackView.axis = UILayoutConstraintAxisHorizontal;
|
|
_stackView.distribution = UIStackViewDistributionFill;
|
|
_stackView.alignment = UIStackViewAlignmentCenter;
|
|
_stackView.spacing = 10;
|
|
}
|
|
return _stackView;
|
|
}
|
|
|
|
- (UITextField *)inputView{
|
|
if (!_inputView) {
|
|
_inputView = [[UITextField alloc] init];
|
|
NSAttributedString * attribute = [[NSAttributedString alloc] initWithString:@"请输入消息" attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15], NSForegroundColorAttributeName :UIColorFromRGB(0x555574)}];
|
|
_inputView.attributedPlaceholder = attribute;
|
|
_inputView.borderStyle = UITextBorderStyleNone;
|
|
_inputView.tintColor = [ThemeColor appMainColor];
|
|
_inputView.textColor = [ThemeColor mainTextColor];
|
|
_inputView.font = [UIFont systemFontOfSize:13.f];
|
|
[_inputView setContentHuggingPriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];
|
|
|
|
}
|
|
return _inputView;
|
|
}
|
|
|
|
- (UIView *)inputBackgroundView {
|
|
if (!_inputBackgroundView) {
|
|
_inputBackgroundView = [[UIView alloc] init];
|
|
_inputBackgroundView.backgroundColor = UIColorFromRGB(0x35354B);
|
|
_inputBackgroundView.layer.masksToBounds = YES;
|
|
_inputBackgroundView.layer.cornerRadius = 4.f;
|
|
}
|
|
return _inputBackgroundView;
|
|
}
|
|
|
|
- (UIButton *)sendButton{
|
|
if (!_sendButton) {
|
|
_sendButton = [[UIButton alloc] init];
|
|
[_sendButton setTitle:@"发送" forState:UIControlStateNormal];
|
|
_sendButton.titleLabel.textColor = [ThemeColor mainTextColor];
|
|
_sendButton.titleLabel.font = [UIFont systemFontOfSize:15];
|
|
[_sendButton setBackgroundImage:[UIImage imageWithColor:[ThemeColor disableButtonColor] ]forState:UIControlStateDisabled];
|
|
[_sendButton setBackgroundImage:[UIImage gradientColorImageFromColors:@[[ThemeColor confirmButtonGradientStartColor], [ThemeColor confirmButtonGradientEndColor]] gradientType:GradientTypeLeftToRight imgSize:CGSizeMake(10, 10)] forState:UIControlStateNormal];
|
|
_sendButton.enabled = NO;
|
|
_sendButton.layer.cornerRadius = 5.0;
|
|
_sendButton.layer.masksToBounds = YES;
|
|
[_sendButton addTarget:self action:@selector(sendMessage:) forControlEvents:UIControlEventTouchUpInside];
|
|
}
|
|
return _sendButton;
|
|
}
|
|
|
|
@end
|