// // GiGaFlyingCommitInputView.m // GIGA // // Created by lianxiang on 2018/8/28. // Copyright © 2018年 com.giga.ios. All rights reserved. // #import "GiGaFlyingCommitInputView.h" @interface GiGaFlyingCommitInputView() @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]; 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 - 60, self.bounds.size.width, 60)]; // commitView.frame = CGRectMake(0, self.frame.size.height-commitView.frame.size.height, self.frame.size.width, commitView.frame.size.height); self.commitInputView = commitView; self.commitInputView.backgroundColor = [UIColor redColor]; [self addSubview:self.commitInputView]; UIButton *sendBtn = [UIButton buttonWithType:UIButtonTypeCustom]; CGFloat topMargin = 5; CGFloat leftMargin = 10; sendBtn.frame = CGRectMake(self.frame.size.width - leftMargin - 60,topMargin,60,40); [sendBtn addTarget:self action:@selector(sendBtnAction:) forControlEvents:UIControlEventTouchUpInside]; [sendBtn setTitle:@"发送" forState:UIControlStateNormal]; [sendBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; sendBtn.backgroundColor = [UIColor purpleColor]; sendBtn.layer.masksToBounds = YES; sendBtn.layer.cornerRadius = 4; [commitView addSubview:sendBtn]; UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 5,self.frame.size.width - sendBtn.frame.size.width - 10*2 - 5, 30)]; textField.backgroundColor = [UIColor lightGrayColor]; textField.clearButtonMode = UITextFieldViewModeWhileEditing; textField.delegate = self; [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]; } -(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:@"Comment content cannot be empty" duration:1 position:CSToastPositionCenter]; return; } if (self.SendCommentHandler) { self.SendCommentHandler(self.inputTextField.text); } [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 - 60, self.bounds.size.width, 60); } - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; self.commitInputView = nil; self.inputTextField= nil; } @end