6

我试图用动画调整tableview的高度,它可以通过动画tableview的frame.size.height来正常工作。

问题是,我有一个高度为 200 像素并滚动到底部的表格视图,我想将其设置为 100 像素,我运行一个简单的

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

这工作正常,但在我调整它的大小后,它不再滚动到表格视图的底部。我希望表格视图在动画时始终在底部滚动。我尝试了很多不同的事情,比如打电话

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

就在我开始调整大小动画之前/之后,但我还没有设法让它们 100% 同步。有没有办法在表格视图的底部显示滚动视图的最后一个元素时调整表格视图的大小。

4

4 回答 4

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中,然后我将tableview包装在一个“父”uiview中,剪辑子视图=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);
}
于 2012-04-27T07:26:18.037 回答
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];
 } ];
于 2012-04-25T20:17:13.443 回答
0

尝试在为框架设置动画时更新 table view 的 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);

}];
于 2012-04-25T19:58:58.403 回答
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)
    }
}
于 2017-11-06T09:48:59.087 回答