0

アニメーションを実行するために、特定の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のコードが同じであるということです。では、一方が機能し、もう一方が機能しないのはなぜでしょうか。

4

2 に答える 2

0

追加し[self keyboardWillShow]て問題を修正した場合は、メソッドが正しく実装されていない可能性があります。keyboardWillShowkeyboardWillShow:は2つの異なる方法です。

通知を呼び出す場合keyboardWillShow:は、次のように定義する必要があります。

- (void) keyboardWillShow: (NSNotification*) notification
{
}
于 2013-02-21T18:38:45.110 に答える
0

これを行うことで解決しました:

-(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メソッドがまったく呼び出されていなかったと思います

于 2013-02-02T14:36:36.180 に答える