GiGaMaskTime/GIGA/Modules/Mask/View/GiGaFlyingCommitInputView.m

185 lines
6.3 KiB
Objective-C

//
// GiGaFlyingCommitInputView.m
// GIGA
//
// Created by lianxiang on 2018/8/28.
// Copyright © 2018年 com.giga.ios. All rights reserved.
//
#import "GiGaFlyingCommitInputView.h"
static const CGFloat kInputViewH = 44.f;
@interface GiGaFlyingCommitInputView()<UITextFieldDelegate>
@property(nonatomic,strong) UIView *commitInputView;
@property(nonatomic,strong) UITextField * inputTextField;
@end
@implementation GiGaFlyingCommitInputView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
//self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismissSelfViewTap)];
[self addGestureRecognizer:tap];
[self creatInputView];
[self bringSubviewToFront:_commitInputView];
}
return self;
}
-(void)creatInputView{
UIView *commitView =[[UIView alloc] initWithFrame:CGRectMake(0, self.bounds.size.height - kInputViewH - PhoneX_BottomMargin, self.bounds.size.width, kInputViewH)];
self.commitInputView = commitView;
self.commitInputView.backgroundColor = GIGARGB(241, 241, 241, 1);
[self addSubview:self.commitInputView];
UIButton *sendBtn = [UIButton buttonWithType:UIButtonTypeCustom];
CGFloat leftMargin = 10;
sendBtn.frame = CGRectMake(self.frame.size.width - leftMargin - 60,commitView.frame.size.height /2 - 20,60,40);
[sendBtn addTarget:self action:@selector(sendBtnAction:) forControlEvents:UIControlEventTouchUpInside];
//[sendBtn setTitle:@"发送" forState:UIControlStateNormal];
[sendBtn setImage:[UIImage imageNamed:@"ic_send"] forState:UIControlStateNormal];
[sendBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
sendBtn.layer.masksToBounds = YES;
sendBtn.layer.cornerRadius = 4;
[commitView addSubview:sendBtn];
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(44, commitView.frame.size.height /2 -15,self.frame.size.width - sendBtn.frame.size.width - 44 - 10 - leftMargin, 30)];
textField.backgroundColor = [UIColor whiteColor];
textField.placeholder = @"请输入弹幕";
textField.layer.shadowColor = GIGARGB(190, 190, 190, 1).CGColor;
textField.layer.shadowOffset = CGSizeMake(5, 5);
textField.layer.shadowOpacity = 0.5;
textField.layer.shadowRadius = 4;
textField.layer.masksToBounds = YES;
textField.layer.cornerRadius = 2;
textField.layer.borderWidth = 1;
textField.layer.borderColor = [UIColor whiteColor].CGColor;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.delegate = self;
textField.returnKeyType = UIReturnKeySend;
UIView *textLeftview = [[UIView alloc] initWithFrame:CGRectMake(0, 0,10, textField.frame.size.height)];
textField.leftView = textLeftview;
textField.leftViewMode = UITextFieldViewModeAlways;
[commitView addSubview:textField];
self.inputTextField = textField;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
-(void)setPalceholderText:(NSString *)palceholderText
{
_palceholderText = palceholderText;
}
-(void)dismissSelfViewTap{
//[self removeFromSuperview];
[self.inputTextField resignFirstResponder];
}
-(void)sendBtnAction:(UIButton *)btn{
[self sendCommend];
}
-(void)sendCommend{
if (self.inputTextField.text.length > 200) {
[[UIApplication sharedApplication].keyWindow makeToast:@"The comments should not exceed 200 words" duration:1 position:CSToastPositionCenter];
return;
}
if (self.inputTextField.text.length == 0) {
[[UIApplication sharedApplication].keyWindow makeToast:@"弹幕不能为空" duration:1 position:CSToastPositionCenter];
return;
}
if (self.SendCommentHandler) {
self.SendCommentHandler(self.inputTextField.text);
}
if (![self.inputTextField isFirstResponder]) return;
[self dismissKeyboard];
}
- (void)dismissKeyboard
{
if ([self.inputTextField becomeFirstResponder]) {
[self.inputTextField resignFirstResponder];
}
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;
{
if (textField ==self.inputTextField) {
textField.placeholder = _palceholderText;
}
return YES;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
if (textField ==self.inputTextField) {
[self sendCommend];
}
return YES;
}
- (void)keyboardWillShow:(NSNotification*)notification {
// get keyboard size and loctaion
CGRect keyboardBounds;
[[notification.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue: &keyboardBounds];
NSNumber *duration = [notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSNumber *curve = [notification.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];
// Need to translate the bounds to account for rotation.
keyboardBounds = [self convertRect:keyboardBounds toView:nil];
// get a rect for the textView frame
CGRect commentInputViewFrame = self.commitInputView.frame;
commentInputViewFrame.origin.y = self.bounds.size.height - (keyboardBounds.size.height + commentInputViewFrame.size.height);
// animations settings
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:[duration doubleValue]];
[UIView setAnimationCurve:[curve intValue]];
// set views with new info
self.commitInputView.frame = commentInputViewFrame;
[UIView commitAnimations];
}
- (void) keyboardWillHide:(NSNotification *)note
{
self.commitInputView.frame = CGRectMake(0, self.bounds.size.height - kInputViewH - PhoneX_BottomMargin, self.bounds.size.width, kInputViewH);
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
self.commitInputView = nil;
self.inputTextField= nil;
}
@end