簡體   English   中英

隱藏另一個視圖的鍵盤的文本字段動畫

[英]Keyboard hiding textfield animation for another View

當我按下文本框時,鍵盤會將其隱藏。 因此,我實現了以下代碼以“向上滾動”視圖:

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [self animateTextField: textField up: YES];
}


- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [self animateTextField: textField up: NO];
}

- (void) animateTextField: (UITextField*) textField up: (BOOL) up
{
    const int movementDistance = 80; // tweak as needed
    const float movementDuration = 0.3f; // tweak as needed

    int movement = (up ? -movementDistance : movementDistance);

    [UIView beginAnimations: @"anim" context: nil];
    [UIView setAnimationBeginsFromCurrentState: YES];
    [UIView setAnimationDuration: movementDuration];
    self.view.frame = CGRectOffset(self.view.frame, 0, movement);
    [UIView commitAnimations];
}

我正在嘗試執行的UIView在另一個筆尖中。 我想因為我有兩個UIView,所以程序變得很困惑,不知道將動畫設置為哪個。

我沒有任何錯誤,但是動畫沒有發生。

我已聲明要為動畫設置的視圖的UIView“ surveyPage”。 上面的代碼中是否有地方我必須指定要設置動畫的UIView?

更新:

下面的建議對我不起作用。 我嘗試將self.view更改為SurveyPage。

在上面發布的代碼上方,我的程序中包含以下代碼:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

上面的代碼可以正常工作,但是動畫卻不能。

您在此處指定UIView:

self.view.frame = CGRectOffset(self.view.frame, 0, movement);

這是self.view

嘗試:

[UIView animateWithDuration:movementDuration animations:^{
    self.view.frame = CGRectOffset(self.view.frame, 0, movement);
}];

代替

[UIView beginAnimations: @"anim" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
self.view.frame = CGRectOffset(self.view.frame, 0, movement);
[UIView commitAnimations];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM