我有一个 View (LSTabBarView),它支持使用以下方法插入/删除新元素:
- (void)insertItem:(LSTabItem *)item atIndex:(NSUInteger)index animated:(BOOL)animated;
- (void)removeItemAtIndex:(NSUInteger)index animated:(BOOL)animated;
LSTabItem 是代表我的模型类的对象。每次使用 insertItem:atIndex:animated 添加新元素时:使用此模型对象内部的信息创建一个新的 subview (LSTabControl),然后添加到主视图,最后调用 layoutSubviews 重新计算每个框架 subview 。
我刚刚实现了这些操作,但是“animated”参数暂时没有使用,我想知道当 animated 为 YES 时我应该把代码放在哪里来处理动画。
这些是相关的方法:
- (void)insertItem:(LSTabItem *)item
atIndex:(NSUInteger)index
animated:(BOOL)animated
{
...
LSTabControl *tab = [self _configureNewTabControlForItem:item atIndex:index];
[tabItems insertObject:item atIndex:index];
[tabViews insertObject:tab atIndex:index]; // array which contains all the subviews
[self setNeedsLayout];
}
- (void)removeItemAtIndex:(NSUInteger)index
animated:(BOOL)animated
{
...
LSTabControl *tab = [tabViews objectAtIndex:index];
...
[tab removeFromSuperview];
[tabViews removeObjectAtIndex:index];
[tabItems removeObjectAtIndex:index];
[self setNeedsLayout];
}
layoutSubviews 所做的基本上是将每个 subview 一个接一个地放置在线性水平序列中,并在每个 subview 之间应用填充。每个 subview 可以有不同的宽度或高度(它们不是固定大小)。 当添加动画设置为 YES 的新元素时(假设在 index=2 处),我希望从 index+1 到 numberOfElements 的每个 subview 都向右移动,然后新的 subview 出现在可用的孔中。
我显然希望 LSTabBarView 是可扩展的,所以在我对这些方法的基本实现中,我不能假设 subview 将如何按子类排列(例如,子类可能会覆盖 layoutSubviews 并决定以垂直布局布置元素而不是水平的,因此决定在添加新元素时将每个 subview 向下移动而不是向右移动...)
我不想知道如何执行动画(我已经知道如何执行)但考虑到我的上述要求,如何为新 subview 的外观设置动画的正确方法是什么。
我应该考虑在 layoutSubviews 中执行所有动画,还是将动画代码放在 insertItem/removeItem 中,让 layoutSubviews 只计算 subview 布局?
谢谢
最佳答案
layoutSubviews
意味着一次性快速方法。最好将 insertItem:
用于动画。
关于ios - layoutSubviews 中的动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8970912/