在各个代理方法中,执行相应的代码。如下
- (void)textViewDidBeginEditing:(UITextView *)textView
{
_isEditing = YES; // 定义的是否处于编辑状态
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
_isEditing = NO;
self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
}
- (void)viewDidLayoutSubviews
{
DLog(@"我执行了");
if (_isEditing) {
CGRect frame = self.commentContent.frame;
int offset = frame.origin.y + 125 - (self.view.frame.size.height - 216.0);//键盘高度216
NSTimeInterval animationDuration = 0.3; // ResizeForKeyBoard
[UIView beginAnimations:@"keyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
[UIView animateWithDuration:animationDuration animations:^{
}];
//将视图的Y坐标向上移动offset个单位,以使下面腾出地方用于软键盘的显示
if (offset >0) {
self.view.frame = CGRectMake(0.0f, -offset, self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}
}
[super viewDidLayoutSubviews];
[self.view layoutSubviews];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[self.commentContent resignFirstResponder];
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if ([text isEqualToString:@"\n"]) {
[textView resignFirstResponder];
}
return YES;
}