iOS 下拉菜單

隨著移動互聯網的發展,手機App的功能是越來越復雜,給用戶提供的選擇也越來越多,那么怎么樣才能更好的給用戶提供高效,完美的體驗呢?

有時候下拉菜單不得不是你最好的選擇

簡易下拉菜單.png

#import "ViewController.h"
#import "DownMenu.h"


這里導入自定義視圖DownMenu.h,以設置代理的形式回調視圖狀態事件

@interface ViewController ()<DownMenuDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    DownMenu *menu = [[DownMenu alloc] initWithFrame:CGRectMake(50, 100, 120, 30)];
    [self.view addSubview:menu];
    menu.delegate = self;
    [menu setMainMenuTitle:@"請選擇" chlidTitleAry:@[@"選項一",@"選項二",@"選項三",@"選項四"]];
}
- (void)downMenu:(DownMenu *)menu selectChildMenu:(NSInteger)index{
    NSLog(@"%ld",index);
}
- (void)downMenuWillShow{
    NSLog(@"將要出現");
}
- (void)downMenuDidShow{
    NSLog(@"已經出現");
}
- (void)downMenuWillHidden{
    NSLog(@"將要消失");
}
- (void)downMenuDidHidden{
    NSLog(@"已經消失");
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

這里介紹了自定義DownMenu視圖的具體代碼實現過程

#import <UIKit/UIKit.h>
@class DownMenu;
@protocol DownMenuDelegate <NSObject>
// 各個選項被點擊的時候出發
- (void)downMenu:(DownMenu *)menu selectChildMenu:(NSInteger)index;
@optional
// 菜單將要出現
- (void)downMenuWillShow;
// 菜單已經出現
- (void)downMenuDidShow;
// 菜單將要消失
- (void)downMenuWillHidden;
// 菜單已經消失
- (void)downMenuDidHidden;
@end
@interface DownMenu : UIView
@property (nonatomic,assign)id<DownMenuDelegate> delegate;
// 設置菜單標題
- (void)setMainMenuTitle:(NSString *)title chlidTitleAry:(NSArray *)childTitleAry;
@end

#import "DownMenu.h"
#define KBounds self.bounds
#define KFrame self.frame
#define KSetFrameHeght(A,B) CGRectMake(A.frame.origin.x,A.frame.origin.y,A.frame.size.width,B)
#define KRowHeight 30
@interface DownMenu ()
@property (nonatomic,strong)UIButton *mainBtn;  // 主按鈕
@property (nonatomic,assign)BOOL isShow;        // 標記視圖顯示狀態
@property (nonatomic,strong)UIView *bgroundView;// 設置背景視圖
@property (nonatomic,assign)CGRect tempFrame;   // 記錄按鈕初始值
@property (nonatomic, nonnull,strong)CAShapeLayer *sanjiaoLayer;
@end
@implementation DownMenu
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup]; // 設置子視圖
        self.tempFrame = frame;
        self.isShow = NO;
    }
    return self;
}
- (CAShapeLayer *)sanjiaoLayer{
    if (!_sanjiaoLayer) {
        self.sanjiaoLayer = [self createLayer];
    }
    return _sanjiaoLayer;
}
// 設置背景視圖
- (UIView *)bgroundView{
    if (!_bgroundView) {
        self.bgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(KBounds), CGRectGetWidth(KBounds), 0)];
    }
    return _bgroundView;
}
// 設置主按鈕
- (UIButton *)mainBtn{
    if (!_mainBtn) {
        self.mainBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_mainBtn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        [_mainBtn addTarget:self action:@selector(mainBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
        _mainBtn.backgroundColor = [UIColor lightGrayColor];
        _mainBtn.frame = KBounds;
        [_mainBtn.layer addSublayer:self.sanjiaoLayer];
    }
    return _mainBtn;
}
// 設置主按鈕點擊事件
- (void)mainBtnAction:(UIButton *)sender{
    if (_isShow) {    // 如果顯示,就隱藏
        [self hiddenMenu];
    }else {          // 如果隱藏,就顯示
        [self openMenu];
    }
}
// 設置子視圖
- (void)setup{
    [self addSubview:self.mainBtn];
    [self addSubview:self.bgroundView];
    self.layer.masksToBounds = YES;
    self.bgroundView.layer.masksToBounds = YES;
    self.backgroundColor = [UIColor redColor];
}
// 設置菜單標題
- (void)setMainMenuTitle:(NSString *)title chlidTitleAry:(NSArray *)childTitleAry{
    [self.mainBtn setTitle:title forState:(UIControlStateNormal)];
    [childTitleAry enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIButton *btn = [self createChildBtnTitle:obj rect:CGRectMake(0, idx * KRowHeight, CGRectGetWidth(KFrame), KRowHeight)];
        [btn addTarget:self action:@selector(btnAction:) forControlEvents:(UIControlEventTouchUpInside)];
        btn.tag = 2000 + idx;
        [_bgroundView addSubview:btn];
    }];
}
// 設置子按鈕點擊事件
- (void)btnAction:(UIButton *)sender{
    if (!self.delegate) {NSLog(@"沒有設置代理"); return;}
    if ([self.delegate respondsToSelector:@selector(downMenu: selectChildMenu:)]) {
        [self.delegate downMenu:self selectChildMenu:sender.tag - 2000];
    }
}
// 菜單將要出現
- (void)downMenuWillShow{
    NSLog(@"222222");
    if (!self.delegate) {NSLog(@"沒有設置代理"); return;}
    if ([self.delegate respondsToSelector:@selector(downMenuWillShow)]) {
        [self.delegate downMenuWillShow];
    }
}
// 菜單已經出現
- (void)downMenuDidShow{
    if (!self.delegate) {NSLog(@"沒有設置代理"); return;}
    if ([self.delegate respondsToSelector:@selector(downMenuDidShow)]) {
        [self.delegate downMenuDidShow];
    }
}
// 菜單將要消失
- (void)downMenuWillHidden{
    if (!self.delegate) {NSLog(@"沒有設置代理"); return;}
    if ([self.delegate respondsToSelector:@selector(downMenuWillHidden)]) {
        [self.delegate downMenuWillHidden];
    }
}
// 菜單已經消失
- (void)downMenuDidHidden{
    if (!self.delegate) {NSLog(@"沒有設置代理"); return;}
    if ([self.delegate respondsToSelector:@selector(downMenuDidHidden)]) {
        [self.delegate downMenuDidHidden];
    }
}
// 展開菜單
- (void)openMenu{
    [self downMenuWillShow];
    [UIView animateWithDuration:0.3 delay:0 options:(UIViewAnimationOptionCurveEaseInOut) animations:^{
        self.frame = KSetFrameHeght(self, self.tempFrame.size.height + KRowHeight * (self.subviews[1].subviews.count));
        self.bgroundView.frame = KSetFrameHeght(self.bgroundView, KRowHeight * (self.subviews[1].subviews.count));
        self.sanjiaoLayer.transform = CATransform3DRotate(CATransform3DIdentity, M_PI, 1, 0, 0);
        self.isShow = YES;
    } completion:^(BOOL finished) {
        [self downMenuDidShow];
    }];
}
// 收回菜單
- (void)hiddenMenu{
    [self downMenuWillHidden];
    [UIView animateWithDuration:0.3 delay:0 options:(UIViewAnimationOptionCurveEaseInOut) animations:^{
        self.frame = KSetFrameHeght(self, self.tempFrame.size.height);
        self.bgroundView.frame = KSetFrameHeght(self.bgroundView, 0);
        self.sanjiaoLayer.transform = CATransform3DIdentity;
        self.isShow = NO;
    } completion:^(BOOL finished) {
        [self downMenuDidHidden];
    }];
}
// 創建一個btn
- (UIButton *)createChildBtnTitle:(NSString *)title rect:(CGRect)frame{
    UIButton *btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [btn setTitle:title forState:(UIControlStateNormal)];
    [btn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    btn.frame = frame;
    return btn;
}
// 創建小三角
- (CAShapeLayer *)createLayer{
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.position = CGPointMake(self.mainBtn.frame.size.width - 16, CGRectGetMidY(self.mainBtn.frame));
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(0, 0)];
    [path addLineToPoint:CGPointMake(10, 0)];
    [path addLineToPoint:CGPointMake(5, cos(30 * M_PI_2 / 180) * 5)];
    [path addLineToPoint:CGPointMake(0, 0)];
    layer.path = path.CGPath;
    layer.fillColor = [UIColor blackColor].CGColor;
    return layer;
}
@end

完成

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,048評論 6 542
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,414評論 3 429
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,169評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,722評論 1 317
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,465評論 6 412
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,823評論 1 328
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,813評論 3 446
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 43,000評論 0 290
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,554評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,295評論 3 358
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,513評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,035評論 5 363
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,722評論 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,125評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,430評論 1 295
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,237評論 3 398
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,482評論 2 379

推薦閱讀更多精彩內容

  • 發現 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 12,170評論 4 61
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,725評論 25 708
  • -1- 這是一個真實的故事,這個故事曾經感動了無數人。 故事發生在17年前的12月31日,也就是除夕夜,在日本札幌...
    上吊的豆腐閱讀 415評論 0 1
  • ① 我一人 守一城 等一人 ② 我一人 留一心 愛一人 ③ 我一人 孤一生 ...
    一人的城閱讀 118評論 0 0
  • 大概是六七年前吧,突然對服飾搭配情有獨衷,那時候我常常癡迷的瀏覽淘寶京東上的服飾,看那些店鋪里的服飾風格,看琳...
    紫薔薇花閱讀 1,318評論 1 6