[英]Hide and show IOS Keyboard when editing UItextview
我想检测何时编辑某些UItextField和UItextView以便运行动画。 我以这种方式解决了这个问题:
-(void)textFieldDidBeginEditing: (UITextField *)textField{
NSLog(@"sowing keyboard");
if ((textField == timebegin)||(textField == timeend)||(textField == number)||(textField == costlist)||(textField == costnormal)||(textField == newmessagename)||(textField == noteView)) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
}else{
}
}
-(void)textFieldDidEndEditing: (UITextField *)textField{
NSLog(@"hiding keyboard");
}
-(void)textViewDidBeginEditing: (UITextView *)textView{
NSLog(@"sowing keyboard");
if (textView == generalDetails) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
}else{
}
}
-(void)textViewDidEndEditing: (UITextView *)textView{
NSLog(@"hiding keyboard");
}
- (void)keyboardDidHide:(NSNotification *)aNotification {
[[NSNotificationCenter defaultCenter] removeObserver:self name: UIKeyboardDidHideNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name: UIKeyboardWillShowNotification object:nil];
NSLog(@"hiding keyboard");
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.27];
scroll1.frame = CGRectMake(0, -340, 768, 1004);
[UIView commitAnimations];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.37];
scroll2.frame = CGRectMake(0, 678, 768, 1004);
[UIView commitAnimations];
[self.mapView setHidden:NO];
addImageForCommentingButton.hidden = NO;
//fade in
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationDelegate:self];
[self.mapView setAlpha:1.0];
[addImageForCommentingButton setAlpha: 1.0];
[UIView commitAnimations];
}
- (void)keyboardWillShow:(NSNotification *)notification {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.27];
scroll1.frame = CGRectMake(0, -604, 768, 1004);
[UIView commitAnimations];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.47];
scroll2.frame = CGRectMake(0, 414, 768, 1004);
[UIView commitAnimations];
[UIView animateWithDuration:1.0
animations:^{
self.mapView.alpha=0.0;
[addImageForCommentingButton setAlpha: 0.0];
}
completion:^(BOOL finished){
self.mapView.hidden=YES;
addImageForCommentingButton.hidden = YES;
}];
}
当我开始编辑名称在if语句中的UItextFields之一时,将添加观察者,并且showKeyboard方法中的动画将运行。 这与UItextView常规详细信息不同。 当我开始对其进行编辑时,该动画不会运行,就像没有调用该代码一样,但是会调用该动画,并添加观察者(我从NSlog理解了这一点)。 关键是,UItextView和UItextField的代码相同,那么一个如何工作而另一个却不行呢?
通过这样做解决了:
-(void)textViewDidBeginEditing: (UITextView *)textView{
NSLog(@"sowing keyboard textView");
if (textView == generalDetails) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[self keyboardWillShow];
}else{
}
}
很奇怪,我认为根本没有调用方法keyboardWillShow
如果添加[self keyboardWillShow]
解决了该问题,则可能是您的方法未正确实施。 keyboardWillShow
和keyboardWillShow:
是两种不同的方法。
如果要调用keyboardWillShow:
通知,则必须按如下所示对其进行定义:
- (void) keyboardWillShow: (NSNotification*) notification
{
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.