diff --git a/CSLinearLayoutView/CSLinearLayoutView.h b/CSLinearLayoutView/CSLinearLayoutView.h index b0e160b..45a0d32 100644 --- a/CSLinearLayoutView/CSLinearLayoutView.h +++ b/CSLinearLayoutView/CSLinearLayoutView.h @@ -58,6 +58,7 @@ typedef enum { - (void)swapItem:(CSLinearLayoutItem *)firstItem withItem:(CSLinearLayoutItem *)secondItem; +- (CSLinearLayoutItem*)findItemByTag:(NSInteger)tag; @end @@ -85,14 +86,20 @@ typedef struct { CGFloat right; } CSLinearLayoutItemPadding; +NS_ENUM(NSInteger, CSLinearLayoutItemVisibility) { + CSLinearLayoutItemHidden, // Occupy the frame + CSLinearLayoutItemGone // Missing in the layout +}; + @interface CSLinearLayoutItem : NSObject -@property (nonatomic, retain) UIView *view; +@property (nonatomic, strong) UIView *view; @property (nonatomic, assign) CSLinearLayoutItemFillMode fillMode; @property (nonatomic, assign) CSLinearLayoutItemHorizontalAlignment horizontalAlignment; // Use horizontalAlignment when the layout view is set to VERTICAL orientation @property (nonatomic, assign) CSLinearLayoutItemVerticalAlignment verticalAlignment; // Use verticalAlignment when the layout view is set to HORIZONTAL orientation +@property (nonatomic, assign) enum CSLinearLayoutItemVisibility hiddenType; // Apply when the layout view is set YES hidden @property (nonatomic, assign) CSLinearLayoutItemPadding padding; -@property (nonatomic, assign) NSDictionary *userInfo; +@property (nonatomic, weak) NSDictionary *userInfo; @property (nonatomic, assign) NSInteger tag; - (id)initWithView:(UIView *)aView; diff --git a/CSLinearLayoutView/CSLinearLayoutView.m b/CSLinearLayoutView/CSLinearLayoutView.m index 94dcef1..06deea0 100644 --- a/CSLinearLayoutView/CSLinearLayoutView.m +++ b/CSLinearLayoutView/CSLinearLayoutView.m @@ -10,6 +10,8 @@ @interface CSLinearLayoutView() +@property (nonatomic, readwrite) NSMutableArray *items; + - (void)setup; - (void)adjustFrameSize; - (void)adjustContentSize; @@ -22,7 +24,6 @@ @implementation CSLinearLayoutView @synthesize orientation = _orientation; @synthesize autoAdjustFrameSize = _autoAdjustFrameSize; @synthesize autoAdjustContentSize = _autoAdjustContentSize; - #pragma mark - Factories - (id)init { @@ -59,10 +60,8 @@ - (void)setup { #pragma mark - Lifecycle - - (void)dealloc { - [_items release], _items = nil; - [super dealloc]; + self.items = nil; } @@ -75,6 +74,10 @@ - (void)layoutSubviews { for (CSLinearLayoutItem *item in _items) { + if (item.view.hidden && item.hiddenType == CSLinearLayoutItemGone) { + continue; + } + CGFloat startPadding = 0.0; CGFloat endPadding = 0.0; @@ -166,6 +169,10 @@ - (CGFloat)layoutOffset { CGFloat currentOffset = 0.0; for (CSLinearLayoutItem *item in _items) { + if (item.view.hidden && item.hiddenType == CSLinearLayoutItemGone) { + continue; + } + if (_orientation == CSLinearLayoutViewOrientationHorizontal) { currentOffset += item.padding.left + item.view.frame.size.width + item.padding.right; } else { @@ -210,12 +217,10 @@ - (void)removeItem:(CSLinearLayoutItem *)linearLayoutItem { return; } - [linearLayoutItem retain]; [_items removeObject:linearLayoutItem]; [linearLayoutItem.view removeFromSuperview]; - [linearLayoutItem release]; } - (void)removeAllItems { @@ -264,12 +269,10 @@ - (void)moveItem:(CSLinearLayoutItem *)movingItem beforeItem:(CSLinearLayoutItem return; } - [movingItem retain]; [_items removeObject:movingItem]; NSUInteger existingItemIndex = [_items indexOfObject:existingItem]; [_items insertObject:movingItem atIndex:existingItemIndex]; - [movingItem release]; [self setNeedsLayout]; } @@ -279,7 +282,6 @@ - (void)moveItem:(CSLinearLayoutItem *)movingItem afterItem:(CSLinearLayoutItem return; } - [movingItem retain]; [_items removeObject:movingItem]; if (existingItem == [_items lastObject]) { @@ -288,7 +290,6 @@ - (void)moveItem:(CSLinearLayoutItem *)movingItem afterItem:(CSLinearLayoutItem NSUInteger existingItemIndex = [_items indexOfObject:existingItem]; [_items insertObject:movingItem atIndex:++existingItemIndex]; } - [movingItem release]; [self setNeedsLayout]; } @@ -298,7 +299,6 @@ - (void)moveItem:(CSLinearLayoutItem *)movingItem toIndex:(NSUInteger)index { return; } - [movingItem retain]; [_items removeObject:movingItem]; if (index == ([_items count] - 1)) { @@ -306,7 +306,6 @@ - (void)moveItem:(CSLinearLayoutItem *)movingItem toIndex:(NSUInteger)index { } else { [_items insertObject:movingItem atIndex:index]; } - [movingItem release]; [self setNeedsLayout]; } @@ -323,6 +322,16 @@ - (void)swapItem:(CSLinearLayoutItem *)firstItem withItem:(CSLinearLayoutItem *) [self setNeedsLayout]; } +- (CSLinearLayoutItem*)findItemByTag:(NSInteger)tag +{ + for (CSLinearLayoutItem *item in _items) { + if (item.tag == tag) { + return item; + } + } + return nil; +} + @end #pragma mark - @@ -353,6 +362,7 @@ - (id)initWithView:(UIView *)aView { self = [super init]; if (self) { self.view = aView; + self.tag = aView.tag; self.horizontalAlignment = CSLinearLayoutItemHorizontalAlignmentLeft; self.verticalAlignment = CSLinearLayoutItemVerticalAlignmentTop; self.fillMode = CSLinearLayoutItemFillModeNormal; @@ -361,17 +371,15 @@ - (id)initWithView:(UIView *)aView { } + (CSLinearLayoutItem *)layoutItemForView:(UIView *)aView { - CSLinearLayoutItem *item = [[[CSLinearLayoutItem alloc] initWithView:aView] autorelease]; + CSLinearLayoutItem *item = [[CSLinearLayoutItem alloc] initWithView:aView]; return item; } #pragma mark - Memory Management - (void)dealloc { - self.view = nil; self.userInfo = nil; - [super dealloc]; }