iOS 低配導航欄下拉菜單

輪子太多,擼了個基礎樣式,大家自己想要什么效果可以自己繼續(xù)改造一下...
效果如下:

SpingPop.gif

個人一般追求代碼即注釋...不多做說明

#import <UIKit/UIKit.h>

@class TYNavigationDropdownMenuView;
@protocol TYNavigationDropdownMenuViewDelegate <NSObject>

@optional
- (void)ty_navigationDropdownMenuView:(TYNavigationDropdownMenuView *)view didSelectText:(NSString *)text;

@end

@interface TYNavigationDropdownMenuView : UIView

// 內容數組
@property (nonatomic, strong) NSArray<NSString *> *menuContextArray;
// 選項高度
@property (nonatomic, assign) CGFloat rowHeight;
// 是否已經顯示
@property (nonatomic, assign) BOOL isShow;
@property (nonatomic, weak) id<TYNavigationDropdownMenuViewDelegate> delegate;

- (void)showInView:(UIView *)view;
- (void)dismiss;

@end

#import "TYNavigationDropdownMenuView.h"

@interface TYNavigationDropdownMenuView () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UIView *backgroundView;
@property (nonatomic, strong) UITableView *containerTableView;

@end

@implementation TYNavigationDropdownMenuView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.menuContextArray = [NSArray array];
        self.rowHeight = 30;
        [self setupBaseView];
    }
    return self;
}

- (void)setupBaseView {

    self.backgroundView = ({
        UIView *backgroundView = [[UIView alloc] initWithFrame:self.bounds];
        backgroundView.backgroundColor = [UIColor blackColor];
        backgroundView.alpha = 0.f;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backgroundViewDidTapped:)];
        [backgroundView addGestureRecognizer:tap];
        
        [self addSubview:backgroundView];
        backgroundView;
    });
    
    self.containerTableView = ({
        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 0) style:UITableViewStylePlain];
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.rowHeight = _rowHeight;
        tableView.showsVerticalScrollIndicator = NO;
        tableView.scrollEnabled = NO;
        [self addSubview:tableView];
        tableView;
    });
}


#pragma mark - Public Methods
- (void)showInView:(UIView *)view {
    [view addSubview:self];
    self.isShow = YES;
    
    CGFloat height = _menuContextArray.count * _rowHeight;
    // 默認,導航欄要是非透明的,這樣 tableview 才可以藏到導航欄下面不被發(fā)現,這個時候 當前 view 的坐標(0,0)是從導航欄下面開始,而不是狀態(tài)欄
    self.containerTableView.frame = CGRectMake(0, -height, [UIScreen mainScreen].bounds.size.width, height);
    [self.containerTableView reloadData];
    dispatch_async(dispatch_get_main_queue(), ^{
        // 結束所有當前響應..比如彈出的鍵盤..
        [view endEditing:YES];
        CGFloat y = 0;
        CGRect endFrame = self.containerTableView.frame;
        endFrame.origin.y = y;
        
        [UIView animateWithDuration:0.6
                              delay:0
             usingSpringWithDamping:0.8
              initialSpringVelocity:2
                            options:UIViewAnimationOptionCurveEaseInOut
                         animations:^{
                             self.backgroundView.alpha = 0.2f;
                             self.containerTableView.frame = endFrame;
                         } completion:nil];
    });
}

- (void)dismiss {
    self.isShow = NO;
    dispatch_async(dispatch_get_main_queue(), ^{
        CGFloat y = - self.containerTableView.frame.size.height;
        CGRect endFrame = self.containerTableView.frame;
        endFrame.origin.y = y;
        
        [UIView animateWithDuration:0.3
                              delay:0
                            options:UIViewAnimationOptionCurveEaseInOut
                         animations:^{
                             self.backgroundView.alpha = 0.f;
                             self.containerTableView.frame = endFrame;
                         } completion:^(BOOL finished) {
                             [self removeFromSuperview];
                         }];
    });
}


#pragma mark - UITableViewDataSource Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _menuContextArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdent = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdent];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdent];
    }
    cell.textLabel.text = _menuContextArray[indexPath.row];
    return cell;
}


#pragma mark - UITableViewDelegate Methods
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return _rowHeight;
}

- (void)tableView:(nonnull UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if ([_delegate respondsToSelector:@selector(ty_navigationDropdownMenuView:didSelectText:)]) {
        [_delegate ty_navigationDropdownMenuView:self didSelectText:_menuContextArray[indexPath.row]];
    }
    [self dismiss];
}


#pragma mark - Action Events
- (void)backgroundViewDidTapped:(UITapGestureRecognizer *)gesture{
    [self dismiss];
}

- (void)dealloc {
    NSLog(@"%@-釋放了",self.class);
}

@end

收工.....

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,291評論 25 708
  • 獻給我深愛的女主角宮園薰。 “今生無悔入四月,來生愿做友人A”。
    KellyGor閱讀 401評論 6 0
  • 閨蜜是什么? 閨蜜 是可以讓你依賴的人 閨蜜 是可以讓你傾訴煩惱的人 閨蜜 是在你困難時不會丟下你的人 閨蜜 是為...
    長不大的楠寶閱讀 185評論 0 1
  • 《摔跤吧,爸爸》目前是各大影院最熱賣的作品,沒有華麗的場景制作,沒有狗血的劇情,有的是改變家庭,改變整個民族精神力...
    5c155d5391ac閱讀 1,351評論 0 51
  • 最近熱播的連續(xù)劇《我的前半生》,讓原著作者亦舒又一次成為話題王。 亦舒是女作家中少有活得真實,活得最接近地氣和充滿...
    熟年與君共閱讀 538評論 0 1

友情鏈接更多精彩內容