diff --git a/JSDropDownMenu/JSDropDownMenu.h b/JSDropDownMenu/JSDropDownMenu.h old mode 100644 new mode 100755 index 234d37b..d57d515 --- a/JSDropDownMenu/JSDropDownMenu.h +++ b/JSDropDownMenu/JSDropDownMenu.h @@ -13,13 +13,9 @@ @interface JSIndexPath : NSObject @property (nonatomic, assign) NSInteger column; -// 0 左边 1 右边 @property (nonatomic, assign) NSInteger leftOrRight; -// 左边行 @property (nonatomic, assign) NSInteger leftRow; -// 右边行 @property (nonatomic, assign) NSInteger row; - - (instancetype)initWithColumn:(NSInteger)column leftOrRight:(NSInteger)leftOrRight leftRow:(NSInteger)leftRow row:(NSInteger)row; + (instancetype)indexPathWithCol:(NSInteger)col leftOrRight:(NSInteger)leftOrRight leftRow:(NSInteger)leftRow row:(NSInteger)row; @@ -63,6 +59,9 @@ @protocol JSDropDownMenuDelegate @optional - (void)menu:(JSDropDownMenu *)menu didSelectRowAtIndexPath:(JSIndexPath *)indexPath; + +// 在菜单显示或隐藏时调用 (PS . 隐藏指再次点击相同menu时的隐藏,menu切换时不会触发隐藏,只会有是显示事件 +- (void)menu:(JSDropDownMenu *)menu didShowMenu:(BOOL)show; @end #pragma mark - interface diff --git a/JSDropDownMenu/JSDropDownMenu.m b/JSDropDownMenu/JSDropDownMenu.m old mode 100644 new mode 100755 index 3103f3e..7fccd69 --- a/JSDropDownMenu/JSDropDownMenu.m +++ b/JSDropDownMenu/JSDropDownMenu.m @@ -49,7 +49,7 @@ - (CGSize)textSizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBre @interface JSCollectionViewCell:UICollectionViewCell @property(nonatomic,strong) UILabel *textLabel; -@property(nonatomic,strong) UIImageView *accessoryView; +@property(nonatomic,strong) UIView *accessoryView; -(void)removeAccessoryView; @@ -180,6 +180,10 @@ @interface JSDropDownMenu () @property (nonatomic, strong) UITableView *leftTableView; @property (nonatomic, strong) UITableView *rightTableView; @property (nonatomic, strong) UICollectionView *collectionView; + +// 出现在table顶端分割栏 +@property (nonatomic, strong) UIView* topShadow; + //data source @property (nonatomic, copy) NSArray *array; //layers array @@ -268,6 +272,12 @@ - (void)setDataSource:(id)dataSource { _bottomShadow.backgroundColor = self.separatorColor; + CALayer *bottomLayer = [CALayer layer]; + bottomLayer.frame = CGRectMake(0, _topShadow.frame.size.height - 0.5, _topShadow.frame.size.width, 0.5f); + bottomLayer.backgroundColor = [self.separatorColor CGColor]; + [_topShadow.layer addSublayer:bottomLayer]; + [_topShadow.layer addSublayer:[self createTopShadowSeparatorLineWithColor:self.separatorColor andPosition:CGPointMake(_topShadow.frame.size.width / 2, _topShadow.frame.size.height / 2)]]; + _titles = [tempTitles copy]; _indicators = [tempIndicators copy]; _bgLayers = [tempBgLayers copy]; @@ -285,28 +295,31 @@ - (instancetype)initWithOrigin:(CGPoint)origin andHeight:(CGFloat)height { _hadSelected = NO; //tableView init - _leftTableView = [[UITableView alloc] initWithFrame:CGRectMake(origin.x, self.frame.origin.y + self.frame.size.height, 0, 0) style:UITableViewStyleGrouped]; + //fix: + _leftTableView = [[UITableView alloc] initWithFrame:CGRectMake(origin.x, origin.y, 0, 0) style:UITableViewStyleGrouped]; _leftTableView.rowHeight = 38; + _leftTableView.backgroundColor = [UIColor whiteColor]; _leftTableView.separatorColor = [UIColor colorWithRed:220.f/255.0f green:220.f/255.0f blue:220.f/255.0f alpha:1.0]; _leftTableView.dataSource = self; _leftTableView.delegate = self; - _rightTableView = [[UITableView alloc] initWithFrame:CGRectMake(self.frame.size.width, self.frame.origin.y + self.frame.size.height, 0, 0) style:UITableViewStyleGrouped]; + //fix: + _rightTableView = [[UITableView alloc] initWithFrame:CGRectMake(self.frame.size.width, origin.y, 0, 0) style:UITableViewStyleGrouped]; _rightTableView.rowHeight = 38; + _rightTableView.backgroundColor = BackColor; _rightTableView.separatorColor = [UIColor colorWithRed:220.f/255.0f green:220.f/255.0f blue:220.f/255.0f alpha:1.0]; _rightTableView.dataSource = self; _rightTableView.delegate = self; UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init]; flowLayout.minimumInteritemSpacing = 0; - _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(origin.x, self.frame.origin.y + self.frame.size.height, self.frame.size.width, 0) collectionViewLayout:flowLayout]; + _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(origin.x, origin.y, self.frame.size.width, 0) collectionViewLayout:flowLayout]; [_collectionView registerClass:[JSCollectionViewCell class] forCellWithReuseIdentifier:@"CollectionCell"]; _collectionView.backgroundColor = [UIColor colorWithRed:220.f/255.0f green:220.f/255.0f blue:220.f/255.0f alpha:1.0]; _collectionView.dataSource = self; _collectionView.delegate = self; - self.autoresizesSubviews = NO; _leftTableView.autoresizesSubviews = NO; _rightTableView.autoresizesSubviews = NO; @@ -325,8 +338,12 @@ - (instancetype)initWithOrigin:(CGPoint)origin andHeight:(CGFloat)height { [_backGroundView addGestureRecognizer:gesture]; //add bottom shadow - _bottomShadow = [[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height-0.5, screenSize.width, 0.5)]; + _bottomShadow = [[UIView alloc] initWithFrame:CGRectMake(0, -0.5, screenSize.width, 0.5)]; [self addSubview:_bottomShadow]; + + //add top shadow + _topShadow = [[UIView alloc] initWithFrame:CGRectMake(origin.x, origin.y, screenSize.width, 15)]; + _topShadow.backgroundColor = [UIColor whiteColor]; } return self; } @@ -346,9 +363,9 @@ - (CAShapeLayer *)createIndicatorWithColor:(UIColor *)color andPosition:(CGPoint CAShapeLayer *layer = [CAShapeLayer new]; UIBezierPath *path = [UIBezierPath new]; - [path moveToPoint:CGPointMake(0, 0)]; - [path addLineToPoint:CGPointMake(8, 0)]; - [path addLineToPoint:CGPointMake(4, 5)]; + [path moveToPoint:CGPointMake(0, 5)]; + [path addLineToPoint:CGPointMake(8, 5)]; + [path addLineToPoint:CGPointMake(4, 0)]; [path closePath]; layer.path = path.CGPath; @@ -365,6 +382,28 @@ - (CAShapeLayer *)createIndicatorWithColor:(UIColor *)color andPosition:(CGPoint return layer; } +- (CAShapeLayer *)createTopShadowSeparatorLineWithColor:(UIColor *)color andPosition:(CGPoint)point { + CAShapeLayer *layer = [CAShapeLayer new]; + + UIBezierPath *path = [UIBezierPath new]; + [path moveToPoint:CGPointMake(-20, 1)]; + [path addLineToPoint:CGPointMake(20, 1)]; + [path moveToPoint:CGPointMake(-20, 4)]; + [path addLineToPoint:CGPointMake(20, 4)]; + + layer.path = path.CGPath; + layer.lineWidth = 1.0; + layer.strokeColor = color.CGColor; + + CGPathRef bound = CGPathCreateCopyByStrokingPath(layer.path, nil, layer.lineWidth, kCGLineCapButt, kCGLineJoinMiter, layer.miterLimit); + layer.bounds = CGPathGetBoundingBox(bound); + + CGPathRelease(bound); + + layer.position = point; + return layer; +} + - (CAShapeLayer *)createSeparatorLineWithColor:(UIColor *)color andPosition:(CGPoint)point { CAShapeLayer *layer = [CAShapeLayer new]; @@ -479,14 +518,15 @@ - (void)menuTapped:(UITapGestureRecognizer *)paramSender { if (haveRightTableView) { rightTableView = _rightTableView; // 修改左右tableview显示比例 - } if (tapIndex == _currentSelectedMenudIndex && _show) { + // 关闭 [self animateIdicator:_indicators[_currentSelectedMenudIndex] background:_backGroundView leftTableView:_leftTableView rightTableView:_rightTableView title:_titles[_currentSelectedMenudIndex] forward:NO complecte:^{ _currentSelectedMenudIndex = tapIndex; _show = NO; + }]; [(CALayer *)self.bgLayers[tapIndex] setBackgroundColor:BackColor.CGColor]; @@ -506,29 +546,20 @@ - (void)menuTapped:(UITapGestureRecognizer *)paramSender { } [_leftTableView reloadData]; - CGFloat ratio = [_dataSource widthRatioOfLeftColumn:_currentSelectedMenudIndex]; - if (_leftTableView) { - - _leftTableView.frame = CGRectMake(_leftTableView.frame.origin.x, self.frame.origin.y + self.frame.size.height, self.frame.size.width*ratio, 0); - } - - if (_rightTableView) { - - _rightTableView.frame = CGRectMake(_origin.x+_leftTableView.frame.size.width, self.frame.origin.y + self.frame.size.height, self.frame.size.width*(1-ratio), 0); - } - if (_currentSelectedMenudIndex!=-1) { // 需要隐藏collectionview [self animateCollectionView:_collectionView show:NO complete:^{ - [self animateIdicator:_indicators[tapIndex] background:_backGroundView leftTableView:_leftTableView rightTableView:_rightTableView title:_titles[tapIndex] forward:YES complecte:^{ + [self animateIdicator:_indicators[tapIndex] background:_backGroundView leftTableView:_leftTableView rightTableView:rightTableView title:_titles[tapIndex] forward:YES complecte:^{ _show = YES; + }]; }]; } else{ - [self animateIdicator:_indicators[tapIndex] background:_backGroundView leftTableView:_leftTableView rightTableView:_rightTableView title:_titles[tapIndex] forward:YES complecte:^{ + [self animateIdicator:_indicators[tapIndex] background:_backGroundView leftTableView:_leftTableView rightTableView:rightTableView title:_titles[tapIndex] forward:YES complecte:^{ _show = YES; + }]; } [(CALayer *)self.bgLayers[tapIndex] setBackgroundColor:SelectColor.CGColor]; @@ -582,6 +613,11 @@ - (void)animateIndicator:(CAShapeLayer *)indicator Forward:(BOOL)forward complet - (void)animateBackGroundView:(UIView *)view show:(BOOL)show complete:(void(^)())complete { if (show) { + CGRect mainFrame = [[UIScreen mainScreen] bounds]; + CGRect viewFrame = view.frame; + viewFrame.origin.y = self.frame.origin.y - mainFrame.size.height + self.frame.size.height; + view.frame = viewFrame; + [self.superview addSubview:view]; [view.superview addSubview:self]; @@ -606,62 +642,92 @@ - (void)animateLeftTableView:(UITableView *)leftTableView rightTableView:(UITabl CGFloat ratio = [_dataSource widthRatioOfLeftColumn:_currentSelectedMenudIndex]; if (show) { + //fix: + CGRect selfFrame = self.frame; + CGFloat leftTableViewHeight = 0; + CGFloat leftTableViewY = self.frame.origin.y; CGFloat rightTableViewHeight = 0; + CGFloat rightTableViewY = self.frame.origin.y; + if (leftTableView) { + leftTableViewHeight = ([leftTableView numberOfRowsInSection:0] > 8) ? (8 * leftTableView.rowHeight) : ([leftTableView numberOfRowsInSection:0] * leftTableView.rowHeight); + } + + if (rightTableView) { - leftTableView.frame = CGRectMake(_origin.x, self.frame.origin.y + self.frame.size.height, self.frame.size.width*ratio, 0); - [self.superview addSubview:leftTableView]; + rightTableViewHeight = ([rightTableView numberOfRowsInSection:0] > 8) ? (8 * rightTableView.rowHeight) : ([rightTableView numberOfRowsInSection:0] * rightTableView.rowHeight); + } + + CGFloat tableViewHeight = MAX(leftTableViewHeight, rightTableViewHeight); + + if (leftTableView) { - leftTableViewHeight = ([leftTableView numberOfRowsInSection:0] > 5) ? (5 * leftTableView.rowHeight) : ([leftTableView numberOfRowsInSection:0] * leftTableView.rowHeight); - + CGRect leftFrame = leftTableView.frame; + + leftTableViewY -= tableViewHeight; + leftFrame.origin.x = selfFrame.origin.x; + leftFrame.origin.y = leftTableViewY; + leftFrame.size.height = tableViewHeight; + leftFrame.size.width = self.frame.size.width * ratio; + + leftTableView.frame = leftFrame; + [self.superview addSubview:leftTableView]; } if (rightTableView) { - rightTableView.frame = CGRectMake(_origin.x+leftTableView.frame.size.width, self.frame.origin.y + self.frame.size.height, self.frame.size.width*(1-ratio), 0); + CGRect rightFrame = rightTableView.frame; + rightTableViewY -= tableViewHeight; + rightFrame.origin.x = selfFrame.origin.x + leftTableView.frame.size.width; + rightFrame.origin.y = rightTableViewY; + rightFrame.size.height = tableViewHeight; + rightFrame.size.width = self.frame.size.width * ( 1- ratio); + + rightTableView.frame = rightFrame; [self.superview addSubview:rightTableView]; - - rightTableViewHeight = ([rightTableView numberOfRowsInSection:0] > 5) ? (5 * rightTableView.rowHeight) : ([rightTableView numberOfRowsInSection:0] * rightTableView.rowHeight); } - CGFloat tableViewHeight = MAX(leftTableViewHeight, rightTableViewHeight); + // top shadow + CGRect topShadowRect = _topShadow.frame; + topShadowRect.origin.y = self.frame.origin.y - tableViewHeight - topShadowRect.size.height; + _topShadow.frame = topShadowRect; + [self.superview addSubview:_topShadow]; + - [UIView animateWithDuration:0.2 animations:^{ - if (leftTableView) { - leftTableView.frame = CGRectMake(_origin.x, self.frame.origin.y + self.frame.size.height, self.frame.size.width*ratio, tableViewHeight); - } - if (rightTableView) { - rightTableView.frame = CGRectMake(_origin.x+leftTableView.frame.size.width, self.frame.origin.y + self.frame.size.height, self.frame.size.width*(1-ratio), tableViewHeight); - } - }]; } else { - [UIView animateWithDuration:0.2 animations:^{ - - if (leftTableView) { - leftTableView.frame = CGRectMake(_origin.x, self.frame.origin.y + self.frame.size.height, self.frame.size.width*ratio, 0); - } - if (rightTableView) { - rightTableView.frame = CGRectMake(_origin.x+leftTableView.frame.size.width, self.frame.origin.y + self.frame.size.height, self.frame.size.width*(1-ratio), 0); - } - - } completion:^(BOOL finished) { - - if (leftTableView) { - [leftTableView removeFromSuperview]; - } - if (rightTableView) { - [rightTableView removeFromSuperview]; - } - }]; + if (leftTableView) { + [leftTableView removeFromSuperview]; + } + if (rightTableView) { + [rightTableView removeFromSuperview]; + } + + [_topShadow removeFromSuperview]; + + } + if ([_delegate respondsToSelector:@selector(menu:didShowMenu:)]) { + [_delegate menu:self didShowMenu:show]; } complete(); } +- (void)animateViewHeight:(UIView*)animateView withAnimationType:(NSString*)animType { + CATransition *animation = [CATransition animation]; + [animation setType:kCATransitionPush]; + [animation setSubtype:animType]; + [animation setDuration:0.5]; + + [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]]; + [[animateView layer] addAnimation:animation forKey:kCATransition]; + animateView.hidden = !animateView.hidden; + +} + /** *动画显示下拉菜单 */ @@ -672,30 +738,24 @@ - (void)animateCollectionView:(UICollectionView *)collectionView show:(BOOL)show CGFloat collectionViewHeight = 0; if (collectionView) { + collectionViewHeight = ([collectionView numberOfItemsInSection:0] > 10) ? (5 * 38) : (ceil([collectionView numberOfItemsInSection:0]/2) * 38); - collectionView.frame = CGRectMake(_origin.x, self.frame.origin.y + self.frame.size.height, self.frame.size.width, 0); + collectionView.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y - collectionViewHeight, self.frame.size.width, collectionViewHeight); [self.superview addSubview:collectionView]; - - collectionViewHeight = ([collectionView numberOfItemsInSection:0] > 10) ? (5 * 38) : (ceil([collectionView numberOfItemsInSection:0]/2.0) * 38); } - [UIView animateWithDuration:0.2 animations:^{ - if (collectionView) { - collectionView.frame = CGRectMake(_origin.x, self.frame.origin.y + self.frame.size.height, self.frame.size.width, collectionViewHeight); - } - }]; + // top shadow + CGRect topShadowRect = _topShadow.frame; + topShadowRect.origin.y = self.frame.origin.y - collectionViewHeight - topShadowRect.size.height; + _topShadow.frame = topShadowRect; + [self.superview addSubview:_topShadow]; + } else { - [UIView animateWithDuration:0.2 animations:^{ - - if (collectionView) { - collectionView.frame = CGRectMake(_origin.x, self.frame.origin.y + self.frame.size.height, self.frame.size.width, 0); - } - } completion:^(BOOL finished) { - - if (collectionView) { - [collectionView removeFromSuperview]; - } - }]; + [collectionView removeFromSuperview]; + [_topShadow removeFromSuperview]; + } + if ([_delegate respondsToSelector:@selector(menu:didShowMenu:)]) { + [_delegate menu:self didShowMenu:show]; } complete(); } @@ -739,7 +799,7 @@ - (void)animateIdicator:(CAShapeLayer *)indicator background:(UIView *)backgroun #pragma mark - table datasource - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { - // 0 左边 1 右边 + NSInteger leftOrRight = 0; if (_rightTableView==tableView) { leftOrRight = 1; @@ -774,7 +834,7 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N UILabel *titleLabel = [[UILabel alloc] init]; titleLabel.textColor = self.textColor; titleLabel.tag = 1; - titleLabel.font = [UIFont systemFontOfSize:14.0]; + titleLabel.font = [UIFont systemFontOfSize:12.0]; [cell addSubview:titleLabel]; @@ -794,14 +854,10 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N titleLabel.text = [self.dataSource menu:self titleForRowAtIndexPath:[JSIndexPath indexPathWithCol:self.currentSelectedMenudIndex leftOrRight:leftOrRight leftRow:_leftSelectedRow row:indexPath.row]]; // 只取宽度 - textSize = [titleLabel.text textSizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:CGSizeMake(MAXFLOAT, 14) lineBreakMode:NSLineBreakByWordWrapping]; + textSize = [titleLabel.text textSizeWithFont:[UIFont systemFontOfSize:12.0f] constrainedToSize:CGSizeMake(MAXFLOAT, 14) lineBreakMode:NSLineBreakByWordWrapping]; } - cell.backgroundColor = [UIColor whiteColor]; - cell.textLabel.font = [UIFont systemFontOfSize:14.0]; - cell.separatorInset = UIEdgeInsetsZero; - if (leftOrRight == 1) { @@ -885,6 +941,21 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath } } +// 解决tableview分割线不顶格问题 +- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { + if ([cell respondsToSelector:@selector(setSeparatorInset:)]) { + [cell setSeparatorInset:UIEdgeInsetsZero]; + } + + if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) { + [cell setPreservesSuperviewLayoutMargins:NO]; + } + + if ([cell respondsToSelector:@selector(setLayoutMargins:)]) { + [cell setLayoutMargins:UIEdgeInsetsZero]; + } +} + - (void)confiMenuWithSelectRow:(NSInteger)row leftOrRight:(NSInteger)leftOrRight{ CATextLayer *title = (CATextLayer *)_titles[_currentSelectedMenudIndex]; title.string = [self.dataSource menu:self titleForRowAtIndexPath:[JSIndexPath indexPathWithCol:self.currentSelectedMenudIndex leftOrRight:leftOrRight leftRow:_leftSelectedRow row:row]]; diff --git a/JSDropDownMenu/ico_make.png b/JSDropDownMenu/ico_make.png old mode 100644 new mode 100755 diff --git a/JSDropDownMenuDemo/.DS_Store b/JSDropDownMenuDemo/.DS_Store new file mode 100644 index 0000000..c27f4ed Binary files /dev/null and b/JSDropDownMenuDemo/.DS_Store differ diff --git a/JSDropDownMenuDemo/JSDropDownMenuDemo.xcodeproj/project.pbxproj b/JSDropDownMenuDemo/JSDropDownMenuDemo.xcodeproj/project.pbxproj index c9dfaa9..9fe88a4 100644 --- a/JSDropDownMenuDemo/JSDropDownMenuDemo.xcodeproj/project.pbxproj +++ b/JSDropDownMenuDemo/JSDropDownMenuDemo.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + 836CBA231ACEC8F6003FADBE /* TableViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 836CBA221ACEC8F6003FADBE /* TableViewController.m */; }; D0EED8831A63CBC700EE7AFD /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = D0EED8821A63CBC700EE7AFD /* main.m */; }; D0EED8861A63CBC700EE7AFD /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = D0EED8851A63CBC700EE7AFD /* AppDelegate.m */; }; D0EED8891A63CBC700EE7AFD /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = D0EED8881A63CBC700EE7AFD /* ViewController.m */; }; @@ -29,6 +30,8 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ + 836CBA211ACEC8F6003FADBE /* TableViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TableViewController.h; sourceTree = ""; }; + 836CBA221ACEC8F6003FADBE /* TableViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TableViewController.m; sourceTree = ""; }; D0EED87D1A63CBC700EE7AFD /* JSDropDownMenuDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = JSDropDownMenuDemo.app; sourceTree = BUILT_PRODUCTS_DIR; }; D0EED8811A63CBC700EE7AFD /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; D0EED8821A63CBC700EE7AFD /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; @@ -95,6 +98,8 @@ D0EED88D1A63CBC700EE7AFD /* Images.xcassets */, D0EED88F1A63CBC700EE7AFD /* LaunchScreen.xib */, D0EED8801A63CBC700EE7AFD /* Supporting Files */, + 836CBA211ACEC8F6003FADBE /* TableViewController.h */, + 836CBA221ACEC8F6003FADBE /* TableViewController.m */, ); path = JSDropDownMenuDemo; sourceTree = ""; @@ -240,6 +245,7 @@ D0EED8891A63CBC700EE7AFD /* ViewController.m in Sources */, D0EED8861A63CBC700EE7AFD /* AppDelegate.m in Sources */, D0EED8A91A63CCB300EE7AFD /* JSDropDownMenu.m in Sources */, + 836CBA231ACEC8F6003FADBE /* TableViewController.m in Sources */, D0EED8831A63CBC700EE7AFD /* main.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; @@ -431,6 +437,7 @@ D0EED8A21A63CBC700EE7AFD /* Release */, ); defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; }; D0EED8A31A63CBC700EE7AFD /* Build configuration list for PBXNativeTarget "JSDropDownMenuDemoTests" */ = { isa = XCConfigurationList; @@ -439,6 +446,7 @@ D0EED8A51A63CBC700EE7AFD /* Release */, ); defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; }; /* End XCConfigurationList section */ }; diff --git a/JSDropDownMenuDemo/JSDropDownMenuDemo/Base.lproj/Main.storyboard b/JSDropDownMenuDemo/JSDropDownMenuDemo/Base.lproj/Main.storyboard index d912f9d..317ae3a 100644 --- a/JSDropDownMenuDemo/JSDropDownMenuDemo/Base.lproj/Main.storyboard +++ b/JSDropDownMenuDemo/JSDropDownMenuDemo/Base.lproj/Main.storyboard @@ -1,13 +1,13 @@ - + - + - + @@ -20,6 +20,41 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/JSDropDownMenuDemo/JSDropDownMenuDemo/TableViewController.h b/JSDropDownMenuDemo/JSDropDownMenuDemo/TableViewController.h new file mode 100644 index 0000000..8446b7a --- /dev/null +++ b/JSDropDownMenuDemo/JSDropDownMenuDemo/TableViewController.h @@ -0,0 +1,13 @@ +// +// TableViewController.h +// JSDropDownMenuDemo +// +// Created by HeQichang on 15/4/3. +// Copyright (c) 2015年 jsfu. All rights reserved. +// + +#import + +@interface TableViewController : UITableViewController + +@end diff --git a/JSDropDownMenuDemo/JSDropDownMenuDemo/TableViewController.m b/JSDropDownMenuDemo/JSDropDownMenuDemo/TableViewController.m new file mode 100644 index 0000000..367f242 --- /dev/null +++ b/JSDropDownMenuDemo/JSDropDownMenuDemo/TableViewController.m @@ -0,0 +1,243 @@ +// +// TableViewController.m +// JSDropDownMenuDemo +// +// Created by HeQichang on 15/4/3. +// Copyright (c) 2015年 jsfu. All rights reserved. +// + +#import "TableViewController.h" +#import "JSDropDownMenu.h" +@interface TableViewController (){ + NSMutableArray *_mArray; + NSMutableArray *_data1; + NSMutableArray *_data2; + NSMutableArray *_data3; + + NSInteger _currentData1Index; + NSInteger _currentData2Index; + NSInteger _currentData3Index; + + NSInteger _currentData1SelectedIndex; + CGFloat prePosistionY; + JSDropDownMenu *menu; +} + +@end + +@implementation TableViewController + +- (void)viewDidLoad { + [super viewDidLoad]; + + _mArray = [[NSMutableArray alloc] init]; + for (int i = 0; i < 25; i++) { + [_mArray addObject:[self createRandomStr]]; + } + + // 指定默认选中 + _currentData1Index = 1; + _currentData1SelectedIndex = 1; + + NSArray *food = @[@"全部美食", @"火锅", @"川菜", @"西餐", @"自助餐"]; + NSArray *travel = @[@"全部旅游", @"周边游", @"景点门票", @"国内游", @"境外游"]; + + _data1 = [NSMutableArray arrayWithObjects:@{@"title":@"美食", @"data":food}, @{@"title":@"旅游", @"data":travel}, nil]; + _data2 = [NSMutableArray arrayWithObjects:@"智能排序", @"离我最近", @"评价最高", @"最新发布", @"人气最高", @"价格最低", @"价格最高", nil]; + _data3 = [NSMutableArray arrayWithObjects:@"不限人数", @"单人餐", @"双人餐", @"3~4人餐", nil]; + + CGRect mainRect = self.tableView.frame; + menu = [[JSDropDownMenu alloc] initWithOrigin:CGPointMake(0, mainRect.size.height - 45) andHeight:45]; + menu.indicatorColor = [UIColor colorWithRed:175.0f/255.0f green:175.0f/255.0f blue:175.0f/255.0f alpha:1.0]; + menu.separatorColor = [UIColor colorWithRed:210.0f/255.0f green:210.0f/255.0f blue:210.0f/255.0f alpha:1.0]; + menu.textColor = [UIColor colorWithRed:83.f/255.0f green:83.f/255.0f blue:83.f/255.0f alpha:1.0f]; + menu.dataSource = self; + menu.delegate = self; + + [self.view addSubview:menu]; + +} + +- (NSString *)createRandomStr { + NSString *str = @"abcdefghijklmnopqrstuvwxyz"; + NSInteger r = 1 + random()%15; + NSString *title = [str substringToIndex:r]; + return title; +} + +- (void)didReceiveMemoryWarning { + [super didReceiveMemoryWarning]; + // Dispose of any resources that can be recreated. +} + +#pragma mark - Table view data source + +- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { + return 1; +} + +- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { + return [_mArray count]; +} + +- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { + UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath]; + cell.textLabel.text = _mArray[indexPath.row]; + return cell; +} + +- (void)scrollViewDidScroll:(UIScrollView *)scrollView { + + CGFloat currentY = scrollView.contentOffset.y; + + + if (scrollView.isDragging && currentY > prePosistionY && currentY > 0) { + menu.hidden = YES; + } else if (scrollView.isDragging) { + menu.hidden = NO; + } + NSLog(@"%lf - %lf", prePosistionY, currentY); + CGRect menuRect = menu.frame; + menuRect.origin.y = menuRect.origin.y + (currentY - prePosistionY); + menu.frame = menuRect; + prePosistionY = currentY; +} + +#pragma mark - JSDropDownMenuDataSource & JSDropDownMenuDelegate + +- (NSInteger)numberOfColumnsInMenu:(JSDropDownMenu *)menu { + + return 3; +} + +-(BOOL)displayByCollectionViewInColumn:(NSInteger)column{ + + if (column==2) { + + return YES; + } + + return NO; +} + +-(BOOL)haveRightTableViewInColumn:(NSInteger)column{ + + if (column==0) { + return YES; + } + return NO; +} + +-(CGFloat)widthRatioOfLeftColumn:(NSInteger)column{ + + if (column==0) { + return 0.5; + } + + return 1; +} + +-(NSInteger)currentLeftSelectedRow:(NSInteger)column{ + + if (column==0) { + + return _currentData1Index; + + } + if (column==1) { + + return _currentData2Index; + } + + return 0; +} + +- (NSInteger)menu:(JSDropDownMenu *)menu numberOfRowsInColumn:(NSInteger)column leftOrRight:(NSInteger)leftOrRight leftRow:(NSInteger)leftRow{ + + if (column==0) { + if (leftOrRight==0) { + + return _data1.count; + } else{ + + NSDictionary *menuDic = [_data1 objectAtIndex:leftRow]; + return [[menuDic objectForKey:@"data"] count]; + } + } else if (column==1){ + + return _data2.count; + + } else if (column==2){ + + return _data3.count; + } + + return 0; +} + +- (NSString *)menu:(JSDropDownMenu *)menu titleForColumn:(NSInteger)column{ + + switch (column) { + case 0: return [[_data1[_currentData1Index] objectForKey:@"data"] objectAtIndex:_currentData1SelectedIndex]; + break; + case 1: return _data2[_currentData2Index]; + break; + case 2: return _data3[_currentData3Index]; + break; + default: + return nil; + break; + } +} + +- (NSString *)menu:(JSDropDownMenu *)menu titleForRowAtIndexPath:(JSIndexPath *)indexPath { + + if (indexPath.column==0) { + if (indexPath.leftOrRight==0) { + NSDictionary *menuDic = [_data1 objectAtIndex:indexPath.row]; + return [menuDic objectForKey:@"title"]; + } else{ + NSInteger leftRow = indexPath.leftRow; + NSDictionary *menuDic = [_data1 objectAtIndex:leftRow]; + return [[menuDic objectForKey:@"data"] objectAtIndex:indexPath.row]; + } + } else if (indexPath.column==1) { + + return _data2[indexPath.row]; + + } else { + + return _data3[indexPath.row]; + } +} + +- (void)menu:(JSDropDownMenu *)menu didSelectRowAtIndexPath:(JSIndexPath *)indexPath { + + if (indexPath.column == 0) { + + if(indexPath.leftOrRight==0){ + + _currentData1Index = indexPath.row; + + return; + } + + } else if(indexPath.column == 1){ + + _currentData2Index = indexPath.row; + + } else{ + + _currentData3Index = indexPath.row; + } +} + +- (void)menu:(JSDropDownMenu *)menu didShowMenu:(BOOL)show { + if (show) { + self.tableView.scrollEnabled = NO; + } else { + self.tableView.scrollEnabled = YES; + } +} + +@end diff --git a/README.md b/README.md index 6e1bc45..86de3c5 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,6 @@ # JSDropDownMenu -类似美团的下拉菜单 +修改为上拉菜单 -![image](https://github.com/jsfu/JSDropDownMenu/raw/master/ScreenShots/2015.1.19.5.31.29.png) +![image](https://raw.githubusercontent.com/heqichang/JSDropDownMenu/master/ScreenShots/2015-4-3%209.26.58%20pm.png) -![image](https://github.com/jsfu/JSDropDownMenu/raw/master/ScreenShots/2015.1.27.2.00.03.png) - -![image](https://github.com/jsfu/JSDropDownMenu/raw/master/ScreenShots/2015.1.27.2.00.06.png) \ No newline at end of file +![image](https://raw.githubusercontent.com/heqichang/JSDropDownMenu/master/ScreenShots/2015-4-3%209.27.09%20pm.png) diff --git a/ScreenShots/.DS_Store b/ScreenShots/.DS_Store new file mode 100644 index 0000000..5008ddf Binary files /dev/null and b/ScreenShots/.DS_Store differ diff --git a/ScreenShots/2015-4-3 9.26.58 pm.png b/ScreenShots/2015-4-3 9.26.58 pm.png new file mode 100644 index 0000000..9cd127c Binary files /dev/null and b/ScreenShots/2015-4-3 9.26.58 pm.png differ diff --git a/ScreenShots/2015-4-3 9.27.09 pm.png b/ScreenShots/2015-4-3 9.27.09 pm.png new file mode 100644 index 0000000..cc50b80 Binary files /dev/null and b/ScreenShots/2015-4-3 9.27.09 pm.png differ diff --git a/ScreenShots/2015.1.19.5.31.29.png b/ScreenShots/2015.1.19.5.31.29.png deleted file mode 100644 index 7bc3489..0000000 Binary files a/ScreenShots/2015.1.19.5.31.29.png and /dev/null differ diff --git a/ScreenShots/2015.1.27.2.00.03.png b/ScreenShots/2015.1.27.2.00.03.png deleted file mode 100644 index cfbd20d..0000000 Binary files a/ScreenShots/2015.1.27.2.00.03.png and /dev/null differ diff --git a/ScreenShots/2015.1.27.2.00.06.png b/ScreenShots/2015.1.27.2.00.06.png deleted file mode 100644 index 9da4e9e..0000000 Binary files a/ScreenShots/2015.1.27.2.00.06.png and /dev/null differ