簡體   English   中英

動畫約束正在打破其他約束

[英]Animating constraints is breaking other constraints

我的視圖底部有以下表格視圖

在此處輸入圖片說明

它具有高度限制(優先級250)和視圖底部的限制(優先級1000)。 高度約束指向我的視圖控制器中的`IBOutlet。

我想將表格視圖的高度從44.0f更改為7 * 44.0f,所以我正在做的是這樣。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.categoriesShown) {
        [self hideCategories];
    } else {
        [self showCategories];
    }

    self.categoriesShown = !self.categoriesShown;
}

- (void)showCategories
{
    self.categoriesHeightConstraint.constant = self.categories.count * 44.0f;
}

- (void)hideCategories
{
    self.categoriesHeightConstraint.constant = 44.0f;
}

工作正常。 但是,當我嘗試使用以下代碼制作所有動畫時:

- (void)showCategories
{
    [self.categoryTableView layoutIfNeeded];

    [UIView transitionWithView:self.categoryTableView duration:0.3f options:UIViewAnimationOptionCurveEaseOut animations:^{

        self.categoriesHeightConstraint.constant = self.categories.count * 44.0f;
        [self.categoryTableView layoutIfNeeded];

    } completion:nil];
}

- (void)hideCategories
{
    [self.categoryTableView layoutIfNeeded];

    [UIView transitionWithView:self.categoryTableView duration:0.3f options:UIViewAnimationOptionCurveEaseOut animations:^{

        self.categoriesHeightConstraint.constant = 44.0f;
        [self.categoryTableView layoutIfNeeded];

    } completion:nil];
}

然后以某種方式打破了tableview和視圖底部之間的約束,這就是我在顯示然后隱藏tableview時得到的結果

在此處輸入圖片說明

有誰知道為什么,約束被打破了,但是只有當我嘗試為更改設置動畫時才被打破?

更新:UIButton約束

這兩個按鈕都具有寬度和高度約束以及對視圖底部的約束。 左側的按鈕對該視圖具有前導約束。 權利之一對視圖具有尾隨約束。 如上所述,兩者都對表格視圖具有水平間距約束。

在此處輸入圖片說明

我認為您的問題出在此方法調用中:

[UIView transitionWithView:self.categoryTableView duration:0.3f options:UIViewAnimationOptionCurveEaseOut animations:^{

        self.categoriesHeightConstraint.constant = 44.0f;
        [self.categoryTableView layoutIfNeeded];

    } completion:nil];

這應該可以滿足您的需求:

self.categoriesHeightConstraint.constant = 44.0f;
[UIView animateWithDuration:0.3f animations:^{
    [self.view layoutIfNeeded];
    //This is assuming the method is called from a view controller.
    //You need to call layoutIfNeeded on the superview of what you're animating
}];

另外,Apple文檔說,這是在更改約束之前進行一次額外的[self.view layoutIfNeeded]調用的好方法,以便所有不完整的約束都可以更改和更新。 不過,我將由您決定。

所以我想我已經找到了您想要的功能:

我給了表格視圖一個高度限制,使其與按鈕的頂部匹配,但是可以是任何東西。

然后我得到桌子應該算出的高度並進行設置:

tableArray = [[NSMutableArray alloc]init];
[tableArray addObject:@"Row 1"];
[tableArray addObject:@"Row 2"];
etc.......


double newHeight = ([tableArray count] * 44);
CGRect newFrame = CGRectMake(basicTable.frame.origin.x, basicTable.frame.origin.y, basicTable.frame.size.width, newHeight);
[basicTable setFrame:newFrame];

然后為您的動畫使用[UIView animateWithDuration],如下所示:

[UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{

    tableHgtConst.constant = newHeight;
    [self.view layoutIfNeeded];

} completion:^(BOOL finished) {


}];

我只留下底部約束,然后更改高度約束以匹配高度,迫使桌子向上而不是向下。

屏幕:模擬器: http : //i.stack.imgur.com/T7MBr.png

代碼文件: http//i.stack.imgur.com/GGOkq.png

暫無
暫無

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

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