彈出抽屜效果

最近公司項目要加個抽屜效果,完成之后自己寫了個demo,看下效果

抽屜.gif

功能實現很簡單,直接上代碼,里面都有注釋

YYListView為抽屜類

//
//  YYListView.h
//  抽屜
//
//  Created by yyMae on 16/2/23.
//  Copyright ? 2016年 yyMae. All rights reserved.
//

#import <UIKit/UIKit.h>
@interface YYListView : UIView<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *tabView;
@property (nonatomic, strong) NSMutableArray *dataSoure;
@property (nonatomic, assign) NSInteger menuIndex;
@end

//
//  YYListView.m
//  抽屜
//
//  Created by yyMae on 16/2/23.
//  Copyright ? 2016年 yyMae. All rights reserved.
//

#import "YYListView.h"
@implementation YYListView

- (NSArray *)dataSoure
{
    if (_dataSoure == nil) {
        _dataSoure = [NSMutableArray array];

    }
    return _dataSoure;
}
- (UITableView *)tabView
{
    
    if (!_tabView) {
        _tabView = [[UITableView alloc]initWithFrame:CGRectMake(0, 50, self.frame.size.width, self.frame.size.height)];
        _tabView.backgroundColor = [[UIColor alloc]initWithRed:246/255.0 green:247/255.0 blue:248/255.0 alpha:1];
        _tabView.dataSource = self;
        _tabView.delegate = self;
        _tabView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
    }
    return _tabView;
}

-(instancetype)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [[UIColor alloc]initWithRed:246/255.0 green:247/255.0 blue:248/255.0 alpha:1];
        self.menuIndex = 0;
        [self initData];
        [self initView];
    }
    
    return self;
}

//此處給你的數據源數組賦值
- (void)initData{
    NSArray *arr = @[@"顏色1",@"顏色2",@"顏色3"];
    self.dataSoure = (NSMutableArray *)arr;
}
- (void)initView{
    [self addSubview:self.tabView];
    [self.tabView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"leftCellIdent"];
    [self.tabView reloadData];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSoure.count;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"leftCellIdent"];
    cell.backgroundColor = [[UIColor alloc]initWithRed:246/255.0 green:247/255.0 blue:248/255.0 alpha:1];
    UIView *backV =[[UIView alloc]initWithFrame:cell.frame];
    backV.backgroundColor = [[UIColor alloc]initWithRed:210/255.0 green:10/255.0 blue:30/255.0 alpha:1];
    cell.selectedBackgroundView = backV;
    NSString *title = [NSString stringWithFormat:@"    %@",self.dataSoure[indexPath.row]];
    cell.textLabel.font = [UIFont systemFontOfSize:22];
    cell.textLabel.text = title;
    cell.textLabel.highlightedTextColor = [UIColor whiteColor];//設置選中時候字體為白色
    return cell;
}
#pragma mark - UITableViewDelegate
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 150)];
    view.backgroundColor = [[UIColor alloc]initWithRed:246/255.0 green:247/255.0 blue:248/255.0 alpha:1];
    UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(65, 40, 70, 70)];
    imgView.contentMode = UIViewContentModeScaleAspectFit;
    imgView.clipsToBounds = YES;
    imgView.image = [UIImage imageNamed:@"金館長"];//添加頭部圖片
    [view addSubview:imgView];
    return view;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 150;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.menuIndex = indexPath.row;
    
}

 @end


//
//  ViewController.h
//  抽屜
//
//  Created by yyMae on 16/2/26.
//  Copyright ? 2016年 yyMae. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end

//
//  ViewController.m
//  抽屜
//
//  Created by yyMae on 16/2/26.
//  Copyright ? 2016年 yyMae. All rights reserved.
//

#import "ViewController.h"
#import "YYListView.h"
@interface ViewController ()
//此處的aboveView和listView都是self.view的子視圖,注意二者有先后順序,先添加的必須是listView,當需要彈出抽屜(即listView)的時候,只要將aboveView的坐標改變就能實現了
@property (nonatomic, strong) UIView *aboveView;
@property (nonatomic, strong) YYListView *listView;

