我有一个问题,其中键盘解雇,我认为干扰了我的动画代码。我想要发生的情况是,如果用户在键盘启动时单击搜索按钮,则键盘会掉落,文本字段会在屏幕顶部生成动画。 喜欢在这个图像。如何停止iOS键盘干扰UITextField位置
但是,如果按下搜索按钮实际发生的事情,而键盘了,是这样的。
下面是搜索按钮的代码:
- (IBAction)searchButton:(id)sender {
[self.view endEditing:YES];
[UIView transitionWithView:_enterRoomLabel
duration:0.4
options:UIViewAnimationOptionTransitionCrossDissolve
animations:NULL
completion:NULL];
_enterRoomLabel.hidden = YES;
[_srcButton setHidden:YES];
[_srcAgainButton setHidden:NO];
[UITextField beginAnimations:nil context:NULL];
[UITextField setAnimationBeginsFromCurrentState:YES];
[UITextField setAnimationDelegate:self];
[UITextField setAnimationDuration:0.3];
_roomCodeInput.frame = CGRectMake(_roomCodeInput.frame.origin.x - 0.0, (_roomCodeInput.frame.origin.y - 105.0), _roomCodeInput.frame.size.width + 0.0, _roomCodeInput.frame.size.height);
[UITextField commitAnimations];
}
再次搜索按钮
- (IBAction)searchAgainButton:(id)sender {
[self.view endEditing:YES];
[UIView transitionWithView:_enterRoomLabel
duration:0.6
options:UIViewAnimationOptionTransitionCrossDissolve
animations:NULL
completion:NULL];
_enterRoomLabel.hidden = NO;
[_srcButton setHidden:NO];
[_srcAgainButton setHidden:YES];
[UITextField beginAnimations:nil context:NULL];
[UITextField setAnimationBeginsFromCurrentState:YES];
[UITextField setAnimationDelegate:self];
[UITextField setAnimationDuration:0.3];
_roomCodeInput.frame = CGRectMake(_roomCodeInput.frame.origin.x + 0.0, (_roomCodeInput.frame.origin.y + 105.0), _roomCodeInput.frame.size.width - 0.0, _roomCodeInput.frame.size.height + 0.0);
[UITextField commitAnimations];
}
这里是我使用的方法解雇键盘
UITapGestureRecognizer* tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismissKeyboard)];
tapGesture.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:tapGesture];
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[_roomCodeInput resignFirstResponder];
return NO;
}
-(void)dismissKeyboard{
[_roomCodeInput resignFirstResponder];
}
我只是想找到最好的解决方案来解决这个问题,或者可能重新设计类似的东西。 任何帮助将不胜感激! :)
^h你试图用自动布局约束来做到这一点?您可以在键盘框发生变化时得到通知,以便根据需要调整约束来移动元素。 – myte
@myte我认为我最近看到了,我想过但是我不太确定如何实际执行,不得不多看看这个主题 – Dowling1dow