- Inizio lavoro su animazioni

This commit is contained in:
Giuseppe Nucifora 2015-07-22 16:26:28 +02:00
parent 967bf95336
commit 7a34b09ccf
7 changed files with 139 additions and 46 deletions

View File

@ -11,14 +11,14 @@
@class TabBarItem; @class TabBarItem;
@protocol TabBarItemDelegate <NSObject> /*@protocol TabBarItemDelegate <NSObject>
@required @required
- (void) tabBarItemSelected:(TabBarItem*) selectedItem; - (void) tabBarItemSelected:(TabBarItem*) selectedItem;
@end @end
*/
@interface TabBarItem : UIView @interface TabBarItem : UIView
/** /**
@ -37,10 +37,8 @@
* Represents the sub items that appeare when you select the item in picker. * Represents the sub items that appeare when you select the item in picker.
*/ */
@property (nonatomic, strong) NSMutableArray *subItems; @property (nonatomic, strong) NSMutableArray *subItems;
/**
* <#Description#> //@property (nonatomic, assign) id<TabBarItemDelegate> delegate;
*/
@property (nonatomic, assign) id<TabBarItemDelegate> delegate;
/** /**
* <#Description#> * <#Description#>

View File

@ -25,12 +25,9 @@ typedef NS_ENUM(NSInteger, TabBarPickerPosition) {
@property (nonatomic, readonly) CGSize tabBarSize; @property (nonatomic, readonly) CGSize tabBarSize;
@property (nonatomic, readonly) TabBarPickerPosition position; @property (nonatomic, readonly) TabBarPickerPosition position;
@property (nonatomic, readonly) NSLayoutRelation layoutRelation; @property (nonatomic, readonly) NSLayoutRelation layoutRelation;
@property (nonatomic) NSUInteger subItemPerRow;
@property (nonatomic) CGFloat paddingLeft;
@property (nonatomic) CGFloat paddingRight;
@property (nonatomic) CGFloat paddingTop;
@property (nonatomic) CGFloat paddingBottom;
@property (nonatomic) CGFloat itemSpacing; @property (nonatomic) CGFloat itemSpacing;
@property (nonatomic, readonly) NSUInteger subItemPerRow;
@property (nonatomic, readonly) CGFloat subItemHeight;
/** /**
* Init TabBarPicker with items. When is selected an item the picher show down subitems. * Init TabBarPicker with items. When is selected an item the picher show down subitems.
@ -115,6 +112,31 @@ typedef NS_ENUM(NSInteger, TabBarPickerPosition) {
*/ */
- (instancetype) initWithTabBarItems:(NSArray*) items withTabBarSize:(CGSize) size forPosition:(TabBarPickerPosition) position andNSLayoutRelation:(NSLayoutRelation) relation subItemsPerRow:(NSUInteger) subItemsPerRow; - (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#> * <#Description#>
* *

View File

@ -11,12 +11,14 @@
#import "TabBarPickerSubItemsView.h" #import "TabBarPickerSubItemsView.h"
#define DEFAULT_SUB_ITEMS_PER_ROW 2 #define DEFAULT_SUB_ITEMS_PER_ROW 2
#define DEFAULT_SUB_ITEM_HEIGHT 44
@interface TabBarPicker() <TabBarItemDelegate> @interface TabBarPicker() <TabBarPickerSubItemsViewDelegate>
@property (nonatomic) UIDeviceOrientation orientation; @property (nonatomic) UIDeviceOrientation orientation;
@property (nonatomic) NSUInteger subItemRows; @property (nonatomic) NSUInteger subItemRows;
@property (nonatomic, strong) TabBarPickerSubItemsView *subItemSelector; @property (nonatomic, strong) TabBarPickerSubItemsView *subItemSelector;
@property (nonatomic) BOOL show;
@end @end
@ -24,34 +26,36 @@
- (instancetype) initWithTabBarItems:(NSArray *) items forPosition:(TabBarPickerPosition) position { - (instancetype) initWithTabBarItems:(NSArray *) items forPosition:(TabBarPickerPosition) position {
return [self initWithTabBarItems:items withTabBarSize:CGSizeZero forPosition:position andNSLayoutRelation:NSLayoutRelationEqual subItemsPerRow:DEFAULT_SUB_ITEMS_PER_ROW]; return [self initWithTabBarItems:items withTabBarSize:CGSizeZero forPosition:position andNSLayoutRelation:NSLayoutRelationEqual subItemsPerRow:DEFAULT_SUB_ITEMS_PER_ROW subItemHeight:DEFAULT_SUB_ITEM_HEIGHT];
} }
- (instancetype) initWithTabBarItems:(NSArray *) items forPosition:(TabBarPickerPosition) position andNSLayoutRelation:(NSLayoutRelation) relation { - (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]; return [self initWithTabBarItems:items withTabBarSize:CGSizeZero 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 { - (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]; 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 { - (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 {
self = [self initForAutoLayout]; self = [self initForAutoLayout];
if (self) { if (self) {
_subItemPerRow = subItemsPerRow; _subItemPerRow = subItemsPerRow;
_itemSpacing = 10; _itemSpacing = 10;
_paddingLeft = 0;
_paddingRight = 0;
_paddingTop = 0;
_paddingBottom = 0;
_layoutRelation = relation; _layoutRelation = relation;
_subItemHeight = subItemHeight;
_position = position; _position = position;
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
@ -72,8 +76,8 @@
if ([_tabBarItems count] > 0) { if ([_tabBarItems count] > 0) {
_subItemSelector = [[TabBarPickerSubItemsView alloc] initWithTabBarItems:_tabBarItems]; _subItemSelector = [[TabBarPickerSubItemsView alloc] initWithTabBarItems:_tabBarItems andsubItemsPerRow:_subItemPerRow];
[_subItemSelector setDelegate:self];
[self addSubview:_subItemSelector]; [self addSubview:_subItemSelector];
} }
} }
@ -88,7 +92,7 @@
if ([self.constraints count] > 0) { if ([self.constraints count] > 0) {
[NSLayoutConstraint deactivateConstraints:self.constraints]; [NSLayoutConstraint deactivateConstraints:self.constraints];
[NSLayoutConstraint deactivateConstraints:_subItemSelector.constraints];
for (TabBarItem *item in _tabBarItems) { for (TabBarItem *item in _tabBarItems) {
[item.constraints autoRemoveConstraints]; [item.constraints autoRemoveConstraints];
} }
@ -138,17 +142,21 @@
break; break;
case TabBarPickerPositionBottom: case TabBarPickerPositionBottom:
default:{ default:{
if (!_show) {
[self autoPinEdgeToSuperviewMargin:ALEdgeBottom];
[self autoSetDimension:ALDimensionHeight toSize:44]; }
[self autoMatchDimension:ALDimensionWidth toDimension:ALDimensionWidth ofView:self.superview withOffset:0 relation:_layoutRelation]; else {
[self autoAlignAxisToSuperviewMarginAxis:ALAxisVertical]; [self autoPinEdgeToSuperviewMargin:ALEdgeBottom];
[self autoSetDimension:ALDimensionHeight toSize:44];
[_tabBarItems autoSetViewsDimension:ALDimensionHeight toSize:44.0]; [self autoMatchDimension:ALDimensionWidth toDimension:ALDimensionWidth ofView:self.superview withOffset:0 relation:_layoutRelation];
[self autoAlignAxisToSuperviewMarginAxis:ALAxisVertical];
[[_tabBarItems firstObject] autoAlignAxisToSuperviewAxis:ALAxisHorizontal];
[_tabBarItems autoSetViewsDimension:ALDimensionHeight toSize:44.0];
[_tabBarItems autoDistributeViewsAlongAxis:ALAxisHorizontal alignedTo:ALAttributeHorizontal withFixedSpacing:_itemSpacing insetSpacing:YES matchedSizes:YES];
[[_tabBarItems firstObject] autoAlignAxisToSuperviewAxis:ALAxisHorizontal];
[_tabBarItems autoDistributeViewsAlongAxis:ALAxisHorizontal alignedTo:ALAttributeHorizontal withFixedSpacing:_itemSpacing insetSpacing:YES matchedSizes:YES];
}
} }
break; break;
} }
@ -156,6 +164,10 @@
[self updateConstraintsIfNeeded]; [self updateConstraintsIfNeeded];
[_subItemSelector layoutSubviews]; [_subItemSelector layoutSubviews];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self show];
});
} }
- (void) setPosition:(TabBarPickerPosition)position { - (void) setPosition:(TabBarPickerPosition)position {
@ -175,7 +187,7 @@
if (item && [item isKindOfClass:[TabBarItem class]]) { if (item && [item isKindOfClass:[TabBarItem class]]) {
[_tabBarItems addObject:item]; [_tabBarItems addObject:item];
[item setDelegate:self]; //[item setDelegate:self];
[self addSubview:item]; [self addSubview:item];
@ -186,12 +198,37 @@
} }
} }
- (void) layoutSubviewsPortrait { - (void) show {
if (!_show) {
_show = YES;
[self setNeedsUpdateConstraints];
[self updateConstraintsIfNeeded];
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
[self layoutIfNeeded]; // this is what actually causes the views to animate to their new layout
}
completion:^(BOOL finished) {
// Run the animation again in the other direction
}];
}
}
- (void) hide {
} }
- (void) layoutSubviewsLandScape { #pragma mark TabBarPickerSubItemsViewDelegate
- (void) tabarPickerSubItemsView:(TabBarPickerSubItemsView*) tabarPickerSubItemsView didSelect:(TabBarItem*) item {
} }
#pragma mark -
@end @end

