我想检测某些 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 语句中的 UItextField 之一时,将添加观察者并运行 showKeyboard 方法中的动画。这与 UItextView 一般细节不同。当我开始编辑它时,动画没有运行,好像代码没有被调用,但是它被调用了,并且添加了观察者(我从 NSlog 理解了这一点)。关键是 UItextView 和 UItextField 的代码是相同的,那么如何一个工作而另一个不工作呢?