@property (nonatomic, assign) BOOL isLeft;//此屬性判斷是否已經彈出抽屜界面,初值設為NO
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.isLeft = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    self.aboveView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.aboveView.backgroundColor = [UIColor whiteColor];//此處必須設置顏色,否則默認為透明色
    self.listView = [[YYListView alloc]initWithFrame:CGRectMake(0, 0, 200, self.view.bounds.size.height)];
    //首先將listView添加到self.view
    [self.view addSubview:self.listView];
    //然后將aboveView加到self.view
    [self.view addSubview:self.aboveView];//其他子控件都添加到aboveView就可以了
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    btn.frame = CGRectMake(10, 10, 50, 50);
    [btn setTitle:@"抽屜" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(addListView) forControlEvents:UIControlEventTouchUpInside];
    [self.aboveView addSubview:btn];
    
    //給listView的menuIndex屬性添加KVO,每當值改變的時候變化aboveView的顏色
    [self.listView addObserver:self forKeyPath:@"menuIndex" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
    
}

- (void)addListView{
    //如果沒有彈出抽屜則彈出,如果已經彈出則關閉抽屜
    if (self.isLeft == NO) {
        [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:10 initialSpringVelocity:0.5 options:UIViewAnimationOptionCurveLinear animations:^{
            
            self.aboveView.frame = CGRectMake(200, 0, self.view.frame.size.width, self.view.frame.size.height);
            
        } completion:^(BOOL finished) {
            self.isLeft = YES;

        }];
        
        
    }else {
        
        [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:10 initialSpringVelocity:0.5 options:UIViewAnimationOptionCurveLinear animations:^{
            
            self.aboveView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
            
        } completion:^(BOOL finished) {
            self.isLeft = NO;
        }];
        
    }
}

//KVO觀察的值改變時候會執行此方法
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
    
    if (self.listView.menuIndex == 1) {
        self.aboveView.backgroundColor = [UIColor greenColor];
    }else if (self.listView.menuIndex == 2){
        self.aboveView.backgroundColor = [UIColor purpleColor];
    }else{
        self.aboveView.backgroundColor = [UIColor whiteColor];
    }
    
}

@end

當然大家也可以在viewController中添加手勢來控制抽屜效果


update:
添加手勢,emptyView僅為實例

點擊空白處抽屜回收
- (void)tapRecognized:(UITapGestureRecognizer*)recognizer{
if (self.isLeft == YES) {
[UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:10 initialSpringVelocity:0.5 options:UIViewAnimationOptionCurveLinear animations:^{
self.emptyView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
} completion:^(BOOL finished) {
}];

        self.isLeft = NO;
    }
}

右滑彈出,左滑消失
- (void)panRecognized:(UIPanGestureRecognizer*)recognizer
{
CGPoint translation = [recognizer translationInView:recognizer.view];
switch (recognizer.state) {
case UIGestureRecognizerStateBegan: {
break;
}
case UIGestureRecognizerStateChanged: {
if (translation.x <= 200 && self.isLeft == NO && translation.x >= 0) {
self.emptyView.frame = CGRectMake(translation.x, 0, self.view.frame.size.width, self.view.frame.size.height);
}
if (translation.x >= -200 && self.isLeft == YES && translation.x <= 0) {
self.emptyView.frame = CGRectMake(200 + translation.x, 0, self.view.frame.size.width, self.view.frame.size.height);

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

推薦閱讀更多精彩內容

  • 發現 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 12,163評論 4 61
  • 轉載自:https://github.com/Tim9Liu9/TimLiu-iOS 目錄 UI下拉刷新模糊效果A...
    袁俊亮技術博客閱讀 11,952評論 9 105
  • 也許是前幾天太累了,早上6點38分才醒過來。天還是那么黑,外面有淅淅瀝瀝的雨聲。原來,下雨了,秋雨。 喜歡下雨時的...
    HCW_7f9c閱讀 464評論 0 7
  • 提起特工,你能想到什么呢? 超群的雙商,一步跨越圍欄的大長腿,還是帥氣且多金的特質? 小p推出了線上App“征文活...
    旁趣PUNCH閱讀 298評論 0 0
  • 第十章 無名陷阱 發音縮寫 發音首字母縮寫原則 客戶選擇公司名稱,傾向于發音導向。公司是視覺導向,為了提升名字的視...
    獅子座秋秋閱讀 341評論 0 0