2012-04-25 158 views
6

我試圖通過動畫調整tableview的高度,它通過動畫tableview的frame.size.height工作正常。調整UITableView的高度,並保持滾動底部

的問題是,我有一個tableview中是200像素高度,滾動至底部,我想動畫此爲100px,我運行一個簡單的

[UIView animateWithDuration:0.245f animations:^{ 
CGRect frame = tableview.frame; 
frame.size.height = 100.f; 
tableview.frame = frame; 
}]; 

這工作得很好,但之後,我調整它它不再滾動到tableview的底部。我希望桌面視圖始終在動畫的底部滾動。我嘗試了目前存在的很多事情就像調用

[tablview scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES]; 

右側前/後,我開始調整大小的動畫,但我沒有設法讓他們同步的100%。有沒有辦法調整tableview的大小,而tableview的底部顯示scrollview的最後一個元素。

+0

使用[UIView的animateWithDuration:動畫:完成:]呢? – onnoweb 2012-04-25 20:00:08

+1

我想滾動發生與調整大小動畫沒有後,但謝謝:) – 2012-04-27 07:16:34

回答

2

我發現我的問題的解決方案,也很可能是一個更好的解決辦法,但其實這工作得很好:)

開始之前,我的動畫我看到,如果contentSize.height是比目標高度大,如果所以我以下:

if (mMessages.contentSize.height > 150.f) 
{ 
    CGFloat expandedOffsetY = 52.f; 
    CGFloat collapsedBottomOffset = (150.f + 18.f); 
    CGFloat expandedBottomOffset = (MIN(320.f, mMessages.contentSize.height) + expandedOffsetY); 
    tableFrame.origin.y = (collapsedBottomOffset - expandedBottomOffset) + expandedOffsetY; 
} 
else 
{ 
    tableFrame.origin.y = 18.f; 
    tableFrame.size.height = 150.f; 
} 

這將使在的tableview負origin.y,然後我已包裹着夾子視圖的「父」的UIView內的tableview = YES。

然後我有一個「完成」動畫塊,「復位」到目標值。

CGRect tableFrame = mMessages.frame; 
tableFrame.origin.y = 18.f; 
tableFrame.size.height = 150.f; 
mMessages.frame = tableFrame; 

if (mMessages.contentSize.height > tableFrame.size.height) 
{ 
    float contentOffsetY = mMessages.contentSize.height - tableFrame.size.height; 
    mMessages.contentOffset = CGPointMake(0.0f, contentOffsetY); 
} 
0

嘗試在爲框架設置動畫時更新表視圖的contentOffset。

[UIView animateWithDuration:0.245f animations:^{ 
CGRect frame = tableview.frame; 
frame.size.height = 100.f; 
tableview.frame = frame; 

float contentOffsetY = tableview.contentSize - tableview.frame.size.height; 
tableview.contentOffset = CGPointMake(0, contentOffsetY); 

}]; 
+0

感謝您的建議,我害怕它看起來不像我想要它,做了一點挖掘後,它看起來像設置contentOffsetY而動畫高度給出了一些奇怪的行爲(抱歉,但很難解釋)。但結果並不是在調整大小時,tableview的底部會向上移動。 – 2012-04-27 07:18:45

0
[UIView animateWithDuration:0.245f animations:^{ 
     CGRect frame = tableview.frame; 
     frame.size.height = 100.f; 
     tableview.frame = frame; 
} completion:^(BOOL finished) { 
     // Find your last indexpath 
     [tablview scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES]; 
} ]; 
+0

我希望Scrollanimation與調整大小動畫不發生後,但謝謝:) – 2012-04-27 07:19:00

0

滾動的tableview在動畫塊底部是爲我工作,有一個小轉折:當我降低了的tableView我叫scrollToIndexPath沒有動畫的身高,當我展開的tableView我叫帶動畫的scrollToIndexPath。

UIView.animate(withDuration: 0.25) { [weak self] in 
    guard let strongSelf = self 
     else { return } 
    // ... 
    // updateConstraints 
    // ... 
    strongSelf.view.layoutIfNeeded() 

    if reduceSize { 
     tableView.scrollToLastRow(animated: false) 
    } else { 
     tableView.scrollToLastRow(animated: true) 
    } 
} 

擴展的UITableView

extension UITableView { 

    func scrollToLastRow(animated: Bool) { 
     let lastSection = numberOfSections-1 
     let lastRow = numberOfRows(inSection: lastSection)-1 
     let lastIndexPath = IndexPath(row: lastRow, section: lastSection) 
     scrollToRow(at: lastIndexPath, at: .bottom, animated: animated) 
    } 
}