0

我有很多 UItextField,以及他们喜欢的动画:

-(void)textFieldDidBeginEditing: (UITextField *)textField{
NSLog(@"sowing keyboard");

[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;
                 } 
                 completion:^(BOOL finished){
                     self.mapView.hidden=YES;
                 }];


}

 -(void)textFieldDidEndEditing: (UITextField *)textField{
NSLog(@"hiding keyboard");

if (scroll2.frame.origin.y != 414) {

[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.47];
scroll2.frame = CGRectMake(0, 678, 768, 1004);
[UIView commitAnimations];

[self.mapView setHidden:NO];

//fade in
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationDelegate:self];

[self.mapView setAlpha:1.0];

[UIView commitAnimations];

    }
}

问题是,如果我正在编辑一个字段,并且从我触摸并开始编辑另一个字段,则会调用代码 didEnd 和 DidBegin。我怎么能阻止它?我希望这些动画仅在我开始编辑文本字段并且没有正在编辑文本字段时出现,并且当我需要隐藏键盘时出现 DidEnd(当用户在 iPad 上按下 DownKey 时)

有任何想法吗??

我试过这个:

if (scroll2.frame.origin.y != 414) {

...它可以工作,但是当我按下重新运行键时,没有动画出现。

4

1 回答 1

1

有问题的 textField 连同参数一起发送给 textFieldDidBeginEditing。

添加一个 if () 块来测试 textField1 和 textField2 即:

-(void)textFieldDidBeginEditing:(UITextField *)textField {
if (textField == textField1) {
    //do animation linked to textField1
}
if (textField == textField2) {
    //do animation linked to textField2
}
}

另外,你应该实现这个方法

-(BOOL)textFieldShouldReturn:(UITextField *)textField {
    //handles the enter/done key
    //perform whatever action for when user touches return
    if (textField == textField1) {
        [textField2 becomeFirstResponder];
    } else {
        [textField1 becomeFirstResponder];
    }
    return YES or NO;
}
于 2012-09-07T15:13:42.620 回答