View File

@ -29,15 +29,16 @@
/** /**
* <#Description#> * <#Description#>
*/ */
@property (nonatomic, assign) id<TabBarItemDelegate> delegate; @property (nonatomic, assign) id<TabBarPickerSubItemsViewDelegate> delegate;
/** /**
* <#Description#> * <#Description#>
* *
* @param items <#items description#> * @param items <#items description#>
* @param itemsPerRow <#itemsPerRow description#>
* *
* @return <#return value description#> * @return <#return value description#>
*/ */
- (instancetype) initWithTabBarItems:(NSArray *) items; - (instancetype) initWithTabBarItems:(NSArray *) items andsubItemsPerRow:(NSUInteger) itemsPerRow;
@end @end

View File

@ -9,13 +9,37 @@
#import "TabBarPickerSubItemsView.h" #import "TabBarPickerSubItemsView.h"
#import <PureLayout/PureLayout.h> #import <PureLayout/PureLayout.h>
@interface TabBarPickerSubItemsView()
@property (nonatomic) NSUInteger itemsPerRow;
@property (nonatomic) NSUInteger rows;
@property (nonatomic, strong) UICollectionView *subItemCollectionView;
@end
@implementation TabBarPickerSubItemsView @implementation TabBarPickerSubItemsView
- (instancetype) initWithTabBarItems:(NSArray *) items { - (instancetype) initWithTabBarItems:(NSArray *) items andsubItemsPerRow:(NSUInteger) itemsPerRow {
self = [self initForAutoLayout]; self = [self initForAutoLayout];
if (self) { if (self) {
_itemsPerRow = itemsPerRow;
_itemsArray = [[NSMutableArray alloc] initWithArray:items]; _itemsArray = [[NSMutableArray alloc] initWithArray:items];
_rows = 0;
for (TabBarItem *item in _itemsArray) {
if (_rows < ([[item subItems] count]/_itemsPerRow)) {
_rows = ceil(([[item subItems] count]/_itemsPerRow));
}
}
[self setBackgroundColor:[UIColor whiteColor]];
/*_subItemCollectionView = [[UICollectionView alloc] initForAutoLayout];
[_subItemCollectionView setPagingEnabled:YES];
[_subItemCollectionView setDelegate:self];
[_subItemCollectionView setDataSource:self];
[self addSubview:_subItemCollectionView];*/
} }
[self updateConstraintsIfNeeded]; [self updateConstraintsIfNeeded];
@ -36,11 +60,13 @@
} }
} }
} }
[self autoMatchDimension:ALDimensionWidth toDimension:ALDimensionWidth ofView:self.superview withOffset:0 relation:NSLayoutRelationEqual];
[self autoSetDimension:ALDimensionHeight toSize:44];
[self autoConstrainAttribute:ALEdgeTop toAttribute:ALMarginTop ofView:self.superview withOffset:self.superview.frame.size.height]; [self autoConstrainAttribute:ALEdgeTop toAttribute:ALMarginTop ofView:self.superview withOffset:self.superview.frame.size.height];
[self autoMatchDimension:ALDimensionWidth toDimension:ALDimensionWidth ofView:self.superview withOffset:0 relation:NSLayoutRelationEqual];
[self autoSetDimension:ALDimensionHeight toSize:_rows*44];
[_subItemCollectionView autoMatchDimension:ALDimensionWidth toDimension:ALDimensionWidth ofView:self];
[_subItemCollectionView autoMatchDimension:ALDimensionHeight toDimension:ALDimensionHeight ofView:self];
[_subItemCollectionView setBackgroundColor:[UIColor redColor]];
} }
@end @end

