簡體   English   中英

iOS動畫主UIViewController的視圖高度

[英]iOS Animating main UIViewController's View height

我有一個文本字段固定在主UIViewController視圖的底部(self.view),當用戶單擊它時,將調用此函數(通過UIKeyboardWillShowNotification),該函數將更改self.view.frame的高度:

-(void)keyboardWillShow: (NSNotification*) notification {
    NSDictionary* info = [notification userInfo];
    CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    CGFloat textFieldHeight = activeField.frame.size.height;
    CGPoint textFieldOrigin = activeField.frame.origin;
    textFieldOrigin.y += textFieldHeight;

    CGRect visibleRect = self.view.frame;
    visibleRect.size.height -= keyboardSize.height;

    if (!CGRectContainsPoint(visibleRect, textFieldOrigin)) {
        CGRect r = self.view.frame;
        r.size.height -= keyboardSize.height;

        [UIView animateWithDuration:1.0
                     animations:^{
                         self.view.frame = r;
                     }];
    }
}

它可以很好地調整self.view.frame的大小,因此都可以調用它並運行,但是由於某種原因拒絕對其進行動畫處理-它只是立即就位。

我需要做些什么才能使這種高度變化動起來?

嘗試稍微延遲一下,例如:

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
  [UIView animateWithDuration:1.0
                     animations:^{
                         self.view.frame = r;
                     }];
}

通常可以達到目的。

如果啟用了自動布局,則不應直接更改框架,而應更改約束條件。 將IBOutlet添加到約束(底部約束),並按如下所示更改常量:

myTextFieldConstrain.constant -= YOUR_VALUE

另外,如果要對其進行動畫處理,請調用[YOURSUPERVIEW layoutIfNeeded]; 更改常數后。

例:

[UIView animateWithDuration:1.0
                     animations:^{
                         myTextFieldConstrain.constant -= YOUR_VALUE;
                         [YOURSUPERVIEW layoutIfNeeded];
                     }];

暫無
暫無

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

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