diff --git a/Example/TabBarPicker/TabBarPickerViewController.m b/Example/TabBarPicker/TabBarPickerViewController.m index 1afc83d..692c592 100644 --- a/Example/TabBarPicker/TabBarPickerViewController.m +++ b/Example/TabBarPicker/TabBarPickerViewController.m @@ -35,33 +35,38 @@ TabBarItem *location = [[TabBarItem alloc] initWithSubItems:@[subItem1,subItem2,subItem3,subItem4,subItem5,subItem6,subItem7]]; [location setItemName:@"Location"]; - [location setImage:[UIImage imageNamed:@"location_off"]]; - [location setSelectedImage:[UIImage imageNamed:@"location_on"]]; + [location setImage:[UIImage imageNamed:@"location"]]; + [location setSelectedImage:[UIImage imageNamed:@"location_selected"]]; + [location setHighlightedImage:[UIImage imageNamed:@"location_highlighted"]]; TabBarItem *calendar = [[TabBarItem alloc] initWithSubItems:@[subItem1,subItem2,subItem3,subItem4,subItem5,subItem6,subItem7]]; [calendar setItemName:@"Calendar"]; - [calendar setImage:[UIImage imageNamed:@"calendar_off"]]; - [calendar setSelectedImage:[UIImage imageNamed:@"calendar_on"]]; + [calendar setImage:[UIImage imageNamed:@"calendar"]]; + [calendar setSelectedImage:[UIImage imageNamed:@"calendar_selected"]]; + [calendar setHighlightedImage:[UIImage imageNamed:@"calendar_highlighted"]]; TabBarItem *type = [[TabBarItem alloc] initWithSubItems:@[subItem1,subItem2,subItem3,subItem4,subItem5,subItem6,subItem7]]; [type setItemName:@"Type"]; - [type setImage:[UIImage imageNamed:@"type_off"]]; - [type setSelectedImage:[UIImage imageNamed:@"type_on"]]; + [type setImage:[UIImage imageNamed:@"type"]]; + [type setSelectedImage:[UIImage imageNamed:@"type_selected"]]; + [type setHighlightedImage:[UIImage imageNamed:@"type_highlighted"]]; TabBarItem *price = [[TabBarItem alloc] initWithSubItems:@[subItem1,subItem2,subItem3,subItem4,subItem5,subItem6,subItem7]]; [price setItemName:@"Price"]; - [price setImage:[UIImage imageNamed:@"price_off"]]; - [price setSelectedImage:[UIImage imageNamed:@"price_on"]]; + [price setImage:[UIImage imageNamed:@"price"]]; + [price setSelectedImage:[UIImage imageNamed:@"price_selected"]]; + [price setHighlightedImage:[UIImage imageNamed:@"price_highlighted"]]; TabBarItem *allergen = [[TabBarItem alloc] initWithSubItems:@[subItem1,subItem2,subItem3,subItem4,subItem5,subItem6,subItem7]]; [allergen setItemName:@"Allergen"]; - [allergen setImage:[UIImage imageNamed:@"allergen_off"]]; - [allergen setSelectedImage:[UIImage imageNamed:@"allergen_on"]]; + [allergen setImage:[UIImage imageNamed:@"allergen"]]; + [allergen setSelectedImage:[UIImage imageNamed:@"allergen_selected"]]; + [allergen setHighlightedImage:[UIImage imageNamed:@"allergen_highlighted"]]; /*TabBarItem *allergen2 = [[TabBarItem alloc] initWithSubItems:@[subItem1,subItem2,subItem3,subItem4,subItem5,subItem6,subItem7]]; [allergen2 setItemName:@"Allergen"]; - [allergen2 setImage:[UIImage imageNamed:@"allergen_off"]]; - [allergen2 setSelectedImage:[UIImage imageNamed:@"allergen_on"]]; + [allergen2 setImage:[UIImage imageNamed:@"allergen"]]; + [allergen2 setSelectedImage:[UIImage imageNamed:@"allergen_selected"]]; */ tabbar = [[TabBarPicker alloc] initWithTabBarItems:@[location,calendar,type,price,allergen] forPosition:TabBarPickerPositionBottom]; [tabbar setItemSpacing:0]; @@ -95,9 +100,7 @@ - (void) viewDidAppear:(BOOL)animated { - dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ - [tabbar show]; - }); + [tabbar selectItem:0]; /*TabBarSubItem *subItem1 = [TabBarSubItem tabBarSubItemWithName:@"Peppe"]; TabBarSubItem *subItem2 = [TabBarSubItem tabBarSubItemWithName:@"Peppe1"]; @@ -109,8 +112,8 @@ TabBarItem *allergen = [[TabBarItem alloc] initWithSubItems:@[subItem1,subItem2,subItem3,subItem4,subItem5,subItem6,subItem7]]; [allergen setItemName:@"Allergen"]; - [allergen setImage:[UIImage imageNamed:@"allergen_off"]]; - [allergen setSelectedImage:[UIImage imageNamed:@"allergen_on"]]; + [allergen setImage:[UIImage imageNamed:@"allergen"]]; + [allergen setSelectedImage:[UIImage imageNamed:@"allergen_selected"]]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [tabbar addItem:allergen]; diff --git a/Pod/Classes/TabBarItem.h b/Pod/Classes/TabBarItem.h index e5062c8..6f44352 100644 --- a/Pod/Classes/TabBarItem.h +++ b/Pod/Classes/TabBarItem.h @@ -30,9 +30,13 @@ */ @property (nonatomic, strong) UIImage *selectedImage; /** - * <#Description#> + * Represents the image visible in picker when item is highlited. */ -@property (nonatomic, strong) UIColor *highlightColor; +@property (nonatomic, strong) UIImage *highlightedImage; +/** + * Represents the color of picker item when is highlited. + */ +@property (nonatomic, strong) UIColor *highlightedColor; /** * Represents the name of the item that can be shown in picker. diff --git a/Pod/Classes/TabBarItem.m b/Pod/Classes/TabBarItem.m index d9ef938..82f8e14 100644 --- a/Pod/Classes/TabBarItem.m +++ b/Pod/Classes/TabBarItem.m @@ -9,6 +9,7 @@ #import "TabBarItem.h" #import #import "NSString+HexColor.h" +#import @interface TabBarItem() @@ -31,7 +32,7 @@ _itemButton = [[UIButton alloc] initForAutoLayout]; [_itemButton addTarget:self action:@selector(itemButtonTapped) forControlEvents:UIControlEventTouchUpInside]; - _highlightColor = [@"ff4e50" colorFromHex]; + [_itemButton setBackgroundColor:[@"ff4e50" colorFromHex] forState:UIControlStateHighlighted]; [self addSubview:_itemButton]; @@ -71,29 +72,40 @@ - (void) setSelectedImage:(UIImage *)selectedImage { [_itemButton setImage:selectedImage forState:UIControlStateSelected]; + [_itemButton setImage:selectedImage forState:UIControlStateSelected|UIControlStateHighlighted]; } -- (void) setHighlightColor:(UIColor *)highlightColor { +- (void) setHighlightedImage:(UIImage *)highlightedImage { + [_itemButton setImage:highlightedImage forState:UIControlStateHighlighted]; + [_itemButton setImage:highlightedImage forState:UIControlStateHighlighted|UIControlStateNormal]; +} + +- (void) setHighlightedColor:(UIColor *)highlightedColor { + + _highlightedColor = highlightedColor; - _highlightColor = highlightColor; } - (void) setHighlighted:(BOOL) highlighted { [_itemButton setHighlighted:highlighted]; if ([_itemButton isHighlighted]) { - [_itemButton setBackgroundColor:_highlightColor]; + + [_itemButton setBackgroundColor:_highlightedColor]; } else { [_itemButton setBackgroundColor:[UIColor clearColor]]; } + NSLog(@"%@",[NSNumber numberWithBool:[_itemButton isSelected]]); + NSLog(@"%@",[NSNumber numberWithBool:[_itemButton isHighlighted]]); + NSLog(@"%@",[NSNumber numberWithBool:[_itemButton isEnabled]]); + NSLog(@"%ld",_itemButton.state); } - (void) itemButtonTapped { - if ([_itemButton isHighlighted]) { - [_itemButton setBackgroundColor:_highlightColor]; + [_itemButton setBackgroundColor:_highlightedColor]; } else { [_itemButton setBackgroundColor:[UIColor clearColor]]; @@ -102,6 +114,13 @@ if (_delegate && [_delegate respondsToSelector:@selector(tabBarItemSelected:)]) { [_delegate tabBarItemSelected:self]; } + NSLog(@"%@",[NSNumber numberWithBool:[_itemButton isSelected]]); + NSLog(@"%@",[NSNumber numberWithBool:[_itemButton isHighlighted]]); + NSLog(@"%@",[NSNumber numberWithBool:[_itemButton isEnabled]]); + NSLog(@"%ld",_itemButton.state); + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ + [_itemButton setHighlighted:YES]; + }); } @end diff --git a/Pod/Classes/TabBarPicker.h b/Pod/Classes/TabBarPicker.h index 3faa1ef..d18589d 100644 --- a/Pod/Classes/TabBarPicker.h +++ b/Pod/Classes/TabBarPicker.h @@ -9,6 +9,7 @@ #import #import "TabbarItem.h" #import "TabBarSubItem.h" +#import "TabBarPickerSubItemsView.h" typedef NS_ENUM(NSInteger, TabBarPickerPosition) { // Informational @@ -91,54 +92,6 @@ typedef NS_ENUM(NSInteger, TabBarPickerPosition) { */ - (instancetype) initWithTabBarItems:(NSArray*) items withTabBarSize:(CGSize) size forPosition:(TabBarPickerPosition) position andNSLayoutRelation:(NSLayoutRelation) relation; -/** - * Init TabBarPicker with items. When is selected an item the picher show down subitems. - * By default when one of more subItems are selected the selected item become selected. - * The picker size is dinamically based the max subitems of items - * - * @param items Represents tab bar items. That array cannot be nil. - * - * @param size Represents the tabbar size - * - * @param position Represents the posizion in the Screen: - * - TabBarPickerPositionLeft : Is positioned in vertical on left of the screen and shows the picker at its right - * - TabBarPickerPositionRight : Is positioned in vertical on right of the screen and shows the picker at its left - * - TabBarPickerPositionBottom : Is positioned in horizontal on bottom of the screen and shows the picker at its top - * - TabBarPickerPositionTop : Is positioned in horizontal on top of the screen and shows the picker at its bottom - * - * @param relation Represents NSLayoutRelation for TabBar layout. - * - * @param subItemsPerRow Represents the number of subItems per single row of subitem picker - * - * @return <#return value description#> - */ -- (instancetype) initWithTabBarItems:(NSArray*) items withTabBarSize:(CGSize) size forPosition:(TabBarPickerPosition) position andNSLayoutRelation:(NSLayoutRelation) relation subItemsPerRow:(NSUInteger) subItemsPerRow; - -/** - * Init TabBarPicker with items. When is selected an item the picher show down subitems. - * By default when one of more subItems are selected the selected item become selected. - * The picker size is dinamically based the max subitems of items - * - * @param items Represents tab bar items. That array cannot be nil. - * - * @param size Represents the tabbar size - * - * @param position Represents the posizion in the Screen: - * - TabBarPickerPositionLeft : Is positioned in vertical on left of the screen and shows the picker at its right - * - TabBarPickerPositionRight : Is positioned in vertical on right of the screen and shows the picker at its left - * - TabBarPickerPositionBottom : Is positioned in horizontal on bottom of the screen and shows the picker at its top - * - TabBarPickerPositionTop : Is positioned in horizontal on top of the screen and shows the picker at its bottom - * - * @param relation Represents NSLayoutRelation for TabBar layout. - * - * @param subItemsPerRow Represents the number of subItems per single row of subitem picker - * - * @param subItemHeight Represents the height of single subItem. - * - * @return <#return value description#> - */ -- (instancetype) initWithTabBarItems:(NSArray*) items withTabBarSize:(CGSize) size forPosition:(TabBarPickerPosition) position andNSLayoutRelation:(NSLayoutRelation) relation subItemsPerRow:(NSUInteger) subItemsPerRow subItemHeight:(CGFloat) subItemHeight; - /** * <#Description#> * @@ -146,6 +99,8 @@ typedef NS_ENUM(NSInteger, TabBarPickerPosition) { */ - (void) addItem:(TabBarItem*) item; +- (void) selectItem:(NSInteger) itemIndex; + - (void) show; - (void) hide; diff --git a/Pod/Classes/TabBarPicker.m b/Pod/Classes/TabBarPicker.m index acbce92..719f73a 100644 --- a/Pod/Classes/TabBarPicker.m +++ b/Pod/Classes/TabBarPicker.m @@ -11,14 +11,11 @@ #import "TabBarPickerSubItemsView.h" #import "MMCPSScrollView.h" #import - -#define DEFAULT_SUB_ITEMS_PER_ROW 2 -#define DEFAULT_SUB_ITEM_HEIGHT 44 +#import "NSString+HexColor.h" @interface TabBarPicker() @property (nonatomic) UIDeviceOrientation orientation; -@property (nonatomic) NSUInteger subItemRows; @property (nonatomic, strong) NSMutableArray *subItemSelectors; @property (nonatomic, strong) NSMutableArray *subItemSelectorsConstraints; @property (nonatomic) BOOL isShow; @@ -35,42 +32,29 @@ - (instancetype) initWithTabBarItems:(NSArray *) items forPosition:(TabBarPickerPosition) position { - return [self initWithTabBarItems:items withTabBarSize:CGSizeZero forPosition:position andNSLayoutRelation:NSLayoutRelationEqual subItemsPerRow:DEFAULT_SUB_ITEMS_PER_ROW subItemHeight:DEFAULT_SUB_ITEM_HEIGHT]; + return [self initWithTabBarItems:items withTabBarSize:CGSizeZero forPosition:position andNSLayoutRelation:NSLayoutRelationEqual]; } - (instancetype) initWithTabBarItems:(NSArray *) items forPosition:(TabBarPickerPosition) position andNSLayoutRelation:(NSLayoutRelation) relation { - return [self initWithTabBarItems:items withTabBarSize:CGSizeZero forPosition:position andNSLayoutRelation:relation subItemsPerRow:DEFAULT_SUB_ITEMS_PER_ROW subItemHeight:DEFAULT_SUB_ITEM_HEIGHT]; + return [self initWithTabBarItems:items withTabBarSize:CGSizeZero forPosition:position andNSLayoutRelation:relation]; } -- (instancetype) initWithTabBarItems:(NSArray *) items withTabBarSize:(CGSize) size forPosition:(TabBarPickerPosition) position andNSLayoutRelation:(NSLayoutRelation) relation { - - return [self initWithTabBarItems:items withTabBarSize:size forPosition:position andNSLayoutRelation:relation subItemsPerRow:DEFAULT_SUB_ITEMS_PER_ROW subItemHeight:DEFAULT_SUB_ITEM_HEIGHT]; -} - -- (instancetype) initWithTabBarItems:(NSArray*) items withTabBarSize:(CGSize) size forPosition:(TabBarPickerPosition) position andNSLayoutRelation:(NSLayoutRelation) relation subItemsPerRow:(NSUInteger) subItemsPerRow { - - return [self initWithTabBarItems:items withTabBarSize:size forPosition:position andNSLayoutRelation:relation subItemsPerRow:DEFAULT_SUB_ITEMS_PER_ROW subItemHeight:DEFAULT_SUB_ITEM_HEIGHT]; -} - - - -- (instancetype) initWithTabBarItems:(NSArray*) items withTabBarSize:(CGSize) size forPosition:(TabBarPickerPosition) position andNSLayoutRelation:(NSLayoutRelation) relation subItemsPerRow:(NSUInteger) subItemsPerRow subItemHeight:(CGFloat) subItemHeight { +- (instancetype) initWithTabBarItems:(NSArray*) items withTabBarSize:(CGSize) size forPosition:(TabBarPickerPosition) position andNSLayoutRelation:(NSLayoutRelation) relation { self = [self initForAutoLayout]; if (self) { - _subItemPerRow = subItemsPerRow; + _itemSpacing = 10; _layoutRelation = relation; - _subItemHeight = subItemHeight; _position = position; _dimWhenShow = YES; _subItemSelectors = [[NSMutableArray alloc] init]; _tabBarItemsConstraints = [[NSMutableArray alloc] init]; _subItemSelectorsConstraints = [[NSMutableArray alloc] init]; - _dimColor = [[UIColor blackColor] colorWithAlphaComponent: 0.5]; + _dimColor = [[@"333333" colorFromHex] colorWithAlphaComponent: 0.5]; [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; @@ -104,7 +88,7 @@ if ([_tabBarItems count] > 0) { int i = 0; for (TabBarItem *item in _tabBarItems) { - TabBarPickerSubItemsView *subItemSelector = [[TabBarPickerSubItemsView alloc] initWithTabBarItem:item andsubItemsPerRow:_subItemPerRow]; + TabBarPickerSubItemsView *subItemSelector = [[TabBarPickerSubItemsView alloc] init]; [subItemSelector setDelegate:self]; switch (i) { case 0: @@ -122,7 +106,9 @@ [subItemSelector setBackgroundColor:[UIColor yellowColor]]; } break; - default: + default:{ + [subItemSelector setBackgroundColor:[UIColor blueColor]]; + } break; } i++; @@ -254,6 +240,14 @@ } } +- (void) selectItem:(NSInteger) itemIndex { + + if ([_tabBarItems count] > itemIndex) { + [(TabBarItem*)[_tabBarItems objectAtIndex:itemIndex] setHighlighted:YES]; + } + [self show]; +} + - (void) show { if (!_isShow) { diff --git a/Pod/Classes/TabBarPickerSubItemsView.h b/Pod/Classes/TabBarPickerSubItemsView.h index 93dad72..c849984 100644 --- a/Pod/Classes/TabBarPickerSubItemsView.h +++ b/Pod/Classes/TabBarPickerSubItemsView.h @@ -11,6 +11,14 @@ @class TabBarPickerSubItemsView; +typedef NS_ENUM(NSInteger, TabBarPickerSubItemsViewType) { + // Informational + TabBarPickerSubItemsViewTypeData, + TabBarPickerSubItemsViewTypeString, + TabBarPickerSubItemsViewTypeButtons, + TabBarPickerSubItemsViewTypeCheckBox +}; + @protocol TabBarPickerSubItemsViewDelegate @required @@ -21,24 +29,10 @@ @interface TabBarPickerSubItemsView : UIView -/** - * <#Description#> - */ -@property (nonatomic, strong) NSMutableArray *subItemsArray; - /** * <#Description#> */ @property (nonatomic, assign) id delegate; -/** - * <#Description#> - * - * @param items <#items description#> - * @param itemsPerRow <#itemsPerRow description#> - * - * @return <#return value description#> - */ -- (instancetype) initWithTabBarItem:(TabBarItem *) item andsubItemsPerRow:(NSUInteger) itemsPerRow; @end diff --git a/Pod/Classes/TabBarPickerSubItemsView.m b/Pod/Classes/TabBarPickerSubItemsView.m index 1e502dd..4cb0ff3 100644 --- a/Pod/Classes/TabBarPickerSubItemsView.m +++ b/Pod/Classes/TabBarPickerSubItemsView.m @@ -9,30 +9,22 @@ #import "TabBarPickerSubItemsView.h" #import -#define DEFAULT_ITEM_HEIGHT 44 - @interface TabBarPickerSubItemsView() -@property (nonatomic) NSUInteger itemsPerRow; -@property (nonatomic) CGFloat itemHeight; -@property (nonatomic) NSUInteger rows; @property (nonatomic, assign) BOOL didSetupConstraints; +@property (nonatomic) TabBarPickerSubItemsViewType type; +@property (nonatomic, strong) NSMutableArray *elementsArray; @end @implementation TabBarPickerSubItemsView -- (instancetype) initWithTabBarItem:(TabBarItem *) item andsubItemsPerRow:(NSUInteger) itemsPerRow { +- (instancetype) initWithType:(TabBarPickerSubItemsViewType) type andElements:(NSArray*) elements { self = [self initForAutoLayout]; if (self) { - _itemsPerRow = itemsPerRow; - _subItemsArray = [[NSMutableArray alloc] initWithArray:[item subItems]]; - _itemHeight = DEFAULT_ITEM_HEIGHT; - _rows = 0; - - _rows = ceil(([_subItemsArray count]/_itemsPerRow)); - + _type = type; + _elementsArray = [[NSMutableArray alloc] initWithArray:elements]; } [self updateConstraintsIfNeeded]; @@ -43,21 +35,11 @@ - (void) layoutSubviews { - /*if ([self.constraints count] > 0) { - - [NSLayoutConstraint deactivateConstraints:self.constraints]; - - for (TabBarItem *item in _itemsArray) { - for (TabBarSubItem *subItem in item.subItems) { - [subItem.constraints autoRemoveConstraints]; - } - } - }*/ if (!_didSetupConstraints) { - //[self autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:-(_rows*_itemHeight)]; [self autoMatchDimension:ALDimensionWidth toDimension:ALDimensionWidth ofView:self.superview withOffset:0 relation:NSLayoutRelationEqual]; [self autoMatchDimension:ALDimensionHeight toDimension:ALDimensionHeight ofView:self.superview withOffset:0 relation:NSLayoutRelationEqual]; + _didSetupConstraints = YES; } }