├── .gitignore ├── README.md └── XDSDropDownMenuDEMO ├── XDSDropDownMenu ├── XDSDropDownMenu.h └── XDSDropDownMenu.m ├── XDSDropDownMenuDEMO.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ ├── contents.xcworkspacedata │ └── xcuserdata │ │ └── cdj.xcuserdatad │ │ └── UserInterfaceState.xcuserstate └── xcuserdata │ └── cdj.xcuserdatad │ └── xcschemes │ └── xcschememanagement.plist └── XDSDropDownMenuDEMO ├── Assets.xcassets └── AppIcon.appiconset │ └── Contents.json ├── Info.plist ├── XDSDropDownMenu ├── XDSDropDownMenu.h └── XDSDropDownMenu.m ├── XDSStartViewController.h ├── XDSStartViewController.m ├── XDSStartViewController.storyboard ├── XDSTableViewController ├── XDSTableViewController.h ├── XDSTableViewController.m └── XDSTableViewController.storyboard ├── XDSViewController ├── XDSViewController.h ├── XDSViewController.m └── XDSViewController.xib ├── main.m └── other ├── AppDelegate.h ├── AppDelegate.m └── Base.lproj ├── LaunchScreen.storyboard └── Main.storyboard /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## Build generated 6 | build/ 7 | DerivedData/ 8 | 9 | ## Various settings 10 | *.pbxuser 11 | !default.pbxuser 12 | *.mode1v3 13 | !default.mode1v3 14 | *.mode2v3 15 | !default.mode2v3 16 | *.perspectivev3 17 | !default.perspectivev3 18 | xcuserdata/ 19 | 20 | ## Other 21 | *.moved-aside 22 | *.xccheckout 23 | *.xcscmblueprint 24 | 25 | ## Obj-C/Swift specific 26 | *.hmap 27 | *.ipa 28 | *.dSYM.zip 29 | *.dSYM 30 | 31 | # CocoaPods 32 | # 33 | # We recommend against adding the Pods directory to your .gitignore. However 34 | # you should judge for yourself, the pros and cons are mentioned at: 35 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 36 | # 37 | # Pods/ 38 | 39 | # Carthage 40 | # 41 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 42 | # Carthage/Checkouts 43 | 44 | Carthage/Build 45 | 46 | # fastlane 47 | # 48 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 49 | # screenshots whenever they are needed. 50 | # For more information about the recommended setup visit: 51 | # https://docs.fastlane.tools/best-practices/source-control/#source-control 52 | 53 | fastlane/report.xml 54 | fastlane/Preview.html 55 | fastlane/screenshots/**/*.png 56 | fastlane/test_output 57 | 58 | # Code Injection 59 | # 60 | # After new code Injection tools there's a generated folder /iOSInjectionProject 61 | # https://github.com/johnno1962/injectionforxcode 62 | 63 | iOSInjectionProject/ 64 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DropDownMenu-SelectMenu 2 | iOS封装下拉选择菜单 3 | 4 | 介绍和使用方法请参考简书链接:https://www.jianshu.com/p/29f444b220bc 5 | 6 | 7 | 8 | 9 | 1. 说一下实现思路: 10 | 11 | (1)下拉菜单使用tableview实现; 12 | 13 | (2)点击选择按钮后,根据按钮在self.view的frame来确定菜单要显示或隐藏的位置,并使用菜单的tag值来确定是关闭还是打开。对于frame的确定,由于在UITableView中,按钮是加在cell的contentview上的,所以在UITableView中,要使用 [button.superview convertRect:button.frame toView:self.view] 方法来获得按钮在self.view上的frame。 14 | 15 | (3)在点击按钮后,要隐藏其它的下拉选择菜单。 16 | 17 | (4)打开和隐藏菜单都是通过动画来实现的,代码如下。 18 | 19 | [UIView beginAnimations:nil context:nil]; 20 | 21 | 。。。。 22 | 23 | [UIView commitAnimations]; 24 | 25 | (5)在点击了菜单的选项后,隐藏这个菜单(这一步通过代理实现),并将文字显示到按钮上。 26 | 27 | 2. 使用方法 28 | 29 | Demo里面有一个XDSDropDownMenu文件夹,直接拖入到你的项目中。 30 | 31 | 然后#import "XDSDropDownMenu.h" 32 | 33 | 至于后面怎么使用,可以去看我的Demo,代码的注释写的很详细。里面分了在UIView和UITballeView中的使用方法,其实,在两个中的使用大多代码是一样的,只是获取按钮在self.view的坐标不一样。 34 | 35 | 如果你的按钮的frame使用的不太对的话,会造成一些“特效”——菜单消失的位置会有变化,其实也挺好玩的,想试一下的同学可以试一下不同的frame。 36 | 37 | 参考的GitHub的Demo:NIDropDown(但是这个用起来会有bug) 38 | -------------------------------------------------------------------------------- /XDSDropDownMenuDEMO/XDSDropDownMenu/XDSDropDownMenu.h: -------------------------------------------------------------------------------- 1 | // 2 | // XDSDropDownMenu.h 3 | // shts_ios_xds 4 | // 5 | // Created by cdj on 2018/5/8. 6 | // Copyright © 2018年 itiis. All rights reserved. 7 | // 8 | 9 | #pragma mark - 选择菜单封装类 10 | 11 | #import 12 | 13 | @class XDSDropDownMenu; 14 | @protocol XDSDropDownMenuDelegate 15 | @required 16 | - (void)setDropDownDelegate:(XDSDropDownMenu *)sender;//设置下拉菜单的tag值 17 | @end 18 | 19 | @interface XDSDropDownMenu : UIView 20 | 21 | @property (nonatomic, retain) id delegate;//代理 22 | @property (nonatomic, retain) NSString *animationDirection;//动画方向 23 | @property (nonatomic, strong) UIImageView *imageView;//图片视图 24 | 25 | //隐藏选择菜单 26 | - (void)hideDropDownMenuWithBtnFrame:(CGRect)btnFrame; 27 | 28 | 29 | //返回选项的下标 30 | + (NSInteger)returnIndexByString:(NSString *)string fromArray:(NSArray *)array; 31 | 32 | //显示选择菜单 33 | /* 34 | button 选择按钮 35 | buttonFrame 选择按钮在self.view的frame 36 | titleArr 选择菜单的文本数组 37 | imageArr 选择菜单的图片数组 38 | direction 选择菜单的方向:down 或者 up 39 | */ 40 | - (void)showDropDownMenu:(UIButton *)button withButtonFrame:(CGRect)buttonFrame arrayOfTitle:(NSArray *)titleArr arrayOfImage:(NSArray *)imageArr animationDirection:(NSString *)direction; 41 | @end 42 | -------------------------------------------------------------------------------- /XDSDropDownMenuDEMO/XDSDropDownMenu/XDSDropDownMenu.m: -------------------------------------------------------------------------------- 1 | // 2 | // XDSDropDownMenu.m 3 | // shts_ios_xds 4 | // 5 | // Created by cdj on 2018/5/8. 6 | // Copyright © 2018年 itiis. All rights reserved. 7 | // 8 | 9 | #import "XDSDropDownMenu.h" 10 | 11 | @interface XDSDropDownMenu() 12 | @property(nonatomic, strong) UITableView *menuTableView; 13 | @property(nonatomic, strong) UIButton *btnSender; 14 | @property(nonatomic) CGRect buttonFrame; 15 | @property(nonatomic, retain) NSArray *titleList; 16 | @property(nonatomic, retain) NSArray *imageList; 17 | @end 18 | 19 | @implementation XDSDropDownMenu 20 | 21 | +(NSInteger)returnIndexByString:(NSString *)string fromArray:(NSArray *)array{ //返回选项下标 22 | NSUInteger index = [array indexOfObject:string]; 23 | return index; 24 | } 25 | 26 | - (void)showDropDownMenu:(UIButton *)button withButtonFrame:(CGRect)buttonFrame arrayOfTitle:(NSArray *)titleArr arrayOfImage:(NSArray *)imageArr animationDirection:(NSString *)direction{ 27 | 28 | self.backgroundColor = [UIColor whiteColor]; 29 | self.animationDirection = direction; 30 | self.btnSender = button; 31 | self.menuTableView = (UITableView *)[super init]; 32 | 33 | self.buttonFrame = buttonFrame; 34 | if (self) { 35 | CGRect btnRect = buttonFrame;//按钮在视图上的位置 36 | CGFloat height = 0;//菜单高度 37 | if ( titleArr.count <= 4) { 38 | height = titleArr.count *40; 39 | }else{ 40 | height = 200; 41 | } 42 | 43 | self.titleList = [NSArray arrayWithArray:titleArr]; 44 | self.imageList = [NSArray arrayWithArray:imageArr]; 45 | 46 | //菜单视图的起始大小和位置 47 | if ([direction isEqualToString:@"up"]) { 48 | self.frame = CGRectMake(btnRect.origin.x, btnRect.origin.y-2, btnRect.size.width, 0); 49 | }else if ([direction isEqualToString:@"down"]) { 50 | self.frame = CGRectMake(btnRect.origin.x, btnRect.origin.y+btnRect.size.height+2, btnRect.size.width, 0); 51 | } 52 | 53 | self.layer.masksToBounds = NO; 54 | self.layer.cornerRadius = 8; 55 | self.layer.shadowRadius = 5; 56 | self.layer.shadowOpacity = 0.5; 57 | 58 | self.menuTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, btnRect.size.width, 0)]; 59 | self.menuTableView.delegate = self; 60 | self.menuTableView.dataSource = self; 61 | self.menuTableView.layer.cornerRadius = 5; 62 | self.menuTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine; 63 | self.menuTableView.separatorColor = [UIColor grayColor]; 64 | self.menuTableView.separatorInset = UIEdgeInsetsMake(0, -20, 0, 0); 65 | self.menuTableView.backgroundColor = [UIColor whiteColor]; 66 | self.menuTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, btnRect.size.width, 0.001)];//最后无分割线 67 | [self.menuTableView flashScrollIndicators];//显示滚动条 68 | 69 | [UIView beginAnimations:nil context:nil];//动画 70 | [UIView setAnimationDuration:0.5]; 71 | //菜单视图的最终大小和位置 72 | if ([direction isEqualToString:@"up"]) { 73 | self.frame = CGRectMake(btnRect.origin.x, btnRect.origin.y-height-2, btnRect.size.width, height); 74 | } else if([direction isEqualToString:@"down"]) { 75 | self.frame = CGRectMake(btnRect.origin.x, btnRect.origin.y+btnRect.size.height+2, btnRect.size.width, height); 76 | } 77 | self.menuTableView.frame = CGRectMake(0, 0, btnRect.size.width, height); 78 | [UIView commitAnimations]; 79 | [self addSubview:self.menuTableView]; 80 | } 81 | 82 | } 83 | 84 | 85 | -(void)hideDropDownMenuWithBtnFrame:(CGRect)btnFrame { 86 | 87 | // CGRect btn = button.frame; 88 | [UIView beginAnimations:nil context:nil]; 89 | [UIView setAnimationDuration:0.3]; 90 | if ([self.animationDirection isEqualToString:@"up"]) { 91 | self.frame = CGRectMake(btnFrame.origin.x, btnFrame.origin.y-2, btnFrame.size.width, 0); 92 | }else if ([self.animationDirection isEqualToString:@"down"]) { 93 | self.frame = CGRectMake(btnFrame.origin.x, btnFrame.origin.y+btnFrame.size.height+2, btnFrame.size.width, 0); 94 | } 95 | self.menuTableView.frame = CGRectMake(0, 0, btnFrame.size.width, 0); 96 | [UIView commitAnimations]; 97 | } 98 | 99 | 100 | - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 101 | return 1; 102 | } 103 | 104 | -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 105 | return 40; 106 | } 107 | 108 | - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 109 | return [self.titleList count]; 110 | } 111 | - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 112 | static NSString *CellIdentifier = @"Cell"; 113 | 114 | UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 115 | if (cell == nil) { 116 | cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 117 | cell.textLabel.font = [UIFont systemFontOfSize:15]; 118 | cell.textLabel.textAlignment = NSTextAlignmentCenter; 119 | } 120 | if ([self.imageList count] == [self.titleList count]) { 121 | cell.textLabel.text =[self.titleList objectAtIndex:indexPath.row]; 122 | cell.imageView.image = [self.imageList objectAtIndex:indexPath.row]; 123 | } else if ([self.imageList count] > [self.titleList count]) { 124 | cell.textLabel.text =[self.titleList objectAtIndex:indexPath.row]; 125 | if (indexPath.row < [self.imageList count]) { 126 | cell.imageView.image = [self.imageList objectAtIndex:indexPath.row]; 127 | } 128 | } else if ([self.imageList count] < [self.titleList count]) { 129 | cell.textLabel.text =[self.titleList objectAtIndex:indexPath.row]; 130 | if (indexPath.row < [self.imageList count]) { 131 | cell.imageView.image = [self.imageList objectAtIndex:indexPath.row]; 132 | } 133 | } 134 | 135 | cell.textLabel.textColor = [UIColor blackColor]; 136 | UIView * v = [[UIView alloc] init]; 137 | v.backgroundColor = [UIColor grayColor]; 138 | cell.selectedBackgroundView = v; 139 | 140 | return cell; 141 | } 142 | - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 143 | [self hideDropDownMenuWithBtnFrame:self.buttonFrame]; 144 | 145 | UITableViewCell *c = [tableView cellForRowAtIndexPath:indexPath]; 146 | [self.btnSender setTitle:c.textLabel.text forState:UIControlStateNormal]; 147 | 148 | for (UIView *subview in self.btnSender.subviews) { 149 | if ([subview isKindOfClass:[UIImageView class]]) { 150 | [subview removeFromSuperview]; 151 | } 152 | } 153 | self.imageView.image = c.imageView.image; 154 | self.imageView = [[UIImageView alloc] initWithImage:c.imageView.image]; 155 | self.imageView.frame = CGRectMake(5, 5, 25, 25); 156 | [self.btnSender addSubview:self.imageView]; 157 | 158 | [self myDelegate]; 159 | } 160 | - (void) myDelegate { 161 | [self.delegate setDropDownDelegate:self]; 162 | } 163 | -(void)dealloc { 164 | 165 | } 166 | @end 167 | -------------------------------------------------------------------------------- /XDSDropDownMenuDEMO/XDSDropDownMenuDEMO.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 48; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 4629E6E120CB8D2900B0E223 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 4629E6E020CB8D2900B0E223 /* AppDelegate.m */; }; 11 | 4629E6E720CB8D2900B0E223 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 4629E6E520CB8D2900B0E223 /* Main.storyboard */; }; 12 | 4629E6E920CB8D2900B0E223 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 4629E6E820CB8D2900B0E223 /* Assets.xcassets */; }; 13 | 4629E6EC20CB8D2900B0E223 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 4629E6EA20CB8D2900B0E223 /* LaunchScreen.storyboard */; }; 14 | 4629E6EF20CB8D2900B0E223 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 4629E6EE20CB8D2900B0E223 /* main.m */; }; 15 | 4629E6F820CB8D4B00B0E223 /* XDSTableViewController.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 4629E6F620CB8D4B00B0E223 /* XDSTableViewController.storyboard */; }; 16 | 4629E6F920CB8D4B00B0E223 /* XDSTableViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 4629E6F720CB8D4B00B0E223 /* XDSTableViewController.m */; }; 17 | 4629E6FD20CB8D5100B0E223 /* XDSStartViewController.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 4629E6FB20CB8D5100B0E223 /* XDSStartViewController.storyboard */; }; 18 | 4629E6FE20CB8D5100B0E223 /* XDSStartViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 4629E6FC20CB8D5100B0E223 /* XDSStartViewController.m */; }; 19 | 4629E70620CB8D5700B0E223 /* XDSDropDownMenu.m in Sources */ = {isa = PBXBuildFile; fileRef = 4629E70120CB8D5700B0E223 /* XDSDropDownMenu.m */; }; 20 | 4629E70720CB8D5700B0E223 /* XDSViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 4629E70420CB8D5700B0E223 /* XDSViewController.m */; }; 21 | 4629E70820CB8D5700B0E223 /* XDSViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 4629E70520CB8D5700B0E223 /* XDSViewController.xib */; }; 22 | /* End PBXBuildFile section */ 23 | 24 | /* Begin PBXFileReference section */ 25 | 4629E6DC20CB8D2900B0E223 /* XDSDropDownMenuDEMO.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = XDSDropDownMenuDEMO.app; sourceTree = BUILT_PRODUCTS_DIR; }; 26 | 4629E6DF20CB8D2900B0E223 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 27 | 4629E6E020CB8D2900B0E223 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; 28 | 4629E6E620CB8D2900B0E223 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 29 | 4629E6E820CB8D2900B0E223 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 30 | 4629E6EB20CB8D2900B0E223 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 31 | 4629E6ED20CB8D2900B0E223 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 32 | 4629E6EE20CB8D2900B0E223 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 33 | 4629E6F520CB8D4B00B0E223 /* XDSTableViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = XDSTableViewController.h; sourceTree = ""; }; 34 | 4629E6F620CB8D4B00B0E223 /* XDSTableViewController.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = XDSTableViewController.storyboard; sourceTree = ""; }; 35 | 4629E6F720CB8D4B00B0E223 /* XDSTableViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = XDSTableViewController.m; sourceTree = ""; }; 36 | 4629E6FA20CB8D5100B0E223 /* XDSStartViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = XDSStartViewController.h; sourceTree = ""; }; 37 | 4629E6FB20CB8D5100B0E223 /* XDSStartViewController.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = XDSStartViewController.storyboard; sourceTree = ""; }; 38 | 4629E6FC20CB8D5100B0E223 /* XDSStartViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = XDSStartViewController.m; sourceTree = ""; }; 39 | 4629E70020CB8D5700B0E223 /* XDSDropDownMenu.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = XDSDropDownMenu.h; sourceTree = ""; }; 40 | 4629E70120CB8D5700B0E223 /* XDSDropDownMenu.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = XDSDropDownMenu.m; sourceTree = ""; }; 41 | 4629E70320CB8D5700B0E223 /* XDSViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = XDSViewController.h; sourceTree = ""; }; 42 | 4629E70420CB8D5700B0E223 /* XDSViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = XDSViewController.m; sourceTree = ""; }; 43 | 4629E70520CB8D5700B0E223 /* XDSViewController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = XDSViewController.xib; sourceTree = ""; }; 44 | /* End PBXFileReference section */ 45 | 46 | /* Begin PBXFrameworksBuildPhase section */ 47 | 4629E6D920CB8D2900B0E223 /* Frameworks */ = { 48 | isa = PBXFrameworksBuildPhase; 49 | buildActionMask = 2147483647; 50 | files = ( 51 | ); 52 | runOnlyForDeploymentPostprocessing = 0; 53 | }; 54 | /* End PBXFrameworksBuildPhase section */ 55 | 56 | /* Begin PBXGroup section */ 57 | 4629E6D320CB8D2900B0E223 = { 58 | isa = PBXGroup; 59 | children = ( 60 | 4629E6DE20CB8D2900B0E223 /* XDSDropDownMenuDEMO */, 61 | 4629E6DD20CB8D2900B0E223 /* Products */, 62 | ); 63 | sourceTree = ""; 64 | }; 65 | 4629E6DD20CB8D2900B0E223 /* Products */ = { 66 | isa = PBXGroup; 67 | children = ( 68 | 4629E6DC20CB8D2900B0E223 /* XDSDropDownMenuDEMO.app */, 69 | ); 70 | name = Products; 71 | sourceTree = ""; 72 | }; 73 | 4629E6DE20CB8D2900B0E223 /* XDSDropDownMenuDEMO */ = { 74 | isa = PBXGroup; 75 | children = ( 76 | 4629E6FA20CB8D5100B0E223 /* XDSStartViewController.h */, 77 | 4629E6FC20CB8D5100B0E223 /* XDSStartViewController.m */, 78 | 4629E6FB20CB8D5100B0E223 /* XDSStartViewController.storyboard */, 79 | 4629E70A20CB8DA300B0E223 /* XDSTableViewController */, 80 | 4629E70220CB8D5700B0E223 /* XDSViewController */, 81 | 4629E6FF20CB8D5700B0E223 /* XDSDropDownMenu */, 82 | 4629E70920CB8D7900B0E223 /* other */, 83 | 4629E6E820CB8D2900B0E223 /* Assets.xcassets */, 84 | 4629E6ED20CB8D2900B0E223 /* Info.plist */, 85 | 4629E6EE20CB8D2900B0E223 /* main.m */, 86 | ); 87 | path = XDSDropDownMenuDEMO; 88 | sourceTree = ""; 89 | }; 90 | 4629E6FF20CB8D5700B0E223 /* XDSDropDownMenu */ = { 91 | isa = PBXGroup; 92 | children = ( 93 | 4629E70020CB8D5700B0E223 /* XDSDropDownMenu.h */, 94 | 4629E70120CB8D5700B0E223 /* XDSDropDownMenu.m */, 95 | ); 96 | path = XDSDropDownMenu; 97 | sourceTree = ""; 98 | }; 99 | 4629E70220CB8D5700B0E223 /* XDSViewController */ = { 100 | isa = PBXGroup; 101 | children = ( 102 | 4629E70320CB8D5700B0E223 /* XDSViewController.h */, 103 | 4629E70420CB8D5700B0E223 /* XDSViewController.m */, 104 | 4629E70520CB8D5700B0E223 /* XDSViewController.xib */, 105 | ); 106 | path = XDSViewController; 107 | sourceTree = ""; 108 | }; 109 | 4629E70920CB8D7900B0E223 /* other */ = { 110 | isa = PBXGroup; 111 | children = ( 112 | 4629E6DF20CB8D2900B0E223 /* AppDelegate.h */, 113 | 4629E6E020CB8D2900B0E223 /* AppDelegate.m */, 114 | 4629E6E520CB8D2900B0E223 /* Main.storyboard */, 115 | 4629E6EA20CB8D2900B0E223 /* LaunchScreen.storyboard */, 116 | ); 117 | path = other; 118 | sourceTree = ""; 119 | }; 120 | 4629E70A20CB8DA300B0E223 /* XDSTableViewController */ = { 121 | isa = PBXGroup; 122 | children = ( 123 | 4629E6F520CB8D4B00B0E223 /* XDSTableViewController.h */, 124 | 4629E6F720CB8D4B00B0E223 /* XDSTableViewController.m */, 125 | 4629E6F620CB8D4B00B0E223 /* XDSTableViewController.storyboard */, 126 | ); 127 | path = XDSTableViewController; 128 | sourceTree = ""; 129 | }; 130 | /* End PBXGroup section */ 131 | 132 | /* Begin PBXNativeTarget section */ 133 | 4629E6DB20CB8D2900B0E223 /* XDSDropDownMenuDEMO */ = { 134 | isa = PBXNativeTarget; 135 | buildConfigurationList = 4629E6F220CB8D2900B0E223 /* Build configuration list for PBXNativeTarget "XDSDropDownMenuDEMO" */; 136 | buildPhases = ( 137 | 4629E6D820CB8D2900B0E223 /* Sources */, 138 | 4629E6D920CB8D2900B0E223 /* Frameworks */, 139 | 4629E6DA20CB8D2900B0E223 /* Resources */, 140 | ); 141 | buildRules = ( 142 | ); 143 | dependencies = ( 144 | ); 145 | name = XDSDropDownMenuDEMO; 146 | productName = XDSDropDownMenuDEMO; 147 | productReference = 4629E6DC20CB8D2900B0E223 /* XDSDropDownMenuDEMO.app */; 148 | productType = "com.apple.product-type.application"; 149 | }; 150 | /* End PBXNativeTarget section */ 151 | 152 | /* Begin PBXProject section */ 153 | 4629E6D420CB8D2900B0E223 /* Project object */ = { 154 | isa = PBXProject; 155 | attributes = { 156 | LastUpgradeCheck = 0920; 157 | ORGANIZATIONNAME = itiis; 158 | TargetAttributes = { 159 | 4629E6DB20CB8D2900B0E223 = { 160 | CreatedOnToolsVersion = 9.2; 161 | ProvisioningStyle = Automatic; 162 | }; 163 | }; 164 | }; 165 | buildConfigurationList = 4629E6D720CB8D2900B0E223 /* Build configuration list for PBXProject "XDSDropDownMenuDEMO" */; 166 | compatibilityVersion = "Xcode 8.0"; 167 | developmentRegion = en; 168 | hasScannedForEncodings = 0; 169 | knownRegions = ( 170 | en, 171 | Base, 172 | ); 173 | mainGroup = 4629E6D320CB8D2900B0E223; 174 | productRefGroup = 4629E6DD20CB8D2900B0E223 /* Products */; 175 | projectDirPath = ""; 176 | projectRoot = ""; 177 | targets = ( 178 | 4629E6DB20CB8D2900B0E223 /* XDSDropDownMenuDEMO */, 179 | ); 180 | }; 181 | /* End PBXProject section */ 182 | 183 | /* Begin PBXResourcesBuildPhase section */ 184 | 4629E6DA20CB8D2900B0E223 /* Resources */ = { 185 | isa = PBXResourcesBuildPhase; 186 | buildActionMask = 2147483647; 187 | files = ( 188 | 4629E6EC20CB8D2900B0E223 /* LaunchScreen.storyboard in Resources */, 189 | 4629E6E920CB8D2900B0E223 /* Assets.xcassets in Resources */, 190 | 4629E6F820CB8D4B00B0E223 /* XDSTableViewController.storyboard in Resources */, 191 | 4629E6E720CB8D2900B0E223 /* Main.storyboard in Resources */, 192 | 4629E70820CB8D5700B0E223 /* XDSViewController.xib in Resources */, 193 | 4629E6FD20CB8D5100B0E223 /* XDSStartViewController.storyboard in Resources */, 194 | ); 195 | runOnlyForDeploymentPostprocessing = 0; 196 | }; 197 | /* End PBXResourcesBuildPhase section */ 198 | 199 | /* Begin PBXSourcesBuildPhase section */ 200 | 4629E6D820CB8D2900B0E223 /* Sources */ = { 201 | isa = PBXSourcesBuildPhase; 202 | buildActionMask = 2147483647; 203 | files = ( 204 | 4629E6FE20CB8D5100B0E223 /* XDSStartViewController.m in Sources */, 205 | 4629E6F920CB8D4B00B0E223 /* XDSTableViewController.m in Sources */, 206 | 4629E70620CB8D5700B0E223 /* XDSDropDownMenu.m in Sources */, 207 | 4629E6EF20CB8D2900B0E223 /* main.m in Sources */, 208 | 4629E70720CB8D5700B0E223 /* XDSViewController.m in Sources */, 209 | 4629E6E120CB8D2900B0E223 /* AppDelegate.m in Sources */, 210 | ); 211 | runOnlyForDeploymentPostprocessing = 0; 212 | }; 213 | /* End PBXSourcesBuildPhase section */ 214 | 215 | /* Begin PBXVariantGroup section */ 216 | 4629E6E520CB8D2900B0E223 /* Main.storyboard */ = { 217 | isa = PBXVariantGroup; 218 | children = ( 219 | 4629E6E620CB8D2900B0E223 /* Base */, 220 | ); 221 | name = Main.storyboard; 222 | sourceTree = ""; 223 | }; 224 | 4629E6EA20CB8D2900B0E223 /* LaunchScreen.storyboard */ = { 225 | isa = PBXVariantGroup; 226 | children = ( 227 | 4629E6EB20CB8D2900B0E223 /* Base */, 228 | ); 229 | name = LaunchScreen.storyboard; 230 | sourceTree = ""; 231 | }; 232 | /* End PBXVariantGroup section */ 233 | 234 | /* Begin XCBuildConfiguration section */ 235 | 4629E6F020CB8D2900B0E223 /* Debug */ = { 236 | isa = XCBuildConfiguration; 237 | buildSettings = { 238 | ALWAYS_SEARCH_USER_PATHS = NO; 239 | CLANG_ANALYZER_NONNULL = YES; 240 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 241 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 242 | CLANG_CXX_LIBRARY = "libc++"; 243 | CLANG_ENABLE_MODULES = YES; 244 | CLANG_ENABLE_OBJC_ARC = YES; 245 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 246 | CLANG_WARN_BOOL_CONVERSION = YES; 247 | CLANG_WARN_COMMA = YES; 248 | CLANG_WARN_CONSTANT_CONVERSION = YES; 249 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 250 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 251 | CLANG_WARN_EMPTY_BODY = YES; 252 | CLANG_WARN_ENUM_CONVERSION = YES; 253 | CLANG_WARN_INFINITE_RECURSION = YES; 254 | CLANG_WARN_INT_CONVERSION = YES; 255 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 256 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 257 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 258 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 259 | CLANG_WARN_STRICT_PROTOTYPES = YES; 260 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 261 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 262 | CLANG_WARN_UNREACHABLE_CODE = YES; 263 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 264 | CODE_SIGN_IDENTITY = "iPhone Developer"; 265 | COPY_PHASE_STRIP = NO; 266 | DEBUG_INFORMATION_FORMAT = dwarf; 267 | ENABLE_STRICT_OBJC_MSGSEND = YES; 268 | ENABLE_TESTABILITY = YES; 269 | GCC_C_LANGUAGE_STANDARD = gnu11; 270 | GCC_DYNAMIC_NO_PIC = NO; 271 | GCC_NO_COMMON_BLOCKS = YES; 272 | GCC_OPTIMIZATION_LEVEL = 0; 273 | GCC_PREPROCESSOR_DEFINITIONS = ( 274 | "DEBUG=1", 275 | "$(inherited)", 276 | ); 277 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 278 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 279 | GCC_WARN_UNDECLARED_SELECTOR = YES; 280 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 281 | GCC_WARN_UNUSED_FUNCTION = YES; 282 | GCC_WARN_UNUSED_VARIABLE = YES; 283 | IPHONEOS_DEPLOYMENT_TARGET = 11.2; 284 | MTL_ENABLE_DEBUG_INFO = YES; 285 | ONLY_ACTIVE_ARCH = YES; 286 | SDKROOT = iphoneos; 287 | }; 288 | name = Debug; 289 | }; 290 | 4629E6F120CB8D2900B0E223 /* Release */ = { 291 | isa = XCBuildConfiguration; 292 | buildSettings = { 293 | ALWAYS_SEARCH_USER_PATHS = NO; 294 | CLANG_ANALYZER_NONNULL = YES; 295 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 296 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 297 | CLANG_CXX_LIBRARY = "libc++"; 298 | CLANG_ENABLE_MODULES = YES; 299 | CLANG_ENABLE_OBJC_ARC = YES; 300 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 301 | CLANG_WARN_BOOL_CONVERSION = YES; 302 | CLANG_WARN_COMMA = YES; 303 | CLANG_WARN_CONSTANT_CONVERSION = YES; 304 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 305 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 306 | CLANG_WARN_EMPTY_BODY = YES; 307 | CLANG_WARN_ENUM_CONVERSION = YES; 308 | CLANG_WARN_INFINITE_RECURSION = YES; 309 | CLANG_WARN_INT_CONVERSION = YES; 310 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 311 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 312 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 313 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 314 | CLANG_WARN_STRICT_PROTOTYPES = YES; 315 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 316 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 317 | CLANG_WARN_UNREACHABLE_CODE = YES; 318 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 319 | CODE_SIGN_IDENTITY = "iPhone Developer"; 320 | COPY_PHASE_STRIP = NO; 321 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 322 | ENABLE_NS_ASSERTIONS = NO; 323 | ENABLE_STRICT_OBJC_MSGSEND = YES; 324 | GCC_C_LANGUAGE_STANDARD = gnu11; 325 | GCC_NO_COMMON_BLOCKS = YES; 326 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 327 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 328 | GCC_WARN_UNDECLARED_SELECTOR = YES; 329 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 330 | GCC_WARN_UNUSED_FUNCTION = YES; 331 | GCC_WARN_UNUSED_VARIABLE = YES; 332 | IPHONEOS_DEPLOYMENT_TARGET = 11.2; 333 | MTL_ENABLE_DEBUG_INFO = NO; 334 | SDKROOT = iphoneos; 335 | VALIDATE_PRODUCT = YES; 336 | }; 337 | name = Release; 338 | }; 339 | 4629E6F320CB8D2900B0E223 /* Debug */ = { 340 | isa = XCBuildConfiguration; 341 | buildSettings = { 342 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 343 | CODE_SIGN_STYLE = Automatic; 344 | DEVELOPMENT_TEAM = 695STD6YP8; 345 | INFOPLIST_FILE = XDSDropDownMenuDEMO/Info.plist; 346 | IPHONEOS_DEPLOYMENT_TARGET = 9.0; 347 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 348 | PRODUCT_BUNDLE_IDENTIFIER = com.itiis.XDSDropDownMenuDEMO; 349 | PRODUCT_NAME = "$(TARGET_NAME)"; 350 | TARGETED_DEVICE_FAMILY = "1,2"; 351 | }; 352 | name = Debug; 353 | }; 354 | 4629E6F420CB8D2900B0E223 /* Release */ = { 355 | isa = XCBuildConfiguration; 356 | buildSettings = { 357 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 358 | CODE_SIGN_STYLE = Automatic; 359 | DEVELOPMENT_TEAM = 695STD6YP8; 360 | INFOPLIST_FILE = XDSDropDownMenuDEMO/Info.plist; 361 | IPHONEOS_DEPLOYMENT_TARGET = 9.0; 362 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 363 | PRODUCT_BUNDLE_IDENTIFIER = com.itiis.XDSDropDownMenuDEMO; 364 | PRODUCT_NAME = "$(TARGET_NAME)"; 365 | TARGETED_DEVICE_FAMILY = "1,2"; 366 | }; 367 | name = Release; 368 | }; 369 | /* End XCBuildConfiguration section */ 370 | 371 | /* Begin XCConfigurationList section */ 372 | 4629E6D720CB8D2900B0E223 /* Build configuration list for PBXProject "XDSDropDownMenuDEMO" */ = { 373 | isa = XCConfigurationList; 374 | buildConfigurations = ( 375 | 4629E6F020CB8D2900B0E223 /* Debug */, 376 | 4629E6F120CB8D2900B0E223 /* Release */, 377 | ); 378 | defaultConfigurationIsVisible = 0; 379 | defaultConfigurationName = Release; 380 | }; 381 | 4629E6F220CB8D2900B0E223 /* Build configuration list for PBXNativeTarget "XDSDropDownMenuDEMO" */ = { 382 | isa = XCConfigurationList; 383 | buildConfigurations = ( 384 | 4629E6F320CB8D2900B0E223 /* Debug */, 385 | 4629E6F420CB8D2900B0E223 /* Release */, 386 | ); 387 | defaultConfigurationIsVisible = 0; 388 | defaultConfigurationName = Release; 389 | }; 390 | /* End XCConfigurationList section */ 391 | }; 392 | rootObject = 4629E6D420CB8D2900B0E223 /* Project object */; 393 | } 394 | -------------------------------------------------------------------------------- /XDSDropDownMenuDEMO/XDSDropDownMenuDEMO.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /XDSDropDownMenuDEMO/XDSDropDownMenuDEMO.xcodeproj/project.xcworkspace/xcuserdata/cdj.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iWindUpBird/DropDownMenu-SelectMenu/b4f99df185c29b9b815bd79352d5f80f119537f7/XDSDropDownMenuDEMO/XDSDropDownMenuDEMO.xcodeproj/project.xcworkspace/xcuserdata/cdj.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /XDSDropDownMenuDEMO/XDSDropDownMenuDEMO.xcodeproj/xcuserdata/cdj.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | XDSDropDownMenuDEMO.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /XDSDropDownMenuDEMO/XDSDropDownMenuDEMO/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "20x20", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "20x20", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "29x29", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "29x29", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "40x40", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "40x40", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "size" : "60x60", 36 | "scale" : "2x" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "size" : "60x60", 41 | "scale" : "3x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "20x20", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "20x20", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "29x29", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "29x29", 61 | "scale" : "2x" 62 | }, 63 | { 64 | "idiom" : "ipad", 65 | "size" : "40x40", 66 | "scale" : "1x" 67 | }, 68 | { 69 | "idiom" : "ipad", 70 | "size" : "40x40", 71 | "scale" : "2x" 72 | }, 73 | { 74 | "idiom" : "ipad", 75 | "size" : "76x76", 76 | "scale" : "1x" 77 | }, 78 | { 79 | "idiom" : "ipad", 80 | "size" : "76x76", 81 | "scale" : "2x" 82 | }, 83 | { 84 | "idiom" : "ipad", 85 | "size" : "83.5x83.5", 86 | "scale" : "2x" 87 | } 88 | ], 89 | "info" : { 90 | "version" : 1, 91 | "author" : "xcode" 92 | } 93 | } -------------------------------------------------------------------------------- /XDSDropDownMenuDEMO/XDSDropDownMenuDEMO/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | LSRequiresIPhoneOS 22 | 23 | UILaunchStoryboardName 24 | LaunchScreen 25 | UIMainStoryboardFile 26 | Main 27 | UIRequiredDeviceCapabilities 28 | 29 | armv7 30 | 31 | UISupportedInterfaceOrientations 32 | 33 | UIInterfaceOrientationPortrait 34 | UIInterfaceOrientationLandscapeLeft 35 | UIInterfaceOrientationLandscapeRight 36 | 37 | UISupportedInterfaceOrientations~ipad 38 | 39 | UIInterfaceOrientationPortrait 40 | UIInterfaceOrientationPortraitUpsideDown 41 | UIInterfaceOrientationLandscapeLeft 42 | UIInterfaceOrientationLandscapeRight 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /XDSDropDownMenuDEMO/XDSDropDownMenuDEMO/XDSDropDownMenu/XDSDropDownMenu.h: -------------------------------------------------------------------------------- 1 | // 2 | // XDSDropDownMenu.h 3 | // shts_ios_xds 4 | // 5 | // Created by cdj on 2018/5/8. 6 | // Copyright © 2018年 itiis. All rights reserved. 7 | // 8 | 9 | #pragma mark - 选择菜单封装类 10 | 11 | #import 12 | 13 | @class XDSDropDownMenu; 14 | @protocol XDSDropDownMenuDelegate 15 | @required 16 | - (void)setDropDownDelegate:(XDSDropDownMenu *)sender;//设置下拉菜单的tag值 17 | @end 18 | 19 | @interface XDSDropDownMenu : UIView 20 | 21 | @property (nonatomic, retain) id delegate;//代理 22 | @property (nonatomic, retain) NSString *animationDirection;//动画方向 23 | @property (nonatomic, strong) UIImageView *imageView;//图片视图 24 | 25 | //隐藏选择菜单 26 | - (void)hideDropDownMenuWithBtnFrame:(CGRect)btnFrame; 27 | 28 | 29 | //返回选项的下标 30 | + (NSInteger)returnIndexByString:(NSString *)string fromArray:(NSArray *)array; 31 | 32 | //显示选择菜单 33 | /* 34 | button 选择按钮 35 | buttonFrame 选择按钮在self.view的frame 36 | titleArr 选择菜单的文本数组 37 | imageArr 选择菜单的图片数组 38 | direction 选择菜单的方向:down 或者 up 39 | */ 40 | - (void)showDropDownMenu:(UIButton *)button withButtonFrame:(CGRect)buttonFrame arrayOfTitle:(NSArray *)titleArr arrayOfImage:(NSArray *)imageArr animationDirection:(NSString *)direction; 41 | @end 42 | -------------------------------------------------------------------------------- /XDSDropDownMenuDEMO/XDSDropDownMenuDEMO/XDSDropDownMenu/XDSDropDownMenu.m: -------------------------------------------------------------------------------- 1 | // 2 | // XDSDropDownMenu.m 3 | // shts_ios_xds 4 | // 5 | // Created by cdj on 2018/5/8. 6 | // Copyright © 2018年 itiis. All rights reserved. 7 | // 8 | 9 | #import "XDSDropDownMenu.h" 10 | 11 | @interface XDSDropDownMenu() 12 | @property(nonatomic, strong) UITableView *menuTableView; 13 | @property(nonatomic, strong) UIButton *btnSender; 14 | @property(nonatomic) CGRect buttonFrame; 15 | @property(nonatomic, retain) NSArray *titleList; 16 | @property(nonatomic, retain) NSArray *imageList; 17 | @end 18 | 19 | @implementation XDSDropDownMenu 20 | 21 | +(NSInteger)returnIndexByString:(NSString *)string fromArray:(NSArray *)array{ //返回选项下标 22 | NSUInteger index = [array indexOfObject:string]; 23 | return index; 24 | } 25 | 26 | - (void)showDropDownMenu:(UIButton *)button withButtonFrame:(CGRect)buttonFrame arrayOfTitle:(NSArray *)titleArr arrayOfImage:(NSArray *)imageArr animationDirection:(NSString *)direction{ 27 | 28 | self.backgroundColor = [UIColor whiteColor]; 29 | self.animationDirection = direction; 30 | self.btnSender = button; 31 | self.menuTableView = (UITableView *)[super init]; 32 | 33 | self.buttonFrame = buttonFrame; 34 | if (self) { 35 | CGRect btnRect = buttonFrame;//按钮在视图上的位置 36 | CGFloat height = 0;//菜单高度 37 | if ( titleArr.count <= 4) { 38 | height = titleArr.count *40; 39 | }else{ 40 | height = 200; 41 | } 42 | 43 | self.titleList = [NSArray arrayWithArray:titleArr]; 44 | self.imageList = [NSArray arrayWithArray:imageArr]; 45 | 46 | //菜单视图的起始大小和位置 47 | if ([direction isEqualToString:@"up"]) { 48 | self.frame = CGRectMake(btnRect.origin.x, btnRect.origin.y-2, btnRect.size.width, 0); 49 | }else if ([direction isEqualToString:@"down"]) { 50 | self.frame = CGRectMake(btnRect.origin.x, btnRect.origin.y+btnRect.size.height+2, btnRect.size.width, 0); 51 | } 52 | 53 | self.layer.masksToBounds = NO; 54 | self.layer.cornerRadius = 8; 55 | self.layer.shadowRadius = 5; 56 | self.layer.shadowOpacity = 0.5; 57 | 58 | self.menuTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, btnRect.size.width, 0)]; 59 | self.menuTableView.delegate = self; 60 | self.menuTableView.dataSource = self; 61 | self.menuTableView.layer.cornerRadius = 5; 62 | self.menuTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine; 63 | self.menuTableView.separatorColor = [UIColor grayColor]; 64 | self.menuTableView.separatorInset = UIEdgeInsetsMake(0, -20, 0, 0); 65 | self.menuTableView.backgroundColor = [UIColor whiteColor]; 66 | self.menuTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, btnRect.size.width, 0.001)];//最后无分割线 67 | [self.menuTableView flashScrollIndicators];//显示滚动条 68 | 69 | [UIView beginAnimations:nil context:nil];//动画 70 | [UIView setAnimationDuration:0.5]; 71 | //菜单视图的最终大小和位置 72 | if ([direction isEqualToString:@"up"]) { 73 | self.frame = CGRectMake(btnRect.origin.x, btnRect.origin.y-height-2, btnRect.size.width, height); 74 | } else if([direction isEqualToString:@"down"]) { 75 | self.frame = CGRectMake(btnRect.origin.x, btnRect.origin.y+btnRect.size.height+2, btnRect.size.width, height); 76 | } 77 | self.menuTableView.frame = CGRectMake(0, 0, btnRect.size.width, height); 78 | [UIView commitAnimations]; 79 | [self addSubview:self.menuTableView]; 80 | } 81 | 82 | } 83 | 84 | 85 | -(void)hideDropDownMenuWithBtnFrame:(CGRect)btnFrame { 86 | 87 | // CGRect btn = button.frame; 88 | [UIView beginAnimations:nil context:nil]; 89 | [UIView setAnimationDuration:0.3]; 90 | if ([self.animationDirection isEqualToString:@"up"]) { 91 | self.frame = CGRectMake(btnFrame.origin.x, btnFrame.origin.y-2, btnFrame.size.width, 0); 92 | }else if ([self.animationDirection isEqualToString:@"down"]) { 93 | self.frame = CGRectMake(btnFrame.origin.x, btnFrame.origin.y+btnFrame.size.height+2, btnFrame.size.width, 0); 94 | } 95 | self.menuTableView.frame = CGRectMake(0, 0, btnFrame.size.width, 0); 96 | [UIView commitAnimations]; 97 | } 98 | 99 | 100 | - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 101 | return 1; 102 | } 103 | 104 | -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 105 | return 40; 106 | } 107 | 108 | - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 109 | return [self.titleList count]; 110 | } 111 | - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 112 | static NSString *CellIdentifier = @"Cell"; 113 | 114 | UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 115 | if (cell == nil) { 116 | cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 117 | cell.textLabel.font = [UIFont systemFontOfSize:15]; 118 | cell.textLabel.textAlignment = NSTextAlignmentCenter; 119 | } 120 | if ([self.imageList count] == [self.titleList count]) { 121 | cell.textLabel.text =[self.titleList objectAtIndex:indexPath.row]; 122 | cell.imageView.image = [self.imageList objectAtIndex:indexPath.row]; 123 | } else if ([self.imageList count] > [self.titleList count]) { 124 | cell.textLabel.text =[self.titleList objectAtIndex:indexPath.row]; 125 | if (indexPath.row < [self.imageList count]) { 126 | cell.imageView.image = [self.imageList objectAtIndex:indexPath.row]; 127 | } 128 | } else if ([self.imageList count] < [self.titleList count]) { 129 | cell.textLabel.text =[self.titleList objectAtIndex:indexPath.row]; 130 | if (indexPath.row < [self.imageList count]) { 131 | cell.imageView.image = [self.imageList objectAtIndex:indexPath.row]; 132 | } 133 | } 134 | 135 | cell.textLabel.textColor = [UIColor blackColor]; 136 | UIView * v = [[UIView alloc] init]; 137 | v.backgroundColor = [UIColor grayColor]; 138 | cell.selectedBackgroundView = v; 139 | 140 | return cell; 141 | } 142 | - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 143 | [self hideDropDownMenuWithBtnFrame:self.buttonFrame]; 144 | 145 | UITableViewCell *c = [tableView cellForRowAtIndexPath:indexPath]; 146 | [self.btnSender setTitle:c.textLabel.text forState:UIControlStateNormal]; 147 | 148 | for (UIView *subview in self.btnSender.subviews) { 149 | if ([subview isKindOfClass:[UIImageView class]]) { 150 | [subview removeFromSuperview]; 151 | } 152 | } 153 | self.imageView.image = c.imageView.image; 154 | self.imageView = [[UIImageView alloc] initWithImage:c.imageView.image]; 155 | self.imageView.frame = CGRectMake(5, 5, 25, 25); 156 | [self.btnSender addSubview:self.imageView]; 157 | 158 | [self myDelegate]; 159 | } 160 | - (void) myDelegate { 161 | [self.delegate setDropDownDelegate:self]; 162 | } 163 | -(void)dealloc { 164 | 165 | } 166 | @end 167 | -------------------------------------------------------------------------------- /XDSDropDownMenuDEMO/XDSDropDownMenuDEMO/XDSStartViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // XDSDemoViewController.h 3 | // XDSDropDownMenu 4 | // 5 | // Created by cdj on 2018/6/9. 6 | // Copyright © 2018年 itiis. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface XDSStartViewController : UITableViewController 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /XDSDropDownMenuDEMO/XDSDropDownMenuDEMO/XDSStartViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // XDSDemoViewController.m 3 | // XDSDropDownMenu 4 | // 5 | // Created by cdj on 2018/6/9. 6 | // Copyright © 2018年 itiis. All rights reserved. 7 | // 8 | 9 | #import "XDSStartViewController.h" 10 | 11 | #import "XDSViewController.h" 12 | 13 | #import "XDSTableViewController.h" 14 | 15 | @interface XDSStartViewController () 16 | 17 | @end 18 | 19 | @implementation XDSStartViewController 20 | 21 | - (void)viewDidLoad { 22 | [super viewDidLoad]; 23 | 24 | } 25 | 26 | - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 27 | 28 | //普通UIView 29 | if (indexPath.section == 0) { 30 | 31 | XDSViewController *vc = [[XDSViewController alloc] init]; 32 | 33 | [self presentViewController:vc animated:YES completion:nil]; 34 | } 35 | 36 | //UITableView 37 | else if (indexPath.section == 1){ 38 | 39 | UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"XDSTableViewController" bundle:nil]; 40 | 41 | XDSTableViewController *vc = [storyBoard instantiateInitialViewController]; 42 | 43 | [self presentViewController:vc animated:YES completion:nil]; 44 | 45 | } 46 | 47 | } 48 | 49 | 50 | - (void)didReceiveMemoryWarning { 51 | [super didReceiveMemoryWarning]; 52 | // Dispose of any resources that can be recreated. 53 | } 54 | 55 | 56 | @end 57 | -------------------------------------------------------------------------------- /XDSDropDownMenuDEMO/XDSDropDownMenuDEMO/XDSStartViewController.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | -------------------------------------------------------------------------------- /XDSDropDownMenuDEMO/XDSDropDownMenuDEMO/XDSTableViewController/XDSTableViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // XDSTableViewController.h 3 | // XDSDropDownMenu 4 | // 5 | // Created by cdj on 2018/6/9. 6 | // Copyright © 2018年 itiis. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface XDSTableViewController : UITableViewController 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /XDSDropDownMenuDEMO/XDSDropDownMenuDEMO/XDSTableViewController/XDSTableViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // XDSTableViewController.m 3 | // XDSDropDownMenu 4 | // 5 | // Created by cdj on 2018/6/9. 6 | // Copyright © 2018年 itiis. All rights reserved. 7 | // 8 | 9 | #import "XDSTableViewController.h" 10 | 11 | #import "XDSDropDownMenu.h" 12 | 13 | @interface XDSTableViewController () 14 | 15 | @property (weak, nonatomic) IBOutlet UIButton *nameButton; 16 | @property (weak, nonatomic) IBOutlet UIButton *sexButton; 17 | @property (weak, nonatomic) IBOutlet UIButton *downButton; 18 | @property (weak, nonatomic) IBOutlet UIButton *upButton; 19 | 20 | @property (nonatomic, strong) NSArray *buttonArray; 21 | @property (nonatomic, strong) NSArray *dropDownMenuArray; 22 | 23 | @end 24 | 25 | @implementation XDSTableViewController{ 26 | 27 | XDSDropDownMenu *nameDropDownMenu; 28 | XDSDropDownMenu *sexDropDownMenu; 29 | XDSDropDownMenu *downDropDownMenu; 30 | XDSDropDownMenu *upDropDownMenu; 31 | 32 | } 33 | 34 | - (void)viewDidLoad { 35 | [super viewDidLoad]; 36 | 37 | [self setArrays]; //配置buttonArray和dropDownMenuArray 38 | [self setButtons];//设置按钮边框和圆角 39 | 40 | } 41 | 42 | 43 | #pragma mark - 配置buttonArray和dropDownMenuArray 44 | - (void)setArrays{ 45 | 46 | //将所有按钮加入buttonArray数组 47 | self.buttonArray = @[self.nameButton,self.sexButton,self.downButton,self.upButton]; 48 | 49 | //初始化所有DropDownMenu下拉菜单 50 | nameDropDownMenu = [[XDSDropDownMenu alloc] init]; 51 | sexDropDownMenu = [[XDSDropDownMenu alloc] init]; 52 | downDropDownMenu = [[XDSDropDownMenu alloc] init]; 53 | upDropDownMenu = [[XDSDropDownMenu alloc] init]; 54 | 55 | 56 | //将所有DropDownMenu加入dropDownMenuArray数组 57 | self.dropDownMenuArray = @[nameDropDownMenu,sexDropDownMenu,downDropDownMenu,upDropDownMenu]; 58 | 59 | //将所有dropDownMenu的初始tag值设为1000 60 | for (__strong XDSDropDownMenu *nextDropDownMenu in self.dropDownMenuArray) { 61 | nextDropDownMenu.tag = 1000; 62 | } 63 | 64 | 65 | } 66 | 67 | 68 | 69 | 70 | #pragma mark - name按钮 71 | - (IBAction)nameBtnClick:(UIButton *)sender { 72 | 73 | NSArray *arr = @[@"xiaoming",@"Sam",@"Tom",@"Jack"]; 74 | 75 | nameDropDownMenu.delegate = self;//设置代理 76 | 77 | //调用方法判断是显示下拉菜单,还是隐藏下拉菜单 78 | [self setupDropDownMenu:nameDropDownMenu withTitleArray:arr andButton:sender andDirection:@"down"]; 79 | 80 | //隐藏其它的下拉菜单 81 | [self hideOtherDropDownMenu:nameDropDownMenu]; 82 | } 83 | 84 | #pragma mark - sex按钮 85 | - (IBAction)sexBtnClick:(UIButton *)sender { 86 | NSArray *arr = @[@"man",@"woman"]; 87 | sexDropDownMenu.delegate = self;//代理 88 | [self setupDropDownMenu:sexDropDownMenu withTitleArray:arr andButton:sender andDirection:@"down"]; 89 | [self hideOtherDropDownMenu:sexDropDownMenu]; 90 | 91 | } 92 | 93 | 94 | #pragma mark - down按钮 95 | - (IBAction)downBtnClick:(UIButton *)sender { 96 | NSArray *arr = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7"]; 97 | downDropDownMenu.delegate = self;//代理 98 | [self setupDropDownMenu:downDropDownMenu withTitleArray:arr andButton:sender andDirection:@"down"]; 99 | [self hideOtherDropDownMenu:downDropDownMenu]; 100 | } 101 | 102 | #pragma mark - up按钮 103 | - (IBAction)upBtnClick:(UIButton *)sender { 104 | NSArray *arr = @[@"1",@"2",@"3"]; 105 | upDropDownMenu.delegate = self;//代理 106 | [self setupDropDownMenu:upDropDownMenu withTitleArray:arr andButton:sender andDirection:@"up"]; 107 | [self hideOtherDropDownMenu:upDropDownMenu]; 108 | 109 | } 110 | 111 | 112 | 113 | - (IBAction)backBtnClick:(UIButton *)sender { 114 | 115 | [self dismissViewControllerAnimated:YES completion:nil]; 116 | 117 | } 118 | 119 | #pragma mark - 设置dropDownMenu 120 | 121 | /* 122 | 判断是显示dropDownMenu还是收回dropDownMenu 123 | */ 124 | 125 | - (void)setupDropDownMenu:(XDSDropDownMenu *)dropDownMenu withTitleArray:(NSArray *)titleArray andButton:(UIButton *)button andDirection:(NSString *)direction{ 126 | 127 | // CGRect btnFrame = button.frame; //如果按钮在UIIiew上用这个 128 | 129 | CGRect btnFrame = [self getBtnFrame:button];//如果按钮在UITabelView上用这个 130 | 131 | 132 | if(dropDownMenu.tag == 1000){ 133 | 134 | /* 135 | 如果dropDownMenu的tag值为1000,表示dropDownMenu没有打开,则打开dropDownMenu 136 | */ 137 | 138 | //初始化选择菜单 139 | [dropDownMenu showDropDownMenu:button withButtonFrame:btnFrame arrayOfTitle:titleArray arrayOfImage:nil animationDirection:direction]; 140 | 141 | //添加到主视图上 142 | [self.view addSubview:dropDownMenu]; 143 | 144 | //将dropDownMenu的tag值设为2000,表示已经打开了dropDownMenu 145 | dropDownMenu.tag = 2000; 146 | 147 | }else { 148 | 149 | /* 150 | 如果dropDownMenu的tag值为2000,表示dropDownMenu已经打开,则隐藏dropDownMenu 151 | */ 152 | 153 | [dropDownMenu hideDropDownMenuWithBtnFrame:btnFrame]; 154 | dropDownMenu.tag = 1000; 155 | } 156 | } 157 | 158 | #pragma mark - 隐藏其它DropDownMenu 159 | /* 160 | 在点击按钮的时候,隐藏其它打开的下拉菜单(dropDownMenu) 161 | */ 162 | - (void)hideOtherDropDownMenu:(XDSDropDownMenu *)dropDownMenu{ 163 | 164 | for ( int i = 0; i < self.dropDownMenuArray.count; i++ ) { 165 | 166 | if( self.dropDownMenuArray[i] != dropDownMenu){ 167 | 168 | XDSDropDownMenu *dropDownMenuNext = self.dropDownMenuArray[i]; 169 | 170 | // CGRect btnFrame = ((UIButton *)self.buttonArray[i]).frame;//如果按钮在UIIiew上用这个 171 | 172 | CGRect btnFrame = [self getBtnFrame:self.buttonArray[i]];//如果按钮在UITabelView上用这个 173 | 174 | [dropDownMenuNext hideDropDownMenuWithBtnFrame:btnFrame]; 175 | dropDownMenuNext.tag = 1000; 176 | } 177 | } 178 | } 179 | 180 | #pragma mark - 获取按钮在self.view的坐标(按钮在UITableView上使用这个方法) 181 | /* 182 | 因为按钮在UITableView上是放在cell的contentView上的,所以要通过以下方法获得其在self.view上坐标 183 | */ 184 | 185 | - (CGRect )getBtnFrame:(UIButton *)button{ 186 | return [button.superview convertRect:button.frame toView:self.view]; 187 | } 188 | 189 | 190 | 191 | #pragma mark - 下拉菜单代理 192 | /* 193 | 在点击下拉菜单后,将其tag值重新设为1000 194 | */ 195 | 196 | - (void) setDropDownDelegate:(XDSDropDownMenu *)sender{ 197 | sender.tag = 1000; 198 | } 199 | 200 | 201 | 202 | #pragma mark - 设置按钮边框和圆角 203 | - (void)setButtons{ 204 | for(UIButton *btn in self.buttonArray){ 205 | btn.layer.cornerRadius = 3; 206 | btn.layer.borderColor = [[UIColor blackColor] CGColor]; 207 | btn.layer.borderWidth = 0.5; 208 | btn.layer.masksToBounds = YES; 209 | } 210 | } 211 | 212 | 213 | - (void)didReceiveMemoryWarning { 214 | [super didReceiveMemoryWarning]; 215 | 216 | } 217 | 218 | 219 | 220 | 221 | 222 | @end 223 | -------------------------------------------------------------------------------- /XDSDropDownMenuDEMO/XDSDropDownMenuDEMO/XDSTableViewController/XDSTableViewController.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 31 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 62 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 95 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 128 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 161 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | -------------------------------------------------------------------------------- /XDSDropDownMenuDEMO/XDSDropDownMenuDEMO/XDSViewController/XDSViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // XDSViewController.h 3 | // XDSDropDownMenu 4 | // 5 | // Created by cdj on 2018/6/9. 6 | // Copyright © 2018年 itiis. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface XDSViewController : UIViewController 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /XDSDropDownMenuDEMO/XDSDropDownMenuDEMO/XDSViewController/XDSViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // XDSViewController.m 3 | // XDSDropDownMenu 4 | // 5 | // Created by cdj on 2018/6/9. 6 | // Copyright © 2018年 itiis. All rights reserved. 7 | // 8 | 9 | #import "XDSViewController.h" 10 | 11 | #import "XDSDropDownMenu.h" 12 | 13 | @interface XDSViewController () 14 | @property (weak, nonatomic) IBOutlet UIButton *nameButton; 15 | @property (weak, nonatomic) IBOutlet UIButton *downButton; 16 | @property (weak, nonatomic) IBOutlet UIButton *upButton; 17 | @property (weak, nonatomic) IBOutlet UIButton *sexButton; 18 | 19 | 20 | @property (nonatomic, strong) NSArray *buttonArray; 21 | @property (nonatomic, strong) NSArray *dropDownMenuArray; 22 | 23 | @end 24 | 25 | @implementation XDSViewController{ 26 | 27 | XDSDropDownMenu *nameDropDownMenu; 28 | XDSDropDownMenu *downDropDownMenu; 29 | XDSDropDownMenu *upDropDownMenu; 30 | XDSDropDownMenu *sexDropDownMenu; 31 | 32 | } 33 | 34 | - (void)viewDidLoad { 35 | [super viewDidLoad]; 36 | 37 | [self setArrays]; //配置buttonArray和dropDownMenuArray 38 | [self setButtons];//设置按钮边框和圆角 39 | 40 | 41 | } 42 | 43 | #pragma mark - 配置buttonArray和dropDownMenuArray 44 | - (void)setArrays{ 45 | 46 | //将所有按钮加入buttonArray数组 47 | self.buttonArray = @[self.nameButton,self.downButton,self.upButton,self.sexButton]; 48 | 49 | //初始化所有DropDownMenu下拉菜单 50 | nameDropDownMenu = [[XDSDropDownMenu alloc] init]; 51 | downDropDownMenu = [[XDSDropDownMenu alloc] init]; 52 | upDropDownMenu = [[XDSDropDownMenu alloc] init]; 53 | sexDropDownMenu = [[XDSDropDownMenu alloc] init]; 54 | 55 | //将所有DropDownMenu加入dropDownMenuArray数组 56 | self.dropDownMenuArray = @[nameDropDownMenu,downDropDownMenu,upDropDownMenu,sexDropDownMenu]; 57 | 58 | //将所有dropDownMenu的初始tag值设为1000 59 | for (__strong XDSDropDownMenu *nextDropDownMenu in self.dropDownMenuArray) { 60 | nextDropDownMenu.tag = 1000; 61 | } 62 | 63 | 64 | } 65 | 66 | 67 | 68 | 69 | #pragma mark - name按钮 70 | - (IBAction)nameBtnClick:(UIButton *)sender { 71 | 72 | NSArray *arr = @[@"xiaoming",@"Sam",@"Tom",@"Jack"]; 73 | 74 | nameDropDownMenu.delegate = self;//设置代理 75 | 76 | //调用方法判断是显示下拉菜单,还是隐藏下拉菜单 77 | [self setupDropDownMenu:nameDropDownMenu withTitleArray:arr andButton:sender andDirection:@"down"]; 78 | 79 | //隐藏其它的下拉菜单 80 | [self hideOtherDropDownMenu:nameDropDownMenu]; 81 | } 82 | 83 | #pragma mark - down按钮 84 | - (IBAction)downBtnClick:(UIButton *)sender { 85 | NSArray *arr = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7"]; 86 | downDropDownMenu.delegate = self;//代理 87 | [self setupDropDownMenu:downDropDownMenu withTitleArray:arr andButton:sender andDirection:@"down"]; 88 | [self hideOtherDropDownMenu:downDropDownMenu]; 89 | } 90 | 91 | #pragma mark - up按钮 92 | - (IBAction)upBtnClick:(UIButton *)sender { 93 | NSArray *arr = @[@"1",@"2",@"3"]; 94 | upDropDownMenu.delegate = self;//代理 95 | [self setupDropDownMenu:upDropDownMenu withTitleArray:arr andButton:sender andDirection:@"up"]; 96 | [self hideOtherDropDownMenu:upDropDownMenu]; 97 | 98 | } 99 | 100 | #pragma mark - sex按钮 101 | - (IBAction)sexBtnClick:(UIButton *)sender { 102 | NSArray *arr = @[@"man",@"woman"]; 103 | sexDropDownMenu.delegate = self;//代理 104 | [self setupDropDownMenu:sexDropDownMenu withTitleArray:arr andButton:sender andDirection:@"down"]; 105 | [self hideOtherDropDownMenu:sexDropDownMenu]; 106 | 107 | } 108 | 109 | - (IBAction)backBtnClick:(UIButton *)sender { 110 | 111 | [self dismissViewControllerAnimated:YES completion:nil]; 112 | 113 | } 114 | 115 | #pragma mark - 设置dropDownMenu 116 | 117 | /* 118 | 判断是显示dropDownMenu还是收回dropDownMenu 119 | */ 120 | 121 | - (void)setupDropDownMenu:(XDSDropDownMenu *)dropDownMenu withTitleArray:(NSArray *)titleArray andButton:(UIButton *)button andDirection:(NSString *)direction{ 122 | 123 | CGRect btnFrame = button.frame; //如果按钮在UIIiew上用这个 124 | 125 | // CGRect btnFrame = [self getBtnFrame:button];//如果按钮在UITabelView上用这个 126 | 127 | 128 | if(dropDownMenu.tag == 1000){ 129 | 130 | /* 131 | 如果dropDownMenu的tag值为1000,表示dropDownMenu没有打开,则打开dropDownMenu 132 | */ 133 | 134 | //初始化选择菜单 135 | [dropDownMenu showDropDownMenu:button withButtonFrame:btnFrame arrayOfTitle:titleArray arrayOfImage:nil animationDirection:direction]; 136 | 137 | //添加到主视图上 138 | [self.view addSubview:dropDownMenu]; 139 | 140 | //将dropDownMenu的tag值设为2000,表示已经打开了dropDownMenu 141 | dropDownMenu.tag = 2000; 142 | 143 | }else { 144 | 145 | /* 146 | 如果dropDownMenu的tag值为2000,表示dropDownMenu已经打开,则隐藏dropDownMenu 147 | */ 148 | 149 | [dropDownMenu hideDropDownMenuWithBtnFrame:btnFrame]; 150 | dropDownMenu.tag = 1000; 151 | } 152 | } 153 | 154 | #pragma mark - 隐藏其它DropDownMenu 155 | /* 156 | 在点击按钮的时候,隐藏其它打开的下拉菜单(dropDownMenu) 157 | */ 158 | - (void)hideOtherDropDownMenu:(XDSDropDownMenu *)dropDownMenu{ 159 | 160 | for ( int i = 0; i < self.dropDownMenuArray.count; i++ ) { 161 | 162 | if( self.dropDownMenuArray[i] != dropDownMenu){ 163 | 164 | XDSDropDownMenu *dropDownMenuNext = self.dropDownMenuArray[i]; 165 | 166 | CGRect btnFrame = ((UIButton *)self.buttonArray[i]).frame;//如果按钮在UIIiew上用这个 167 | 168 | // CGRect btnFrame = [self getBtnFrame:self.buttonArray[i]];//如果按钮在UITabelView上用这个 169 | 170 | [dropDownMenuNext hideDropDownMenuWithBtnFrame:btnFrame]; 171 | dropDownMenuNext.tag = 1000; 172 | } 173 | } 174 | } 175 | 176 | //#pragma mark - 获取按钮在self.view的坐标(按钮在UITableView上使用这个方法) 177 | /* 178 | 因为按钮在UITableView上是放在cell的contentView上的,所以要通过以下方法获得其在self.view上坐标 179 | */ 180 | 181 | //- (CGRect )getBtnFrame:(UIButton *)button{ 182 | // return [button.superview convertRect:button.frame toView:self.view]; 183 | //} 184 | 185 | 186 | 187 | #pragma mark - 下拉菜单代理 188 | /* 189 | 在点击下拉菜单后,将其tag值重新设为1000 190 | */ 191 | 192 | - (void) setDropDownDelegate:(XDSDropDownMenu *)sender{ 193 | sender.tag = 1000; 194 | } 195 | 196 | 197 | 198 | #pragma mark - 设置按钮边框和圆角 199 | - (void)setButtons{ 200 | for(UIButton *btn in self.buttonArray){ 201 | btn.layer.cornerRadius = 3; 202 | btn.layer.borderColor = [[UIColor blackColor] CGColor]; 203 | btn.layer.borderWidth = 0.5; 204 | btn.layer.masksToBounds = YES; 205 | } 206 | } 207 | 208 | - (void)didReceiveMemoryWarning { 209 | [super didReceiveMemoryWarning]; 210 | 211 | } 212 | 213 | 214 | 215 | @end 216 | -------------------------------------------------------------------------------- /XDSDropDownMenuDEMO/XDSDropDownMenuDEMO/XDSViewController/XDSViewController.xib: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 40 | 46 | 52 | 65 | 71 | 84 | 90 | 103 | 111 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | -------------------------------------------------------------------------------- /XDSDropDownMenuDEMO/XDSDropDownMenuDEMO/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // XDSDropDownMenuDEMO 4 | // 5 | // Created by cdj on 2018/6/9. 6 | // Copyright © 2018年 itiis. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "AppDelegate.h" 11 | 12 | int main(int argc, char * argv[]) { 13 | @autoreleasepool { 14 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /XDSDropDownMenuDEMO/XDSDropDownMenuDEMO/other/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // XDSDropDownMenuDEMO 4 | // 5 | // Created by cdj on 2018/6/9. 6 | // Copyright © 2018年 itiis. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface AppDelegate : UIResponder 12 | 13 | @property (strong, nonatomic) UIWindow *window; 14 | 15 | 16 | @end 17 | 18 | -------------------------------------------------------------------------------- /XDSDropDownMenuDEMO/XDSDropDownMenuDEMO/other/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // XDSDropDownMenuDEMO 4 | // 5 | // Created by cdj on 2018/6/9. 6 | // Copyright © 2018年 itiis. All rights reserved. 7 | // 8 | 9 | #import "AppDelegate.h" 10 | #import "XDSStartViewController.h" 11 | @interface AppDelegate () 12 | 13 | @end 14 | 15 | @implementation AppDelegate 16 | 17 | 18 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 19 | 20 | self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; 21 | 22 | UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"XDSStartViewController" bundle:nil]; 23 | 24 | XDSStartViewController *startVC = [storyBoard instantiateInitialViewController]; 25 | 26 | self.window.rootViewController = startVC; 27 | 28 | return YES; 29 | } 30 | 31 | 32 | 33 | - (void)applicationWillResignActive:(UIApplication *)application { 34 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 35 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. 36 | } 37 | 38 | 39 | - (void)applicationDidEnterBackground:(UIApplication *)application { 40 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 41 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 42 | } 43 | 44 | 45 | - (void)applicationWillEnterForeground:(UIApplication *)application { 46 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 47 | } 48 | 49 | 50 | - (void)applicationDidBecomeActive:(UIApplication *)application { 51 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 52 | } 53 | 54 | 55 | - (void)applicationWillTerminate:(UIApplication *)application { 56 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 57 | } 58 | 59 | 60 | @end 61 | -------------------------------------------------------------------------------- /XDSDropDownMenuDEMO/XDSDropDownMenuDEMO/other/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /XDSDropDownMenuDEMO/XDSDropDownMenuDEMO/other/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | --------------------------------------------------------------------------------