IOS 仿京东右侧滑出筛选页面效果

先看下效果图:
这里写图片描述

直接上代码:

#import "FilterViewController.h"

#define WEAK_SELF(weakSelf)  __weak __typeof(&*self)weakSelf = self;
#define ScreenBounds [[UIScreen mainScreen] bounds]     //屏幕frame
#define ScreenFullWidth [[UIScreen mainScreen] bounds].size.width     //屏幕宽度

@interface ViewController (){

    UILabel *showLabel;
}

@property (nonatomic, weak) UIView *upView;

@property (nonatomic, strong) UIWindow *window;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   self.title = @"主页";
    [self createBarRightWithImage:@"" :@"筛选"];

    showLabel= [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 200, 50)];
    showLabel.text = @"主页";
    showLabel.textColor = [UIColor blackColor];
    [self.view addSubview:showLabel];

}
//点击筛选按钮,出现筛选页面
- (void)showRight:(UIButton *)sender{

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenFullWidth, ScreenBounds.size.height)];
    view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    UITapGestureRecognizer *tap  = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
    [view addGestureRecognizer:tap];
    [[self mainWindow] addSubview:view];
    self.upView = view;


    UIWindow *window = [[UIWindow alloc] initWithFrame:CGRectMake(100, 0, ScreenFullWidth-100, ScreenBounds.size.height)];
    window.backgroundColor = [[UIColor clearColor] colorWithAlphaComponent:0.7];
    window.windowLevel = UIWindowLevelNormal;
    window.hidden = NO;
    [window makeKeyAndVisible];
    self.window = window;

    WEAK_SELF(weakSelf)

    FilterViewController *up = [[FilterViewController alloc] init];
    up.tap = ^{

        weakSelf.window.hidden = YES;//少这句话的就会有问题

        [weakSelf.window resignKeyWindow];
        [weakSelf.upView removeFromSuperview];

        weakSelf.window  = nil;
        weakSelf.upView = nil;


    };
    up.clickCell = ^(NSString *chooseStr, NSInteger index){

        showLabel.text = [NSString stringWithFormat:@"%@,%ld",chooseStr,(long)index];
    };

    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:up];
    up.view.frame = window.bounds;
    window.rootViewController = nav;

}

- (void)tapAction{

    [self.window resignKeyWindow];
    [self.upView removeFromSuperview];
    self.window  = nil;
    self.upView = nil;

}

看下筛选页面的代码:

//.h文件

#import <UIKit/UIKit.h>
#import "BaseViewController.h"

@interface FilterViewController : BaseViewController<UITableViewDelegate,UITableViewDataSource>
//在灰色部分点击返回上个页面的block
@property(nonatomic,copy)void(^tap)();
//选中cell后在上个页面显示带回去的值的block
@property(nonatomic,copy)void(^clickCell)(NSString *chooseStr, NSInteger index);


@end


//.m文件
#import "FilterViewController.h"

@interface FilterViewController (){

    NSArray *arrList;
}

@end

@implementation FilterViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.title = @"筛选";
    [self createBarLeftWithImage:@"backBtn.png" :@"nil"];

    arrList = [NSArray arrayWithObjects:@"大家好",@"你们好",@"他们好",@"哈哈",@"嘻嘻",@"嘎嘎", nil];
    [self createTable];
}

- (void)createTable{

    UITableView *myTable = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    myTable.delegate = self;
    myTable.dataSource = self;
    myTable.rowHeight = 44;
    [self.view addSubview:myTable];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return arrList.count;

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    cell.textLabel.text = [NSString stringWithFormat:@"%@",arrList[indexPath.row]];
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    self.clickCell(arrList[indexPath.row], indexPath.row);
    self.tap();
}
//返回按钮
-(void)showLeft:(UIButton *)sender{

    self.tap();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值