侧边栏筛选

作者: 今晚月色 | 来源:发表于2018-08-21 15:40 被阅读198次
    宝儿姐镇楼图

    废话不多说直接上代码
    一、头文件

    #import <UIKit/UIKit.h>
    
    @interface SliderCategoryView : UIView
    
    - (void)showView;
    
    @end
    

    二、实现文件

    #import "SliderCategoryView.h"
    #import "HomeTableView.h"
    
    @interface SliderCategoryView  ()<UITableViewDelegate, UITableViewDataSource, UIGestureRecognizerDelegate>
    
    @property (nonatomic, strong) UITableView *tableView;
    @property (nonatomic, strong) HomeTableView *homeTableView;
    
    @end
    
    @implementation SliderCategoryView
    
    - (instancetype)init{
    
        self = [super init];
        if(self){
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissView)];
            tap.delegate = self;
            [self addGestureRecognizer:tap];
            [self setupSubviews];
        }
        return self;
    }
    
    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
        
        if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
            return NO;
        } else if ([NSStringFromClass([touch.view class]) isEqualToString:@"UICollectionViewCellContentView"]) {
            return NO;
        }
        return YES;
    }
    
    - (void)showView {
        
        self.frame = CGRectMake(UIScreen.mainScreen.bounds.size.width, 0, UIScreen.mainScreen.bounds.size.width, UIScreen.mainScreen.bounds.size.height);
        self.alpha = 0;
        [UIView animateWithDuration:0.8 animations:^{
            self.frame = CGRectMake(0, 0, UIScreen.mainScreen.bounds.size.width, UIScreen.mainScreen.bounds.size.height);
            [[[[UIApplication sharedApplication] delegate] window] addSubview:self];
            self.alpha = 1;
        }];
    }
    
    - (void)dismissView {
    
        [UIView animateWithDuration:0.8 animations:^{
            self.frame = CGRectMake(UIScreen.mainScreen.bounds.size.width, 0, UIScreen.mainScreen.bounds.size.width, UIScreen.mainScreen.bounds.size.height);
            self.alpha = 0;
        } completion:^(BOOL finished) {
             [self removeFromSuperview];
        }];
    }
    
    #pragma mark - SetupSubviewsUI
    - (void)setupSubviews{
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.4];
        // 默认加载显示TableView
        [self addSubview:self.tableView];
    }
    
    - (HomeTableView *)homeTableView {
        if (!_homeTableView) {
            _homeTableView = [[HomeTableView alloc] init];
        }
        return _homeTableView;
    }
    
    - (UITableView *)tableView {
        if (!_tableView) {
            _tableView = [[UITableView alloc] init];
            _tableView.delegate = self;
            _tableView.dataSource = self;
            _tableView.backgroundColor = UIColor.whiteColor;
            _tableView.frame = CGRectMake(UIScreen.mainScreen.bounds.size.width/5, 0, UIScreen.mainScreen.bounds.size.width * 4/5, UIScreen.mainScreen.bounds.size.height);
        }
        return _tableView;
    }
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        return 10;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return 10;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        }
        
        cell.textLabel.text = [NSString stringWithFormat:@"%ld+%ld",indexPath.section,indexPath.row];
        
        return cell;
    }
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        self.homeTableView.frame = CGRectMake(UIScreen.mainScreen.bounds.size.width, 0, UIScreen.mainScreen.bounds.size.width * 4/5, UIScreen.mainScreen.bounds.size.height);
        
        [UIView animateWithDuration:0.5 animations:^{
            self.homeTableView.frame = CGRectMake(UIScreen.mainScreen.bounds.size.width/5, 0, UIScreen.mainScreen.bounds.size.width * 4/5, UIScreen.mainScreen.bounds.size.height);
            [self addSubview:self.homeTableView];
        } completion:^(BOOL finished) {
             
        }];
    }
    
    @end
    

    HomeTableView代码

    #import "HomeTableView.h"
    
    @interface HomeTableView  ()<UITableViewDelegate, UITableViewDataSource>
    
    @end
    
    @implementation HomeTableView
    
    - (instancetype)init{
    
        self = [super init];
        if(self){
            self.delegate = self;
            self.dataSource = self;
            self.backgroundColor = UIColor.whiteColor;
        }
        return self;
    }
    
    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
        return 45;
    }
    
    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
        UIView *view = [[UIView alloc] init];
        view.backgroundColor = UIColor.redColor;
        
        UIButton *button = [[UIButton alloc] init];
        [button setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
        button.frame = CGRectMake(15, 45/2-30/2, 30, 30);
        button.imageView.contentMode = UIViewContentModeScaleAspectFit;
        [button addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:button];
        return view;
    }
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        return 1;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return 10;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        }
        
        cell.textLabel.text = [NSString stringWithFormat:@"%ld+%ld",indexPath.section,indexPath.row];
        
        return cell;
    }
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        [self dismiss];
    }
    
    - (void)dismiss {
        [UIView animateWithDuration:0.5 animations:^{
            self.frame = CGRectMake(UIScreen.mainScreen.bounds.size.width, 0, UIScreen.mainScreen.bounds.size.width * 4/5, UIScreen.mainScreen.bounds.size.height);
        } completion:^(BOOL finished) {
            [self removeFromSuperview];
        }];
    }
    

    Swift版本(其实差别不大)

    import UIKit
    
    class SliderCategory_Swift: UIView {
        
        private lazy var tableView:UITableView = {
            let tableView = UITableView.init()
            tableView.backgroundColor = UIColor.white
            tableView.delegate = self
            tableView.dataSource = self
            tableView.frame = CGRect(x: UIScreen.main.bounds.width/5, y: 0, width: UIScreen.main.bounds.width*4/5, height: UIScreen.main.bounds.height)
            return tableView
        }()
        
        private lazy var homeTableView:HomeTableView = {
            let tableView = HomeTableView.init(frame: CGRect(x: UIScreen.main.bounds.width/5, y: 0, width: UIScreen.main.bounds.width*4/5, height: UIScreen.main.bounds.height), style: .grouped)
            return tableView
        }()
    
        public func showView() {
            
            self.frame = CGRect(x: UIScreen.main.bounds.width, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.size.height)
            self.alpha = 0
            
            UIView.animate(withDuration: 0.8) {
                self.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.size.height)
                UIApplication.shared.delegate?.window??.addSubview(self)
                self.alpha = 1
            }
        }
        
        override init(frame: CGRect) {
            super.init(frame: frame)
            self.backgroundColor = UIColor.init(white: 0, alpha: 0.3)
            let tap:UITapGestureRecognizer = UITapGestureRecognizer.init(target: self, action: #selector(dismissView))
            tap.delegate = self
            self .addGestureRecognizer(tap)
            
            /// 默认加载一个tableView
            self.addSubview(tableView)
        }
        
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
        
        @objc private func dismissView() {
            UIView.animate(withDuration: 0.8, animations: {
                self.frame = CGRect(x: UIScreen.main.bounds.width, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.size.height)
                self.alpha = 0
            }) { (bool) in
                self.removeFromSuperview()
            }
        }
    }
    
    extension SliderCategory_Swift: UIGestureRecognizerDelegate {
        func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
            print(NSStringFromClass((touch.view?.classForCoder)!))
            if NSStringFromClass((touch.view?.classForCoder)!) == "UITableViewCellContentView" {
                return false
            } else if NSStringFromClass((touch.view?.classForCoder)!) == "UICollectionViewCellContentView" {
                return false
            } else {
                return true
            }
        }
    }
    
    extension SliderCategory_Swift: UITableViewDelegate, UITableViewDataSource {
        func numberOfSections(in tableView: UITableView) -> Int {
            return  1
        }
        
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return 10
        }
        
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
            if cell == nil {
                cell = UITableViewCell.init(style: .default, reuseIdentifier: "cell")
            }
            
            cell?.textLabel?.text = "\(indexPath.row+1)"
            
            return cell!
        }
        
        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            
            self.homeTableView.frame = CGRect(x: UIScreen.main.bounds.width, y: 0, width: UIScreen.main.bounds.width*4/5, height: UIScreen.main.bounds.size.height)
            UIView.animate(withDuration: 0.5) {
                self.homeTableView.frame = CGRect(x: UIScreen.main.bounds.width/5, y: 0, width: UIScreen.main.bounds.width*4/5, height: UIScreen.main.bounds.size.height)
                self.addSubview(self.homeTableView)
            }
        }
    }
    

    二、HomeTableView代码

    import UIKit
    
    class HomeTableView: UITableView {
    
        override init(frame: CGRect, style: UITableViewStyle) {
            super.init(frame: frame, style: style)
            self.delegate = self
            self.dataSource = self
        }
        
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
        
        @objc private func dismissView() {
            UIView.animate(withDuration: 0.5, animations: {
                self.frame = CGRect(x: UIScreen.main.bounds.width, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.size.height)
            }) { (bool) in
                self.removeFromSuperview()
            }
        }
    }
    
    extension HomeTableView: UITableViewDelegate, UITableViewDataSource {
        
        func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
            let view = UIView.init()
            view.backgroundColor = UIColor.red
            let button = UIButton.init()
            button.setImage(UIImage.init(named: "back"), for: .normal)
            button.frame = CGRect(x: 15, y: 15, width: 30, height: 30)
            button.imageView?.contentMode = .scaleAspectFit
            button.addTarget(self, action: #selector(dismissView), for: .touchUpInside)
            view.addSubview(button)
            return view
        }
        
        func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
            return 45
        }
        
        func numberOfSections(in tableView: UITableView) -> Int {
            return  1
        }
        
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return 10
        }
        
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
            if cell == nil {
                cell = UITableViewCell.init(style: .default, reuseIdentifier: "cell")
            }
            
            cell?.textLabel?.text = "\(indexPath.row+1)"
            
            return cell!
        }
        
        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
             dismissView()
        }
    }
    
    

    效果图

    效果图.gif
    Demo地址
    https://github.com/wudan-ios/SliderCategory.git

    相关文章

      网友评论

      • 3ec27b8a259a:寻找ios马甲包上架大神,有意私聊1916699999
      • seasonZhu:想问一下 你这个的应用场景是怎么样的 特别是跳转到第二个tableView是什么目的呢?
        今晚月色:@seasonZhu 没事 没事 有时间可以多交流交流
        seasonZhu:@梦里落雪 好的 谢谢 因为看的都是空数据 所以突然一看例子 没明白 谢谢
        今晚月色:这个就和京东的那个分类类似。跳转到第二个tableView目的:举个例子吧,商品分类一开始只显示热门的数据,需要显示全部数据的时候,如果在一个TableView里面全部展开,可能看起来就太多了。

      本文标题:侧边栏筛选

      本文链接:https://www.haomeiwen.com/subject/bcljiftx.html