在第二个UITextField编辑中显示和隐藏键盘

时间:2012-09-07 15:06:17

标签: ios xcode

我有很多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];

    }
}

问题在于,如果我正在编辑一个字段,并且从他们触摸并开始编辑另一个字段,代码就会调用,然后调用DidBegin。我该怎么办?我希望这些动画只在我开始编辑文本字段并且没有编辑文本字段时发生,而当我需要隐藏键盘时(当用户在iPad上按下DownKey时)DidEnd

任何想法??

我试过了:

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

...它有效,但是当我按下重新运行键时,没有动画发生。

1 个答案:

答案 0 :(得分:1)

有问题的textField将自己与textFieldDidBeginEditing的参数一起发送。

添加if()块以测试textField1和textField2 ie:

-(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;
}