- Fix
- upgrade Pods
This commit is contained in:
1 parent
1797083a99
commit
4936fcd2ec
131 files changed
+2426
-2093
No files matched your search
@@ -11,14 +11,14 @@
|
||||
|
||||
@class TabBarItem;
|
||||
|
||||
/*@protocol TabBarItemDelegate <NSObject>
|
||||
@protocol TabBarItemDelegate <NSObject>
|
||||
|
||||
@required
|
||||
|
||||
- (void) tabBarItemSelected:(TabBarItem*) selectedItem;
|
||||
|
||||
@end
|
||||
*/
|
||||
|
||||
@interface TabBarItem : UIView
|
||||
|
||||
/**
|
||||
@@ -29,6 +29,11 @@
|
||||
* Represents the image visible in picker when item is selected.
|
||||
*/
|
||||
@property (nonatomic, strong) UIImage *selectedImage;
|
||||
/**
|
||||
* <#Description#>
|
||||
*/
|
||||
@property (nonatomic, strong) UIColor *highlightColor;
|
||||
|
||||
/**
|
||||
* Represents the name of the item that can be shown in picker.
|
||||
*/
|
||||
@@ -38,7 +43,7 @@
|
||||
*/
|
||||
@property (nonatomic, strong) NSMutableArray *subItems;
|
||||
|
||||
//@property (nonatomic, assign) id<TabBarItemDelegate> delegate;
|
||||
@property (nonatomic, assign) id<TabBarItemDelegate> delegate;
|
||||
|
||||
/**
|
||||
* <#Description#>
|
||||
@@ -49,5 +54,7 @@
|
||||
*/
|
||||
- (instancetype) initWithSubItems:(NSArray*) array;
|
||||
|
||||
- (void) setHighlighted:(BOOL) highlighted;
|
||||
|
||||
|
||||
@end
|
||||
@@ -8,6 +8,7 @@
|
||||
|
||||
#import "TabBarItem.h"
|
||||
#import <PureLayout/PureLayout.h>
|
||||
#import "NSString+HexColor.h"
|
||||
|
||||
@interface TabBarItem()
|
||||
|
||||
@@ -29,6 +30,8 @@
|
||||
_orientation = [[UIDevice currentDevice] orientation];
|
||||
|
||||
_itemButton = [[UIButton alloc] initForAutoLayout];
|
||||
[_itemButton addTarget:self action:@selector(itemButtonTapped) forControlEvents:UIControlEventTouchUpInside];
|
||||
_highlightColor = [@"ff4e50" colorFromHex];
|
||||
|
||||
[self addSubview:_itemButton];
|
||||
|
||||
@@ -70,4 +73,35 @@
|
||||
[_itemButton setImage:selectedImage forState:UIControlStateSelected];
|
||||
}
|
||||
|
||||
- (void) setHighlightColor:(UIColor *)highlightColor {
|
||||
|
||||
_highlightColor = highlightColor;
|
||||
}
|
||||
|
||||
- (void) setHighlighted:(BOOL) highlighted {
|
||||
[_itemButton setHighlighted:highlighted];
|
||||
if ([_itemButton isHighlighted]) {
|
||||
[_itemButton setBackgroundColor:_highlightColor];
|
||||
|
||||
}
|
||||
else {
|
||||
[_itemButton setBackgroundColor:[UIColor clearColor]];
|
||||
}
|
||||
}
|
||||
|
||||
- (void) itemButtonTapped {
|
||||
|
||||
if ([_itemButton isHighlighted]) {
|
||||
[_itemButton setBackgroundColor:_highlightColor];
|
||||
|
||||
}
|
||||
else {
|
||||
[_itemButton setBackgroundColor:[UIColor clearColor]];
|
||||
}
|
||||
|
||||
if (_delegate && [_delegate respondsToSelector:@selector(tabBarItemSelected:)]) {
|
||||
[_delegate tabBarItemSelected:self];
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -28,6 +28,8 @@ typedef NS_ENUM(NSInteger, TabBarPickerPosition) {
|
||||
@property (nonatomic) CGFloat itemSpacing;
|
||||
@property (nonatomic, readonly) NSUInteger subItemPerRow;
|
||||
@property (nonatomic, readonly) CGFloat subItemHeight;
|
||||
@property (nonatomic) BOOL dimWhenShow;
|
||||
@property (nonatomic, strong) UIColor * dimColor;
|
||||
|
||||
/**
|
||||
* Init TabBarPicker with items. When is selected an item the picher show down subitems.
|
||||
@@ -144,4 +146,8 @@ typedef NS_ENUM(NSInteger, TabBarPickerPosition) {
|
||||
*/
|
||||
- (void) addItem:(TabBarItem*) item;
|
||||
|
||||
- (void) show;
|
||||
|
||||
- (void) hide;
|
||||
|
||||
@end
|
||||
+170
-38
@@ -9,20 +9,26 @@
|
||||
#import "TabBarPicker.h"
|
||||
#import <PureLayout/PureLayout.h>
|
||||
#import "TabBarPickerSubItemsView.h"
|
||||
#import "MMCPSScrollView.h"
|
||||
#import <UIView-Overlay/UIView+Overlay.h>
|
||||
|
||||
#define DEFAULT_SUB_ITEMS_PER_ROW 2
|
||||
#define DEFAULT_SUB_ITEM_HEIGHT 44
|
||||
|
||||
@interface TabBarPicker() <TabBarPickerSubItemsViewDelegate>
|
||||
@interface TabBarPicker() <TabBarPickerSubItemsViewDelegate,TabBarItemDelegate>
|
||||
|
||||
@property (nonatomic) UIDeviceOrientation orientation;
|
||||
@property (nonatomic) NSUInteger subItemRows;
|
||||
@property (nonatomic, strong) TabBarPickerSubItemsView *subItemSelector;
|
||||
@property (nonatomic) BOOL show;
|
||||
@property (nonatomic, strong) NSMutableArray *subItemSelectors;
|
||||
@property (nonatomic, strong) NSMutableArray *subItemSelectorsConstraints;
|
||||
@property (nonatomic) BOOL isShow;
|
||||
@property (nonatomic, assign) BOOL didSetupConstraints;
|
||||
@property (nonatomic, strong) NSLayoutConstraint *showConstraint;
|
||||
|
||||
@property (nonatomic, strong) NSLayoutConstraint *hideConstraint;
|
||||
@property (nonatomic, strong) NSMutableArray *tabBarItemsConstraints;
|
||||
@property (nonatomic, strong) TabBarItem *selectedTabBarItem;
|
||||
|
||||
@property (nonatomic, strong) MMCPSScrollView *subItemScrollView;
|
||||
|
||||
@end
|
||||
|
||||
@@ -61,10 +67,15 @@
|
||||
_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];
|
||||
|
||||
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
|
||||
|
||||
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(deviceOrientationDidChange:) name: UIDeviceOrientationDidChangeNotification object: nil];
|
||||
[[NSNotificationCenter defaultCenter] addObserver: self selector:@selector(deviceOrientationDidChange:) name:UIDeviceOrientationDidChangeNotification object: nil];
|
||||
|
||||
NSAssert(items, @"TabBar Items array cannot be nil!");
|
||||
|
||||
@@ -75,14 +86,49 @@
|
||||
_tabBarItems = [[NSMutableArray alloc] init];
|
||||
|
||||
for (NSObject *item in items) {
|
||||
[self addItem:item];
|
||||
if (item && [item isKindOfClass:[TabBarItem class]]) {
|
||||
|
||||
[_tabBarItems addObject:item];
|
||||
[(TabBarItem*)item setDelegate:self];
|
||||
[self addSubview:item];
|
||||
}
|
||||
}
|
||||
|
||||
_subItemScrollView = [[MMCPSScrollView alloc] initForAutoLayout];
|
||||
[_subItemScrollView setType:MMCPSScrollHorizontal];
|
||||
[_subItemScrollView setMMCPSDelegate:self];
|
||||
[_subItemScrollView setPagingEnabled:YES];
|
||||
|
||||
[self addSubview:_subItemScrollView];
|
||||
|
||||
if ([_tabBarItems count] > 0) {
|
||||
|
||||
_subItemSelector = [[TabBarPickerSubItemsView alloc] initWithTabBarItems:_tabBarItems andsubItemsPerRow:_subItemPerRow];
|
||||
[_subItemSelector setDelegate:self];
|
||||
[self addSubview:_subItemSelector];
|
||||
int i = 0;
|
||||
for (TabBarItem *item in _tabBarItems) {
|
||||
TabBarPickerSubItemsView *subItemSelector = [[TabBarPickerSubItemsView alloc] initWithTabBarItem:item andsubItemsPerRow:_subItemPerRow];
|
||||
[subItemSelector setDelegate:self];
|
||||
switch (i) {
|
||||
case 0:
|
||||
|
||||
break;
|
||||
case 1:{
|
||||
[subItemSelector setBackgroundColor:[UIColor redColor]];
|
||||
}
|
||||
break;
|
||||
case 2:{
|
||||
[subItemSelector setBackgroundColor:[UIColor greenColor]];
|
||||
}
|
||||
break;
|
||||
case 3: {
|
||||
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
[_subItemScrollView addSubview:subItemSelector];
|
||||
|
||||
[_subItemSelectors addObject:subItemSelector];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -157,24 +203,26 @@
|
||||
|
||||
[[_tabBarItems firstObject] autoAlignAxisToSuperviewAxis:ALAxisHorizontal];
|
||||
|
||||
[_tabBarItems autoDistributeViewsAlongAxis:ALAxisHorizontal alignedTo:ALAttributeHorizontal withFixedSpacing:_itemSpacing insetSpacing:YES matchedSizes:YES];
|
||||
[_tabBarItemsConstraints addObjectsFromArray:[_tabBarItems autoDistributeViewsAlongAxis:ALAxisHorizontal alignedTo:ALAttributeHorizontal withFixedSpacing:_itemSpacing insetSpacing:YES matchedSizes:YES]];
|
||||
|
||||
[_subItemSelectors autoDistributeViewsAlongAxis:ALAxisHorizontal alignedTo:ALAttributeHorizontal withFixedSpacing:0 insetSpacing:YES matchedSizes:YES];
|
||||
|
||||
[_subItemScrollView autoMatchDimension:ALDimensionWidth toDimension:ALDimensionWidth ofView:self];
|
||||
[_subItemScrollView autoSetDimension:ALDimensionHeight toSize:343];
|
||||
[_subItemScrollView autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:[_tabBarItems firstObject]];
|
||||
[_subItemScrollView autoAlignAxisToSuperviewAxis:ALAxisVertical];
|
||||
[_subItemScrollView setBackgroundColor:[UIColor blueColor]];
|
||||
|
||||
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
[_subItemSelector layoutSubviews];
|
||||
|
||||
_didSetupConstraints = YES;
|
||||
|
||||
[self updateConstraintsIfNeeded];
|
||||
}
|
||||
|
||||
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
|
||||
[self show];
|
||||
});
|
||||
|
||||
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
|
||||
[self hide];
|
||||
});
|
||||
}
|
||||
|
||||
- (void) setPosition:(TabBarPickerPosition)position {
|
||||
@@ -194,58 +242,108 @@
|
||||
if (item && [item isKindOfClass:[TabBarItem class]]) {
|
||||
|
||||
[_tabBarItems addObject:item];
|
||||
//[item setDelegate:self];
|
||||
[item setDelegate:self];
|
||||
[self addSubview:item];
|
||||
|
||||
[_tabBarItemsConstraints autoRemoveConstraints];
|
||||
|
||||
if (self.superview) {
|
||||
|
||||
[self layoutSubviews];
|
||||
}
|
||||
[_tabBarItemsConstraints addObjectsFromArray:[_tabBarItems autoDistributeViewsAlongAxis:ALAxisHorizontal alignedTo:ALAttributeHorizontal withFixedSpacing:_itemSpacing insetSpacing:YES matchedSizes:YES]];
|
||||
}
|
||||
}
|
||||
|
||||
- (void) show {
|
||||
|
||||
if (!_show) {
|
||||
if (!_isShow) {
|
||||
|
||||
_show = YES;
|
||||
|
||||
_isShow = YES;
|
||||
[self setNeedsUpdateConstraints];
|
||||
[self updateConstraintsIfNeeded];
|
||||
|
||||
[UIView animateWithDuration:4
|
||||
[UIView animateWithDuration:1.5
|
||||
delay:0.0
|
||||
usingSpringWithDamping:0.6
|
||||
usingSpringWithDamping:1
|
||||
initialSpringVelocity:0
|
||||
options:0
|
||||
animations:^{
|
||||
[_hideConstraint autoRemove];
|
||||
if (_dimWhenShow) {
|
||||
|
||||
[self.superview ag_addOverlayWithColor:_dimColor];
|
||||
self.layer.zPosition = 999;
|
||||
}
|
||||
switch (_position) {
|
||||
case TabBarPickerPositionLeft: {
|
||||
|
||||
break;
|
||||
}
|
||||
case TabBarPickerPositionRight: {
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case TabBarPickerPositionTop: {
|
||||
|
||||
break;
|
||||
}
|
||||
case TabBarPickerPositionBottom:
|
||||
default: {
|
||||
[_hideConstraint autoRemove];
|
||||
|
||||
_showConstraint = [self autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:_subItemScrollView.frame.size.height];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
_showConstraint = [self autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:_subItemSelector.frame.size.height];
|
||||
[self layoutIfNeeded];
|
||||
}
|
||||
completion:^(BOOL finished) {
|
||||
// Run the animation again in the other direction
|
||||
}];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
- (void) hide {
|
||||
if (_show) {
|
||||
if (_isShow) {
|
||||
|
||||
_show = NO;
|
||||
_isShow = NO;
|
||||
[self setNeedsUpdateConstraints];
|
||||
[self updateConstraintsIfNeeded];
|
||||
|
||||
[UIView animateWithDuration:4
|
||||
[UIView animateWithDuration:1.5
|
||||
delay:0.0
|
||||
usingSpringWithDamping:0.6
|
||||
usingSpringWithDamping:1
|
||||
initialSpringVelocity:0
|
||||
options:0
|
||||
animations:^{
|
||||
if (_dimWhenShow) {
|
||||
[self.superview ag_removeOverlay];
|
||||
self.layer.zPosition = 0;
|
||||
}
|
||||
switch (_position) {
|
||||
case TabBarPickerPositionLeft: {
|
||||
|
||||
break;
|
||||
}
|
||||
case TabBarPickerPositionRight: {
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case TabBarPickerPositionTop: {
|
||||
|
||||
break;
|
||||
}
|
||||
case TabBarPickerPositionBottom:
|
||||
default: {
|
||||
[_showConstraint autoRemove];
|
||||
|
||||
_hideConstraint = [self autoPinEdgeToSuperviewMargin:ALEdgeBottom];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
[_showConstraint autoRemove];
|
||||
|
||||
_hideConstraint = [self autoPinEdgeToSuperviewMargin:ALEdgeBottom];
|
||||
[self layoutIfNeeded];
|
||||
}
|
||||
completion:^(BOOL finished) {
|
||||
// Run the animation again in the other direction
|
||||
@@ -261,4 +359,38 @@
|
||||
|
||||
#pragma mark -
|
||||
|
||||
#pragma mark TabBarItemDelegate
|
||||
|
||||
- (void) tabBarItemSelected:(TabBarItem *)selectedItem {
|
||||
for (TabBarItem *item in _tabBarItems) {
|
||||
if (![item isEqual:selectedItem]) {
|
||||
[item setHighlighted:NO];
|
||||
}
|
||||
}
|
||||
|
||||
if (!_isShow) {
|
||||
[self show];
|
||||
}
|
||||
else {
|
||||
if(![_selectedTabBarItem isEqual:selectedItem]) {
|
||||
|
||||
}
|
||||
}
|
||||
_selectedTabBarItem = selectedItem;
|
||||
}
|
||||
|
||||
#pragma mark -
|
||||
|
||||
#pragma mark MMCPSScrollViewDelegate
|
||||
|
||||
- (void)scrollView:(MMCPSScrollView *)scrollView didScrollToPage:(NSUInteger)pageIndex {
|
||||
NSLog(@"The MMCPSScrollView is now on page %i.", pageIndex);
|
||||
}
|
||||
|
||||
- (void)scrollView:(MMCPSScrollView *)scrollView willScrollToPage:(NSUInteger)pageIndex {
|
||||
NSLog(@"The MMCPSScrollView is now going to page %i.", pageIndex);
|
||||
}
|
||||
|
||||
#pragma mark -
|
||||
|
||||
@end
|
||||
@@ -24,7 +24,7 @@
|
||||
/**
|
||||
* <#Description#>
|
||||
*/
|
||||
@property (nonatomic, strong) NSMutableArray *itemsArray;
|
||||
@property (nonatomic, strong) NSMutableArray *subItemsArray;
|
||||
|
||||
/**
|
||||
* <#Description#>
|
||||
@@ -39,6 +39,6 @@
|
||||
*
|
||||
* @return <#return value description#>
|
||||
*/
|
||||
- (instancetype) initWithTabBarItems:(NSArray *) items andsubItemsPerRow:(NSUInteger) itemsPerRow;
|
||||
- (instancetype) initWithTabBarItem:(TabBarItem *) item andsubItemsPerRow:(NSUInteger) itemsPerRow;
|
||||
|
||||
@end
|
||||
@@ -16,35 +16,23 @@
|
||||
@property (nonatomic) NSUInteger itemsPerRow;
|
||||
@property (nonatomic) CGFloat itemHeight;
|
||||
@property (nonatomic) NSUInteger rows;
|
||||
@property (nonatomic, strong) UICollectionView *subItemCollectionView;
|
||||
@property (nonatomic, assign) BOOL didSetupConstraints;
|
||||
|
||||
@end
|
||||
|
||||
@implementation TabBarPickerSubItemsView
|
||||
|
||||
- (instancetype) initWithTabBarItems:(NSArray *) items andsubItemsPerRow:(NSUInteger) itemsPerRow {
|
||||
- (instancetype) initWithTabBarItem:(TabBarItem *) item andsubItemsPerRow:(NSUInteger) itemsPerRow {
|
||||
self = [self initForAutoLayout];
|
||||
|
||||
if (self) {
|
||||
_itemsPerRow = itemsPerRow;
|
||||
_itemsArray = [[NSMutableArray alloc] initWithArray:items];
|
||||
_subItemsArray = [[NSMutableArray alloc] initWithArray:[item subItems]];
|
||||
_itemHeight = DEFAULT_ITEM_HEIGHT;
|
||||
_rows = 0;
|
||||
|
||||
for (TabBarItem *item in _itemsArray) {
|
||||
if (_rows < ([[item subItems] count]/_itemsPerRow)) {
|
||||
_rows = ceil(([[item subItems] count]/_itemsPerRow));
|
||||
}
|
||||
}
|
||||
_rows = ceil(([_subItemsArray count]/_itemsPerRow));
|
||||
|
||||
[self setBackgroundColor:[UIColor redColor]];
|
||||
/*_subItemCollectionView = [[UICollectionView alloc] initForAutoLayout];
|
||||
[_subItemCollectionView setPagingEnabled:YES];
|
||||
[_subItemCollectionView setDelegate:self];
|
||||
[_subItemCollectionView setDataSource:self];
|
||||
|
||||
[self addSubview:_subItemCollectionView];*/
|
||||
}
|
||||
|
||||
[self updateConstraintsIfNeeded];
|
||||
@@ -71,9 +59,6 @@
|
||||
[self autoMatchDimension:ALDimensionWidth toDimension:ALDimensionWidth ofView:self.superview withOffset:0 relation:NSLayoutRelationEqual];
|
||||
[self autoSetDimension:ALDimensionHeight toSize:_rows*_itemHeight];
|
||||
|
||||
[_subItemCollectionView autoMatchDimension:ALDimensionWidth toDimension:ALDimensionWidth ofView:self];
|
||||
[_subItemCollectionView autoMatchDimension:ALDimensionHeight toDimension:ALDimensionHeight ofView:self];
|
||||
[_subItemCollectionView setBackgroundColor:[UIColor redColor]];
|
||||
_didSetupConstraints = YES;
|
||||
}
|
||||
}
|
||||
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
//
|
||||
// MMCPSScrollView.h
|
||||
// MMCPSScrollView
|
||||
//
|
||||
// Created by Martin on 02-04-13.
|
||||
// Copyright (c) 2013 Martin Metselaar. All rights reserved.
|
||||
//
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
|
||||
typedef enum {
|
||||
MMCPSScrollHorizontal,
|
||||
MMCPSScrollVertical
|
||||
} MMCPSScrollType;
|
||||
|
||||
@class MMCPSScrollView;
|
||||
|
||||
@protocol MMCPSScrollViewDelegate <NSObject>
|
||||
|
||||
- (void) scrollView:(MMCPSScrollView *) scrollView willScrollToPage:(NSUInteger) pageIndex;
|
||||
- (void) scrollView:(MMCPSScrollView *) scrollView didScrollToPage:(NSUInteger) pageIndex;
|
||||
|
||||
@end
|
||||
|
||||
@interface MMCPSScrollView : UIScrollView <UIScrollViewDelegate> {
|
||||
MMCPSScrollType _type;
|
||||
|
||||
CGPoint _endScrollingPoint;
|
||||
CGPoint _startDraggingPoint;
|
||||
NSInteger _pageToScrollToo;
|
||||
|
||||
CGRect _bottomComponent;
|
||||
}
|
||||
|
||||
// The height of one segment
|
||||
@property (nonatomic) NSInteger segmentSize;
|
||||
// The number of segments that represent one page
|
||||
@property (nonatomic) NSInteger pageSize;
|
||||
@property (nonatomic) MMCPSScrollType type;
|
||||
|
||||
// Set to true if you want it to fit the screen.
|
||||
// Set to false if you want to have white space.
|
||||
@property (nonatomic, assign) BOOL enableFitScreen;
|
||||
|
||||
// Time it takes to scroll to next page
|
||||
@property (nonatomic) CGFloat scrollingTime;
|
||||
|
||||
@property (nonatomic) NSUInteger currentPage;
|
||||
|
||||
@property (nonatomic, retain) id<MMCPSScrollViewDelegate> MMCPSDelegate;
|
||||
|
||||
- (id)initWithFrame:(CGRect)frame andType:(MMCPSScrollType) type;
|
||||
|
||||
@end
|
||||
+321
@@ -0,0 +1,321 @@
|
||||
//
|
||||
// MMCPSScrollView.m
|
||||
// MMCPSScrollView
|
||||
//
|
||||
// Created by Martin on 02-04-13.
|
||||
// Copyright (c) 2013 Martin Metselaar. All rights reserved.
|
||||
//
|
||||
|
||||
#import "MMCPSScrollView.h"
|
||||
|
||||
@implementation MMCPSScrollView
|
||||
|
||||
@synthesize type = _type;
|
||||
|
||||
- (id)initWithFrame:(CGRect)frame
|
||||
{
|
||||
self = [self initWithFrame:frame andType:MMCPSScrollVertical];
|
||||
|
||||
if (self) {
|
||||
self.backgroundColor = [UIColor whiteColor];
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (id)initWithFrame:(CGRect)frame andType:(MMCPSScrollType) type
|
||||
{
|
||||
self = [super initWithFrame:frame];
|
||||
|
||||
if (self) {
|
||||
_segmentSize = [self CGRectSize:frame];
|
||||
_pageSize = 1;
|
||||
_type = type;
|
||||
_scrollingTime = 0.35f;
|
||||
|
||||
_bottomComponent = CGRectZero;
|
||||
_endScrollingPoint = CGPointZero;
|
||||
|
||||
self.showsHorizontalScrollIndicator = NO;
|
||||
self.showsVerticalScrollIndicator = NO;
|
||||
self.delegate = self;
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
#pragma mark - ScrollView Position Helpers
|
||||
// These functions are for getting the right value for the calculations to make horizontal and vertical scrolling possible
|
||||
|
||||
- (CGFloat) CGPoint:(CGPoint) point {
|
||||
if (_type == MMCPSScrollVertical)
|
||||
return point.y;
|
||||
else
|
||||
return point.x;
|
||||
}
|
||||
|
||||
- (CGFloat) contentScrollOffset:(UIScrollView *) scrollView {
|
||||
if (_type == MMCPSScrollVertical)
|
||||
return scrollView.contentOffset.y;
|
||||
else
|
||||
return scrollView.contentOffset.x;
|
||||
}
|
||||
|
||||
- (CGFloat) contentScrollSize:(UIScrollView *) scrollView {
|
||||
if (_type == MMCPSScrollVertical)
|
||||
return scrollView.contentSize.height;
|
||||
else
|
||||
return scrollView.contentSize.width;
|
||||
}
|
||||
|
||||
- (CGFloat) contentSize:(CGSize) size {
|
||||
if (_type == MMCPSScrollVertical)
|
||||
return size.height;
|
||||
else
|
||||
return size.width;
|
||||
}
|
||||
|
||||
- (CGFloat) contentSizeReversed:(CGSize) size {
|
||||
if (_type == MMCPSScrollVertical)
|
||||
return size.width;
|
||||
else
|
||||
return size.height;
|
||||
}
|
||||
|
||||
- (CGFloat) frameSize:(UIView *) view {
|
||||
return [self CGRectSize:view.frame];
|
||||
}
|
||||
|
||||
- (CGFloat) frameOrigin:(UIView *) view {
|
||||
return [self CGRectOrigin:view.frame];
|
||||
}
|
||||
|
||||
- (CGFloat) CGRectOrigin:(CGRect) rect {
|
||||
if (_type == MMCPSScrollVertical)
|
||||
return rect.origin.y;
|
||||
else
|
||||
return rect.origin.x;
|
||||
}
|
||||
|
||||
- (CGFloat) CGRectSize:(CGRect) rect {
|
||||
if (_type == MMCPSScrollVertical)
|
||||
return rect.size.height;
|
||||
else
|
||||
return rect.size.width;
|
||||
}
|
||||
|
||||
- (CGSize) setCGSize:(CGSize) size withValue:(CGFloat) value {
|
||||
if (_type == MMCPSScrollVertical)
|
||||
size.height = value;
|
||||
else
|
||||
size.width = value;
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
- (CGSize) CGSizeMake:(CGFloat) value1 and:(CGFloat) value2 {
|
||||
if (_type == MMCPSScrollVertical)
|
||||
return CGSizeMake(value1, value2);
|
||||
else
|
||||
return CGSizeMake(value2, value1);
|
||||
}
|
||||
|
||||
#pragma mark - Custom ScrollView Helpers
|
||||
|
||||
- (BOOL) isScrollViewBouncing:(UIScrollView *) scrollView {
|
||||
if ([self contentScrollOffset:scrollView] < 0)
|
||||
return YES;
|
||||
|
||||
if ([self contentScrollOffset:scrollView] > ([self contentScrollSize:scrollView] - [self frameSize:scrollView]))
|
||||
return YES;
|
||||
|
||||
return NO;
|
||||
}
|
||||
|
||||
#pragma mark - Scroll to pages
|
||||
|
||||
- (void) scrollToPage:(NSInteger) index {
|
||||
[self scrollToPage:index withHeight:_segmentSize];
|
||||
}
|
||||
|
||||
- (void) scrollToPage:(NSInteger) index withHeight:(NSInteger) height {
|
||||
[self scrollToPage:index withHeight:_segmentSize andSize:_pageSize];
|
||||
}
|
||||
|
||||
- (void) scrollToPage:(NSInteger) index withHeight:(NSInteger) height andSize:(NSInteger) size {
|
||||
_currentPage = index + 1;
|
||||
// height is the height/width for one segment
|
||||
// index is the index of the page
|
||||
// size is the number of segments that present one page
|
||||
int value = height * index * size;
|
||||
|
||||
// check if the page where the user wants so scroll to is in the contentSize of the ScrollView
|
||||
if (!(value + [self frameSize:self] < [self contentScrollSize:self])) {
|
||||
value = [self contentScrollSize:self] - [self frameSize:self];
|
||||
}
|
||||
|
||||
[self setContentScrollOffset:value];
|
||||
|
||||
if ([_MMCPSDelegate respondsToSelector:@selector(scrollView:willScrollToPage:)])
|
||||
[_MMCPSDelegate scrollView:self willScrollToPage:_currentPage];
|
||||
|
||||
}
|
||||
|
||||
- (void)setContentScrollOffset:(CGFloat) value {
|
||||
|
||||
CGPoint contentOffset = CGPointZero;
|
||||
if (_type == MMCPSScrollVertical)
|
||||
contentOffset = CGPointMake(0, value);
|
||||
else
|
||||
contentOffset = CGPointMake(value, 0);
|
||||
|
||||
[UIView animateWithDuration:_scrollingTime delay:0.0f options:UIViewAnimationOptionCurveEaseOut
|
||||
animations:^{ [self setContentOffset:contentOffset animated:NO]; }
|
||||
completion:^(BOOL finished){
|
||||
if (finished) {
|
||||
if ([_MMCPSDelegate respondsToSelector:@selector(scrollView:didScrollToPage:)])
|
||||
[_MMCPSDelegate scrollView:self didScrollToPage:_currentPage];
|
||||
}
|
||||
}];
|
||||
|
||||
}
|
||||
|
||||
// Scroll forward multiple pages
|
||||
- (void) scrollPagesForward:(NSInteger) value {
|
||||
for (int i = 0; i < value; i++) {
|
||||
_pageToScrollToo++;
|
||||
}
|
||||
|
||||
[self scrollToPage:_pageToScrollToo];
|
||||
}
|
||||
|
||||
// Scroll back multiple pages
|
||||
- (void) scrollPagesBack:(NSInteger) value {
|
||||
value++;
|
||||
for (int i = 0; i < value; i++) {
|
||||
_pageToScrollToo--;
|
||||
}
|
||||
|
||||
[self scrollToPage:_pageToScrollToo];
|
||||
}
|
||||
|
||||
// Scroll to next page
|
||||
- (void) scrollToNextPage {
|
||||
_pageToScrollToo++;
|
||||
[self scrollToPage:_pageToScrollToo];
|
||||
}
|
||||
|
||||
// Scroll to previous page
|
||||
- (void) scrollToPreviousPage {
|
||||
if (!_pageToScrollToo <= 0) {
|
||||
_pageToScrollToo--;
|
||||
} else
|
||||
_pageToScrollToo = 0;
|
||||
|
||||
[self scrollToPage:_pageToScrollToo];
|
||||
}
|
||||
|
||||
#pragma mark - UIScrollViewDelegate
|
||||
|
||||
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
|
||||
// Keep track when started dragging
|
||||
_startDraggingPoint = scrollView.contentOffset;
|
||||
}
|
||||
|
||||
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
|
||||
// Check if the ScrollView is within his bounds
|
||||
if ([self isScrollViewBouncing:scrollView])
|
||||
return;
|
||||
|
||||
// Check is any scrolling is needed.
|
||||
if ((int) [self contentScrollOffset:scrollView] % (_segmentSize * _pageSize ) != 0) {
|
||||
// If the user is not dragging the ScrollView anymore.
|
||||
if (!scrollView.isDragging) {
|
||||
|
||||
int startPoint = [self CGPoint:_startDraggingPoint];
|
||||
float lengthScrolled = [self contentScrollOffset:scrollView] - startPoint;
|
||||
|
||||
float numberOfSegments;
|
||||
if (lengthScrolled < 0.0f)
|
||||
numberOfSegments = floor(lengthScrolled / _segmentSize);
|
||||
else
|
||||
numberOfSegments = ceil(lengthScrolled / _segmentSize);
|
||||
|
||||
int pagesToScroll;
|
||||
if (numberOfSegments < 0.0f)
|
||||
pagesToScroll = floor(numberOfSegments / _pageSize);
|
||||
else
|
||||
pagesToScroll = ceil(numberOfSegments / _pageSize);
|
||||
|
||||
if (pagesToScroll < 0) {
|
||||
[self scrollPagesBack:abs(pagesToScroll + 1)];
|
||||
}else
|
||||
[self scrollPagesForward:pagesToScroll];
|
||||
|
||||
return;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// Content Offset where the user did end dragging
|
||||
_endScrollingPoint = scrollView.contentOffset;
|
||||
}
|
||||
|
||||
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
|
||||
|
||||
// Check if the scrollview is not bouncing.
|
||||
if ([self isScrollViewBouncing:scrollView])
|
||||
return;
|
||||
|
||||
// If the _endScrollingPoint has been set then the user has been dragging the ScrollView.
|
||||
// So then we can determite what to do.
|
||||
if (!CGPointEqualToPoint(_endScrollingPoint, CGPointZero)) {
|
||||
|
||||
int startPoint = [self CGPoint:_endScrollingPoint];
|
||||
_endScrollingPoint = CGPointZero;
|
||||
|
||||
if (startPoint < [self contentScrollOffset:scrollView]) {
|
||||
[self scrollToNextPage];
|
||||
} else {
|
||||
[self scrollToPreviousPage];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
|
||||
// When the scrolling is done decelerating then reset the _endScrollingPoint
|
||||
_endScrollingPoint = CGPointZero;
|
||||
}
|
||||
|
||||
#pragma mark - UIView
|
||||
|
||||
- (void)addSubview:(UIView *)view {
|
||||
[super addSubview:view];
|
||||
|
||||
int currentLowestPoint = [self CGRectOrigin:_bottomComponent] + [self CGRectSize:_bottomComponent];
|
||||
int viewLowestPoint = [self frameOrigin:view] + [self frameSize:view];
|
||||
|
||||
if (currentLowestPoint < viewLowestPoint) {
|
||||
// New lowest point
|
||||
_bottomComponent = view.frame;
|
||||
|
||||
CGSize contentSize = [self CGSizeMake:[self contentSizeReversed:self.contentSize]
|
||||
and:[self frameOrigin:view] + [self frameSize:view]];
|
||||
|
||||
// Check if the contentSize does not corresponds with the pages
|
||||
if (!_enableFitScreen && !((int)contentSize.height % (_pageSize * _segmentSize) == 0)) {
|
||||
|
||||
// Calculate how much 'white space' is needed.
|
||||
int currentContentSize = [self contentSize:contentSize];
|
||||
int sizeOnePage = (_pageSize * _segmentSize);
|
||||
int canSomebodyPleaseRenameThisValueIfHeOrSheFindsOutHowToCallIt = ((int)[self contentSize:contentSize] % (_pageSize * _segmentSize));
|
||||
|
||||
CGFloat value = sizeOnePage - canSomebodyPleaseRenameThisValueIfHeOrSheFindsOutHowToCallIt + currentContentSize;
|
||||
contentSize = [self setCGSize:contentSize withValue:value];
|
||||
}
|
||||
|
||||
[self setContentSize:contentSize];
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
//
|
||||
// NSString+HexColor.h
|
||||
// Seating
|
||||
//
|
||||
// Created by Giuseppe Nucifora on 20/10/12.
|
||||
// Copyright (c) 2012 Meedori S.r.l. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
@interface NSString (HexColor)
|
||||
|
||||
- (UIColor*)colorFromHex;
|
||||
|
||||
@end
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
//
|
||||
// NSString+HexColor.m
|
||||
// Seating
|
||||
//
|
||||
// Created by Giuseppe Nucifora on 20/10/12.
|
||||
// Copyright (c) 2012 Meedori S.r.l. All rights reserved.
|
||||
//
|
||||
|
||||
#import "NSString+HexColor.h"
|
||||
|
||||
@implementation NSString (HexColor)
|
||||
|
||||
- (UIColor*)colorFromHex
|
||||
{
|
||||
NSString *hexColor = [[self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
|
||||
|
||||
if ([hexColor length] < 6)
|
||||
return [UIColor blackColor];
|
||||
if ([hexColor hasPrefix:@"#"])
|
||||
hexColor = [hexColor substringFromIndex:1];
|
||||
if ([hexColor length] != 6 && [hexColor length] != 8)
|
||||
return [UIColor blackColor];
|
||||
|
||||
NSRange range;
|
||||
range.location = 0;
|
||||
range.length = 2;
|
||||
|
||||
NSString *rString = [hexColor substringWithRange:range];
|
||||
|
||||
range.location = 2;
|
||||
NSString *gString = [hexColor substringWithRange:range];
|
||||
|
||||
range.location = 4;
|
||||
NSString *bString = [hexColor substringWithRange:range];
|
||||
|
||||
range.location = 6;
|
||||
NSString *aString = @"FF";
|
||||
if ([hexColor length] == 8)
|
||||
aString = [hexColor substringWithRange:range];
|
||||
|
||||
// Scan values
|
||||
unsigned int r, g, b, a;
|
||||
[[NSScanner scannerWithString:rString] scanHexInt:&r];
|
||||
[[NSScanner scannerWithString:gString] scanHexInt:&g];
|
||||
[[NSScanner scannerWithString:bString] scanHexInt:&b];
|
||||
[[NSScanner scannerWithString:aString] scanHexInt:&a];
|
||||
|
||||
return [UIColor colorWithRed:((float) r / 255.0f)
|
||||
green:((float) g / 255.0f)
|
||||
blue:((float) b / 255.0f)
|
||||
alpha:((float) a / 255.0f)];
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in new issue
Block a user