只是作爲一個背景,這個問題是與此相關的一個我剛纔發佈:Trying to expand/collapse UITableViewCell from a UIButton on the custom cellUITableViewCell的展開/摺疊動畫(工程拓展的時候,卻沒有崩潰)
總之,我有幾個自定義表格單元格的UITableView 。每個自定義UITableViewCell都有一個文本區域,後跟一個「查看更多」按鈕。實際上,每個單元格最初將顯示3行文本,但當用戶點擊「查看更多」按鈕時,單元格應該展開爲整個文本區域的高度。再次點擊按鈕將摺疊單元格。
我覺得這是很難想象的,所以我在這裏拍的短片:http://dl.dropbox.com/u/762437/cell-animation.mov
(這是關於一個3.5MB的文件,所以不應該花這麼長時間來加載)。在視頻中,我展示了一個細胞在做展開/摺疊動畫。進行展開時,「查看更多」按鈕向下動畫。當再次點擊它時,它會跳回原來的位置而不會動畫。 編輯:對不起,我應該更清楚。 UITableView是正確的動畫單元格,我問的是如何使「查看更多」按鈕動畫正確。
我已經得到了擴展/崩潰的工作(不管我做對了還是錯是另一回事!),但動畫並不像我期望的那樣工作。擴大的動畫有效,但不是崩潰。
在每個自定義UITableViewCell類中,我有一個IBAction,當用戶點擊「查看更多」按鈕時被調用。我還跟蹤當前在NSMutableArray中展開的單元格。當用戶點擊「關閉」按鈕時,該單元將從陣列中移除。
在我的tableView的cellForRowAtIndexPath中,我檢查數組以查看哪些單元格應該展開,哪些應以默認大小顯示。
我用這個代碼這樣做:
// Check if the array contains the particular cell, if so, then it needs to be expanded
if([expandedRows containsObject:indexPath])
{
// Expand the cell's text area to fit the entire contents
[cell.textLabel sizeToFitFixedWidth:cell.textLabel.frame.size.width];
// Find the new Y-position of where the "Close" button.
// The new Y position should be at the bottom of the newly expanded text label
CGFloat bottomYPos = cell.textLabel.frame.origin.y + cell.textLabel.frame.size.height;
// Get a rect with the "Close" button's frame and new Y position
CGRect buttonRect = cell.showAllButton.frame;
buttonRect.origin.y = bottomYPos;
// Animation block to shift the "Close" button down to its new rect
[UIView animateWithDuration:0.3
delay:0.0
options: UIViewAnimationCurveLinear
animations:^{
cell.showAllButton.frame = buttonRect;
[cell.showAllButton setTitle:@"Close" forState:UIControlStateNormal];
}
completion:^(BOOL finished){
NSLog(@"Done 1!");
}];
}
else
{
// This cell is currently collapsed
// Get the button rect and subtract the height delta to put it back
// OriginalCellSizes is where I keep track of the original Y positions
CGRect buttonRect = cell.showAllButton.frame;
buttonRect.origin.y -= cell.frame.size.height - [[originalCellSizes objectAtIndex:indexPath.row] floatValue];
// Animation block for the Button
[UIView animateWithDuration:0.3
delay:0.0
options: UIViewAnimationCurveEaseOut
animations:^{
cell.showAllButton.frame = buttonRect;
[cell.showAllButton setTitle:@"View More" forState:UIControlStateNormal];
}
completion:^(BOOL finished){
NSLog(@"Done! 2");
}];
}
經調查,我發現的「其他」分支的if-else,該buttonRect已經在相同的Y位置原來的位置。我懷疑這就是爲什麼沒有動畫發生,我不知道。
任何幫助都會非常棒!
這個答案沒有任何與被問的問題。 – Dalmazio 2014-07-25 02:37:27
你能看到答案被接受嗎? – 2014-07-25 06:14:08
是的,那更令人困惑。 – Dalmazio 2014-07-25 21:36:46