I have a view (LSTabBarView) which supports insertion/deletion of new elements with the following methods:

- (void)insertItem:(LSTabItem *)item atIndex:(NSUInteger)index animated:(BOOL)animated; 
- (void)removeItemAtIndex:(NSUInteger)index animated:(BOOL)animated;

LSTabItem is the object that represents my model class. Every time that a new element is added with insertItem:atIndex:animated: a new subview is created (LSTabControl) using the informations inside of this model object, then added to the main view, and finally layoutSubviews is called to recalculate the frame of each subviews.

I have just implemented these operations but the "animated" parameter is not used for now and I was wondering where should I put the code to handle the animation when animated is YES.

These are the relevant methods:

- (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];
}

What layoutSubviews do is basically to put every subviews in a linear horizontal sequence one after another applying a padding between each subview. Each subview could have a different width or height (they are not fixed size). When a new element is added with animation set to YES (lets say at index=2) I want that each subviews from index+1 to numberOfElements are shifted to right and then the new subview appear in the available hole.

I want obviously LSTabBarView to be extensible, so in my base implementation of these methods I cannot make assumptions about how the subviews will be arranged by subclasses (for example a subclass might override layoutSubviews and decide to layout the elements in a vertical layout instead of horizontal and as consequence decides to shift each subviews down instead of to right when adding new elements...)

I don't want to know how to perform the animation (I already know how to do it) but what is the correct approach how to animate the appearance of a new subview, considering my requirements described above.

Should I consider to perform all the animations inside of layoutSubviews or puts the animation code inside of insertItem/removeItem letting layoutSubviews just to calculate the subviews layout?

Thanks

有帮助吗?

解决方案

layoutSubviews meant to be one-shot quick method. Better use your insertItem: for animation.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top