アニメーションを実行するために、特定の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の1つを編集し始めると、オブザーバーが追加され、showKeyboardメソッドのアニメーションが実行されます。これは、UItextViewの一般的な詳細と同じではありません。編集を開始すると、コードが呼び出されていないかのようにアニメーションが実行されませんが、呼び出されてオブザーバーが追加されます(これはNSlogから理解できました)。重要なのは、UItextViewとUItextFieldのコードが同じであるということです。では、一方が機能し、もう一方が機能しないのはなぜでしょうか。