本文介绍了您如何为tableView的大小设置动画,而不会削减其行/子视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:我最初是在调整一般视图大小时问这个问题的,但是我现在意识到它是针对tableViews的。

I originally asked this question in regards to general view resizing, but I realize now that it's something much more specific to tableViews.

说tableView最初是具有(0 0,320 460)的帧,即,它填充了整个iPhone屏幕。为了调整tableView的高度并设置动画大小,我将以下代码映射到按钮:

Say that a tableView originally has a frame of (0 0, 320 460), i.e., it fills an entire iPhone screen. In order to resize the tableView's height and animate the resize, I map the following code to a button:

[UIView beginAnimations:@"Animation" context:nil];
[UIView setAnimationDuration:3];

CGRect rect = self.table.bounds;
rect.size = CGSizeMake(320, 200);
self.table.bounds = rect;
self.table.center = CGPointMake(160, 100);

[UIView commitAnimations]; 

此代码将动画化桌子的高度,但是...在发生任何动画之前,桌子将剪掉所有不适合新边界的表格单元。因此,给定一个原来适合10行的tableView,上面的动画代码将导致以下事件序列:

This code will animate the table's height down, BUT...before any animation happens, the table will clip off all of the table cells that won't fit in the new bounds. So, given a tableView that originally fits 10 rows, the animation code above will cause this sequence of events:


  1. tableView删除了底部的5立即行。请注意,它仅会删除行-tableView的背景仍会延伸到底部。

  2. tableView会为表格其余部分设置高度变化的动画。

这不是我想要的行为。而不是立即剪切底部的5行,tableView应该在表的底部边缘向上动画时保持其行在适当位置。

This is not the behavior I am looking for. Instead of clipping the bottom 5 rows immediately, the tableView should keep its rows in place while the bottom edge of the table just animates upwards.

基于我的行为看到tableView发现边界即将改变后,似乎正在获取某种消息以立即刷新其行。有人可以阐明这种行为以及如何避免这种行为吗?

Based on the behavior I'm seeing, it seems that the tableView is getting some sort of message to refresh its rows immediately after it finds out that its bounds is going to change. Can someone shed some light on this behavior and how I could avoid it?

推荐答案

视图的bounds属性定义了内部坐标视图的空间。如果要更改视图的大小和/或位置,则应仅更改视图的框架。从理论上讲,bounds属性可以定义一个与视图的封闭框架断开连接的完全任意的坐标空间,并且UIKit似乎旨在支持这种区别。实际上,我认为我从未在UIKit中看到过这种方式(bounds.size始终与frame.size相同,bounds.origin始终为{0,0}),但这是区别在那里,记住这一点很重要-特别是如果这种行为将来会改变。

The bounds property of a view defines the internal coordinate space of the view. If you are changing the size and/or position of the view, you should only change the view's frame. Theoretically, the bounds property could define a totally arbitrary coordinate space disconnected from the view's enclosing frame and UIKit is seemingly designed to support that distinction. In practice I don't think I've ever seen it work that way in UIKit (the bounds.size is always the same as frame.size and bounds.origin is always {0,0}), but that's the distinction being made there and it's important to remember - especially if this behavior ever changes in the future.

我不确定只是更改代码来修改框架而不是限制将解决问题,但我建议从那里开始。如果那不能解决问题,那么问题可能出在tableview中,将立即删除在新框架中不可见的行,因此,唯一的解决方案可能就是自己制作动画设置一个简单的NSTimer驱动的例程,该例程会随着时间的流逝缓慢地更改帧的大小/位置,从而创建您想要的幻觉,尽管tableview进行了优化或与Core Animation交互。

I'm not certain that simply changing the code to modify the frame instead of the bounds will solve the issue, but I recommend starting there. If that doesn't solve it, then the problem is probably that the tableview is immediately removing the rows which will become invisible with the new frame, so if that's the case, it's possible that the only solution would be to do the animation yourself by setting up a simple NSTimer-driven routine that slowly changes the frames size/position over time thus creating the illusion you want in spite of the tableview's optimizations or interactions with Core Animation.

这篇关于您如何为tableView的大小设置动画,而不会削减其行/子视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 13:40