2014-10-29 154 views
1

在我的應用程序中,我使用的是UIView,其中包含UITableView,ButtonsLabels。它使用Storyboard創建。當用戶單擊一個導航欄按鈕UIView將出現從頂部到一定高度的動畫,如果他們再次點擊它,它會用動畫隱藏UIView(從該高度到頂部)。 和UIActionView相同。動畫UIView無法正常工作

如果UITableView中沒有記錄,它可以正常工作。但如果它有任何記錄,在調用[self hideBasket]時,UIView會從視圖底部出現到頂部(未隱藏)。

//隱藏籃碼

-(void)hideBasket{ 
    /*Finished Hiding the Basket 
    [self.view sendSubviewToBack:_shoppingCartView]; 
    [_shoppingCartView setHidden:YES]; 
    _isShoppingCartSeen = NO;*/ 

    CGRect basketFrame = _shoppingCartView.frame; 
    basketFrame.origin.y = -basketFrame.size.height; 

    [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{ 
     _shoppingCartView.frame = basketFrame; 
    } completion:^(BOOL finished) { 
     // Finished Hiding the Basket 
     //[self.view sendSubviewToBack:_shoppingCartView]; 
     // [_shoppingCartView setHidden:YES]; 
     _isShoppingCartSeen = NO; 
}]; 

//顯示籃碼

-(void)showBasket{ 

    /*[self.view bringSubviewToFront:_shoppingCartView]; 
    [_shoppingCartView setHidden:NO]; 
    _isShoppingCartSeen = YES;*/ 

    CGRect basketFrame = _shoppingCartView.frame; 
    basketFrame.origin.y = 0; 

    [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{ 
     _shoppingCartView.frame = basketFrame; 
    } completion:^(BOOL finished) { 
     // Finished Showing the Basket 
     [self.view bringSubviewToFront:_shoppingCartView]; 
     [_shoppingCartView setHidden:NO]; 
     _isShoppingCartSeen = YES; 
    }]; 
} 

我做錯的是什麼?

+0

是否在故事板上選擇了AutoLayout? – 2014-10-29 15:04:29

+0

@JimTierney是的,它是 – GoCrazy 2014-10-29 15:20:49

回答

4

使用自動佈局,您應該動畫您的約束,而不是更改對象的框架。

我嘲笑起來從哪裏開始使用約束粗糙的例子,這應該解決您的問題

首先,你需要設置

每個對象都必須有你的籃子視圖的限制至少要設置4個約束才能正確設置。

查看下面的屏幕快照,按下我選擇的視圖底部的約束圖標來設置視圖的寬度和高度以及左側距離約束。

然後,您需要將空間設置爲超級視圖頂部,請參閱第二個屏幕截圖。

enter image description here

約束設置到上海華頂

enter image description here

一旦你的約束已經建立您設置CTRL拖動頂部空間,上海華財產像你的頭文件下面的截圖。 (你需要在視圖中設定的限制,以適應你的表客體等也是如此),

enter image description here

現在,這已經成立,請通過以下方式代替你的代碼,它應該工作細

-(void)hideBasket{ 

self.topVerticalSpaceConstraint.constant = -312; 

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

    [self.view layoutIfNeeded]; 

} completion:^(BOOL finished) { 

}]; 

}

- (無效)showBasket {

self.topVerticalSpaceConstraint.constant = 0; 

    [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{ 
     [self.view layoutIfNeeded]; 

    } completion:^(BOOL finished) { 

    }]; 

}

請注意,我只需將一定量的人工這裏的虛擬視圖的大小我做了,但你當然會改變這是您的視圖的大小等

請記住,您的每個視圖/對象應理想地設置其約束,尤其是下拉視圖中的UITableview。在UIView中設置表格的高度,寬度和頂部和左側空間約束就足夠了。

如果你希望你的觀點,從第一負載被隱藏,你的viewDidLoad中設置約束-valueOfHeightOfBasket

我希望這有助於。

+0

+1非常感謝您的時間和精力來複制它。我會嘗試你的建議並會回覆給你。 – GoCrazy 2014-10-29 16:51:01

+0

完美工作順利。謝謝 – GoCrazy 2014-10-30 12:36:22

+0

輝煌,非常感謝。 – 2015-11-02 13:05:31