美文网首页
iOS(OC)菜单头部封装

iOS(OC)菜单头部封装

作者: MichaelDing | 来源:发表于2020-04-30 15:45 被阅读0次
    1. iOS(OC)菜单头部封装
      .h代码如下;
    @interface TitleScrollView : UIView<UIScrollViewDelegate>
    @property (nonatomic,strong) UIScrollView *titleScroller;
    @property (nonatomic,strong)UIButton *selectBtn;
    @property (nonatomic,strong)NSArray *titleArr;
    @property(nonatomic,copy)void(^clickTitleScrollView)(NSInteger);//tag
    @end
    

    .m代码如下;

    #import "TitleScrollView.h"
    
    @implementation TitleScrollView
    - (instancetype)initWithFrame:(CGRect)frame{
        
        self = [super initWithFrame:frame];
        if (self) {
            self.backgroundColor = [UIColor whiteColor];
            [self creatScrollerView];
        }
        return self;
    }
    -(void)setTitleArr:(NSArray *)titleArr{
        _titleArr = titleArr;
        for (UIView *view in self.titleScroller.subviews) {
               [view removeFromSuperview];
           }
         CGFloat totalWidth = 20;
         for (int i = 0 ; i < self.titleArr.count; i++){
          CGRect rect = [self.titleArr[i] boundingRectWithSize:CGSizeMake(MAXFLOAT, 44) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]} context:nil];
          UIButton *titleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
          titleBtn.frame = CGRectMake(totalWidth, 0, rect.size.width, 44);
          totalWidth = totalWidth + rect.size.width + 20;
          titleBtn.titleLabel.font = [UIFont systemFontOfSize:16];
          [titleBtn setTitle:self.titleArr[i] forState:UIControlStateNormal];
          [titleBtn setTitleColor:[UIColor colorWithRed:166/255.0 green:167/255.0 blue:168/255.0 alpha:1.0] forState:UIControlStateNormal];
          [titleBtn setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
          titleBtn.tag = 400 + i;
          [titleBtn addTarget:self action:@selector(titleBtnClick:) forControlEvents:UIControlEventTouchUpInside];
          [self.titleScroller addSubview:titleBtn];
          if (i == 0){
              self.selectBtn = titleBtn;
              titleBtn.selected = YES;
              UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(titleBtn.center.x-20, 41,40 , 3)];
              lineView.backgroundColor = [UIColor blueColor];
              lineView.tag = 222;
              [self.titleScroller addSubview:lineView];
          }
        }
          if (totalWidth > kScreenWidths){
              self.titleScroller.contentSize = CGSizeMake(totalWidth,44);
          }
              self.titleScroller.showsHorizontalScrollIndicator = NO;
    }
    -(void)creatScrollerView{
          self.titleScroller = [[UIScrollView alloc]initWithFrame:CGRectMake(0,0,kScreenWidths ,44)];
          self.titleScroller.backgroundColor = [UIColor whiteColor];
          self.titleScroller.layer.shadowColor = [UIColor darkGrayColor].CGColor;
          self.titleScroller.layer.shadowOffset = CGSizeMake(0, 5);
          self.titleScroller.layer.shadowOpacity = 0.3;
          self.titleScroller.layer.shadowRadius = 3;
          self.titleScroller.layer.masksToBounds = NO;
          [self addSubview:self.titleScroller];
    }
    -(void)titleBtnClick:(UIButton *)sender{
        if (sender != self.selectBtn){
            self.selectBtn.selected = NO;
            sender.selected = YES;
            self.selectBtn = sender;
            UIView *lineView = [self viewWithTag:222];
            lineView.frame = CGRectMake(sender.center.x-20, 41, 40, 3);
            if (CGRectGetMaxX(sender.frame)>kScreenWidths){
                self.titleScroller.contentOffset = CGPointMake(CGRectGetMaxX(sender.frame)-kScreenWidths+20, 0);
            }else {
                self.titleScroller.contentOffset = CGPointMake(0, 0);
            }
           if(self.clickTitleScrollView!= nil) {
               self.clickTitleScrollView(sender.tag);
           }
        }
    }
    

    2:控制器使用方法:

    #import "QTItemViewController.h"
    #import "TitleScrollView.h"
    #import "view1.h"
    #import "view2.h"
    #import "view3.h"
    #import "view4.h"
    #import "view5.h"
    #import "view6.h"
    @interface QTItemViewController ()
    @property (nonatomic,strong)NSArray *titleArr;
    @property (nonatomic,strong)TitleScrollView *titleScroller;
    @property (nonatomic,strong)UIButton *selectBtn;
    @property (nonatomic,strong)view1 *view1;
    @property (nonatomic,strong)view2 *view2;
    @property (nonatomic,strong)view3 *view3;
    @property (nonatomic,strong)view4 *view4;
    @property (nonatomic,strong)view5 *view5;
    @property (nonatomic,strong)view6 *view6;
    @end
    
    @implementation QTItemViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.navigationItem.title = @"演示demo";
        self.view.backgroundColor = [UIColor colorWithRed:243/255.0 green:246/255.0 blue:252/255.0 alpha:1.0];
        [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
        [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
        self.titleScroller = [[TitleScrollView alloc]initWithFrame:CGRectMake(0,kNavigationBarHeight,kScreenWidths,44)];
        self.titleScroller.backgroundColor = [UIColor redColor];
        __weak typeof(self) weakSelf = self;
        self.titleScroller.clickTitleScrollView = ^(NSInteger buttonIndex) {
            NSLog(@"%ld",(long)buttonIndex);
            [weakSelf clickButton:buttonIndex];
        };
        [self.view addSubview:self.titleScroller];
        [self getData];
        [self clickButton:400];
    }
    -(void)getData{
       self.titleArr = [NSArray arrayWithObjects:@"活动1",@"活动2",@"活动3",@"活动4",@"活动5", @"活动6",nil];
       self.titleScroller.titleArr = self.titleArr;
    }
    -(void)clickButton:(NSInteger)senderTag{
         if ([self.view viewWithTag:100] != nil) {
                [[self.view viewWithTag:100] removeFromSuperview];
            }
         if ([self.view viewWithTag:101] != nil) {
            [[self.view viewWithTag:101] removeFromSuperview];
         }
        if ([self.view viewWithTag:102] != nil) {
                   [[self.view viewWithTag:102] removeFromSuperview];
               }
        if ([self.view viewWithTag:103] != nil) {
           [[self.view viewWithTag:103] removeFromSuperview];
        }
        if ([self.view viewWithTag:104] != nil) {
                   [[self.view viewWithTag:104] removeFromSuperview];
               }
        if ([self.view viewWithTag:105] != nil) {
           [[self.view viewWithTag:105] removeFromSuperview];
        }
        switch (senderTag) {
            case 400:
                if (!_view1) {
                    _view1 = [[view1 alloc]initWithFrame:CGRectMake(0,44 + kNavigationBarHeight, kScreenWidth,kScreenHeight - kNavigationBarHeight)];
                    _view1.tag = 100;
                    }
                [self.view addSubview:self.view1];
                break;
            case 401:
               if (!_view2) {
                  _view2 = [[view2 alloc]initWithFrame:CGRectMake(0,44 + kNavigationBarHeight, kScreenWidth,kScreenHeight - kNavigationBarHeight)];
                  _view2.tag = 101;
                  }
              [self.view addSubview:self.view2];
                break;
            case 402:
               if (!_view3) {
                   _view3 = [[view3 alloc]initWithFrame:CGRectMake(0,44 + kNavigationBarHeight, kScreenWidth,kScreenHeight - kNavigationBarHeight)];
                   _view3.tag = 102;
                   }
               [self.view addSubview:self.view3];
               break;
           case 403:
              if (!_view4) {
                 _view4 = [[view4 alloc]initWithFrame:CGRectMake(0,44 + kNavigationBarHeight, kScreenWidth,kScreenHeight - kNavigationBarHeight)];
                 _view4.tag = 103;
                 }
             [self.view addSubview:self.view4];
               break;
           case 404:
              if (!_view5) {
                  _view5 = [[view5 alloc]initWithFrame:CGRectMake(0,44 + kNavigationBarHeight, kScreenWidth,kScreenHeight - kNavigationBarHeight)];
                  _view5.tag = 104;
                  }
              [self.view addSubview:self.view5];
              break;
            case 405:
             if (!_view6) {
                _view6= [[view6 alloc]initWithFrame:CGRectMake(0,44 + kNavigationBarHeight, kScreenWidth,kScreenHeight - kNavigationBarHeight)];
                _view6.tag = 105;
                }
            [self.view addSubview:self.view6];
              break;
            default:
                break;
        }
    }
    

    3.效果展示:


    Simulator Screen Shot - iPhone 11 Pro Max - 2020-04-30 at 15.49.37.png

    相关文章

      网友评论

          本文标题:iOS(OC)菜单头部封装

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