![](/img/trans.png)
[英]Swift -- UITextField is not showing keyboard and I cannot edit it
[英]Showing and hiding keyboard on second UITextField edit
我有很多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。 我該如何阻止呢? 我希望這些動畫僅在我開始編輯文本字段且未編輯任何文本字段時發生,而當我需要隱藏鍵盤時(用戶在iPad上按下DownKey時)則出現DidEnd
有任何想法嗎??
我嘗試了這個:
if (scroll2.frame.origin.y != 414) {
...有效,但是當我按重新運行鍵時,沒有動畫發生。
有問題的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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.