美文网首页
iOS - 滑动菜单(上下联动)

iOS - 滑动菜单(上下联动)

作者: 温柔vs先生 | 来源:发表于2023-06-05 16:24 被阅读0次
    //
    //  GYAIKnowledgeSearchVC.m
    //  GuiYuSiri
    //
    //  Created by wbb on 2023/6/6.
    //
    
    #import "GYAIKnowledgeSearchVC.h"
    #import "GYAIKnowledgeListVC.h"
    
    @interface GYAIKnowledgeSearchVC ()<UITextFieldDelegate, UIScrollViewDelegate>
    @property (nonatomic, strong) UIView * navBackView;
    @property (nonatomic, strong) UIScrollView * titleSC;
    @property (nonatomic, strong) UIScrollView * scrollView;
    @property (nonatomic, strong) UIButton * currentBtn;
    @property (nonatomic, copy) NSArray * titleArr;
    @property (nonatomic, strong) NSMutableDictionary *listVCQueue;
    
    @end
    
    @implementation GYAIKnowledgeSearchVC
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        _listVCQueue = [NSMutableDictionary dictionaryWithCapacity:0];
    
        [self initSearchNav];
        [self initSubViews];
    }
    /// 取消搜索
    - (void)cancelClick {
        [self.navigationController popViewControllerAnimated:YES];
    }
    
    - (void)titleBtnClick:(UIButton *)btn {
        if(btn == _currentBtn) return;
        [btn setTitleColor:hexColorAlpha(ffffff, 1) forState:UIControlStateNormal];
        btn.backgroundColor = hexColorAlpha(4349A9, 1);
        _currentBtn.backgroundColor = hexColorAlpha(ffffff, 0.1);
        [_currentBtn setTitleColor:hexColorAlpha(ffffff, 0.4) forState:UIControlStateNormal];
    
    
        [UIView animateWithDuration:0.3 animations:^{
            [self.scrollView setContentOffset:CGPointMake((btn.tag-100)*ViewWidth, self.scrollView.contentOffset.y)  animated:NO];
        }];
    
        [self addListVCWithIndex:btn.tag-100];
    
        // 有数学公式得出的算法
        if((ViewWidth - btn.x) < 2*btn.width) {
            if((btn.x - (ViewWidth -btn.width)/2.f)<(self.titleSC.contentSize.width-ViewWidth)) {
                [UIView animateWithDuration:0.5 animations:^{
                    self.titleSC.contentOffset = CGPointMake(btn.x - (ViewWidth -btn.width)/2.f, 0);
                }];
            } else {
                [UIView animateWithDuration:0.5 animations:^{
                    self.titleSC.contentOffset = CGPointMake(self.titleSC.contentSize.width - ViewWidth, 0);
                }];
            }
        } else {
            [UIView animateWithDuration:0.5 animations:^{
                self.titleSC.contentOffset = CGPointMake(0, 0);
            }];
        }
        
        
        _currentBtn = btn;
    }
    
    
    - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
        
        NSInteger index = (scrollView.contentOffset.x+ViewWidth/2.f)/ViewWidth;
        UIButton *btn = (UIButton *)[self.titleSC viewWithTag:index+100];
        [self titleBtnClick:btn];
        
        [self addListVCWithIndex:(int)(scrollView.contentOffset.x/ViewWidth)];
    }
    
    #pragma mark - addVC
    - (void)addListVCWithIndex:(NSInteger)index {
        
        if (index<0||index>=self.titleArr.count) {
            return;
        }
        //根据页数添加相对应的视图 并存入数组
        
        if (![_listVCQueue objectForKey:@(index)]) {
            GYAIKnowledgeListVC *contentVC = [[GYAIKnowledgeListVC alloc] init];
            [self addChildViewController:contentVC];
    //        TYAVLableModel *model =  self.titleArr[index];
    //        contentVC.vClass = model.name;
    
            contentVC.view.frame = CGRectMake(ViewWidth*index, 0, self.scrollView.width, self.scrollView.height);
            [self.scrollView addSubview:contentVC.view];
            
            [_listVCQueue setObject:contentVC forKey:@(index)];
        }
    }
    #pragma mark - 初始化UI
    - (void)initSearchNav {
        self.view.backgroundColor = hexColor(090e25);
    
        UIView *navBackView = [[UIView alloc] init];
        [self.view addSubview:navBackView];
        self.navBackView = navBackView;
        [navBackView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
            make.right.left.offset(0);
            make.height.offset(ScaleNum(44));
    
        }];
        
        UIButton * cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [cancelBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        cancelBtn.titleLabel.font = FontRegularSize(12);
        [cancelBtn addTarget:self action:@selector(cancelClick) forControlEvents:UIControlEventTouchUpInside];
        [navBackView addSubview:cancelBtn];
        [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.offset(-ScaleNum(12));
            make.centerY.offset(0);
            make.top.bottom.offset(0);
            make.width.offset(ScaleNum(30));
        }];
        
        
        UIView *searchBackView = [[UIView alloc] init];
        searchBackView.backgroundColor = hexColorAlpha(ffffff, 0.1);
        searchBackView.cornerRadius = 15;
        [navBackView addSubview:searchBackView];
        [searchBackView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(16*ScaleWidth);
            make.right.equalTo(cancelBtn.mas_left).offset(-10*ScaleWidth);
            make.height.offset(ScaleNum(30));
            make.centerY.offset(0);
        }];
        
        UIImageView *searchImgView = [[UIImageView alloc] initWithImage:ImageWithFileName(@"AIKnowledge_search_icon")];
        [searchBackView addSubview:searchImgView];
        [searchImgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(ScaleNum(10));
            make.centerY.offset(0);
            make.width.height.offset(ScaleNum(16));
        }];
        
        UITextField *textField = [[UITextField alloc] init];
        textField.backgroundColor = [UIColor clearColor];
        NSString *title = @"搜索热门关键词";
        NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:title];
        [attStr addAttribute:NSForegroundColorAttributeName value:rgba(255, 255, 255, 0.35) range:NSMakeRange(0, title.length)];
        textField.attributedPlaceholder = attStr;
        textField.textColor = rgba(255, 255, 255, 1);
        textField.font = FontRegularSize(12);
        textField.delegate = self;
        textField.returnKeyType = UIReturnKeySearch;
        [searchBackView addSubview:textField];
        [textField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(searchImgView.mas_right).offset(ScaleNum(9));
            make.centerY.offset(0);
            make.right.offset(-ScaleNum(10));
        }];
    }
    - (void)initSubViews {
        /// 头部标题scrollView
        UIScrollView *topTitleSC = [[UIScrollView alloc] init];
        topTitleSC.showsHorizontalScrollIndicator = NO;
        topTitleSC.showsVerticalScrollIndicator = NO;
        [self.view addSubview:topTitleSC];
        self.titleSC = topTitleSC;
        [topTitleSC mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.navBackView.mas_bottom).offset(ScaleNum(10));
            make.left.equalTo(self.view.mas_safeAreaLayoutGuideLeft);
            make.right.equalTo(self.view.mas_safeAreaLayoutGuideRight);
            make.height.offset(ScaleNum(28));
        }];
        
        CGFloat jianGe = ScaleNum(16);
        
        UIButton *selectBtn = nil;
        for (int i =0; i<self.titleArr.count; i++) {
            NSString *title = [NSString stringWithFormat:@"  %@  ", self.titleArr[i]];
            UIButton * telementBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            telementBtn.backgroundColor = hexColorAlpha(ffffff, 0.1);
            [telementBtn setTitle:title forState:UIControlStateNormal];
            [telementBtn setTitleColor:hexColorAlpha(ffffff, 0.4) forState:UIControlStateNormal];
            telementBtn.titleLabel.font = FontRegularSize(13);
            telementBtn.cornerRadius = ScaleNum(4);
            telementBtn.tag = 100 + i;
            [telementBtn addTarget:self action:@selector(titleBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            [topTitleSC addSubview:telementBtn];
    //        telementBtn.frame = CGRectMake((jianGe+btnWidth)*i+jianGe, 0, btnWidth, scHeight-1);
    
            
            if (i == 0) {
                [telementBtn setTitleColor:hexColorAlpha(ffffff, 1) forState:UIControlStateNormal];
                telementBtn.backgroundColor = hexColorAlpha(4349A9, 1);
                self.currentBtn = telementBtn;
            }
            
            [telementBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                if(selectBtn) {
                    make.left.equalTo(selectBtn.mas_right).offset(jianGe);
                    make.top.equalTo(selectBtn);
                }else {
                    make.left.offset(jianGe);
                    make.top.offset(0);
                }
            }];
           
            selectBtn = telementBtn;
        }
        
        
        /// 内容scrollView
        [self.view addSubview:self.scrollView];
        [self.view sendSubviewToBack:self.scrollView];
        [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.titleSC.mas_bottom).offset(ScaleNum(5));
            make.left.right.bottom.offset(0);
        }];
        
        [self.view setNeedsLayout];
        [self.view layoutIfNeeded];
        
        topTitleSC.contentSize = CGSizeMake(selectBtn.right+jianGe, topTitleSC.height);
    
        [self addListVCWithIndex:0];
    }
    - (void)viewDidLayoutSubviews {
        [super viewDidLayoutSubviews];
    
        self.scrollView.contentSize = CGSizeMake(self.titleArr.count*ViewWidth, self.scrollView.height);
    }
    #pragma mark - getter
    
    - (UIScrollView *)scrollView {
        if (!_scrollView) {
            _scrollView = [[UIScrollView alloc] init];
            _scrollView.delegate = self;
            _scrollView.pagingEnabled = YES;
            _scrollView.showsHorizontalScrollIndicator = NO;
            _scrollView.showsVerticalScrollIndicator = NO;
            
        }
        return  _scrollView;
    }
    - (NSArray *)titleArr {
        if(!_titleArr) {
            _titleArr = @[@"全部",@"全部",@"全部",@"全部全部全部",@"全部全部全部",@"全部全部全部",@"全部全部全部",];
        }
        return _titleArr;
    }
    @end
    
    

    相关文章

      网友评论

          本文标题:iOS - 滑动菜单(上下联动)

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