View File

@ -11,6 +11,7 @@
@interface TabBarSubItem : UIView @interface TabBarSubItem : UIView
@property (nonatomic, strong) NSString *name; @property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) UIButton *subItemButton;
+ (instancetype) tabBarSubItemWithName:(NSString*)name; + (instancetype) tabBarSubItemWithName:(NSString*)name;

View File

@ -26,6 +26,9 @@
[[NSNotificationCenter defaultCenter] addObserver: self selector:@selector(deviceOrientationDidChange:) name: UIDeviceOrientationDidChangeNotification object: nil]; [[NSNotificationCenter defaultCenter] addObserver: self selector:@selector(deviceOrientationDidChange:) name: UIDeviceOrientationDidChangeNotification object: nil];
_name = name; _name = name;
_subItemButton = [[UIButton alloc] initForAutoLayout];
[_subItemButton setTitle:_name forState:UIControlStateNormal];
} }
return self; return self;
} }
@ -34,6 +37,11 @@
return [[self alloc] initWithName:name]; return [[self alloc] initWithName:name];
} }
- (void) setName:(NSString *)name {
_name = name;
[_subItemButton setTitle:name forState:UIControlStateNormal];
}
- (void)deviceOrientationDidChange:(NSNotification *)notification { - (void)deviceOrientationDidChange:(NSNotification *)notification {
//Obtain current device orientation //Obtain current device orientation