仿网易滑动界面+仿安卓顶部滑动按钮

来源:互联网 发布:阿里云空间代理 编辑:程序博客网 时间:2024/04/29 21:44




                本文是基于实现的  HMSegmentedControl,仅仅是对初学者提供一个小小的思路

          

              HMSegmentedControl的链接     https://github.com/HeshamMegid/HMSegmentedControl

    



        {
              HMSegmentedControl *_copytopAndroid;//仿安卓顶部按钮栏
              HMSegmentedControl *_copyNeteaseSideslip;//仿网易顶部
              UIScrollView *_scrollVierw;
      }




   //仿安卓顶部按钮
    
    //初始化的数组,可以根据需求改变,根据最长的来确定长度
    _copytopAndroid = [[HMSegmentedControl alloc] initWithSectionTitles:@[@"热点",@"体育",@"头条",@"娱乐",@"直播",@"汽车",@"轻松一刻",@"时尚"]];
    
//    _copytopAndroid.backgroundColor = [UIColor redColor];
    
    //目测是自动适配啥的,具体不知道
    _copytopAndroid.autoresizingMask = UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleWidth;
    
    //设置按钮栏的frame
    _copytopAndroid.frame = CGRectMake(20, 64+50, self.view.frame.size.width-20*2, 40);
    
    //设置文字距离左右上下的高度,
    _copytopAndroid.segmentEdgeInset = UIEdgeInsetsMake(0, 10, 0, 10);
    
    //选择条(底部滑动的那个)的样式
    _copytopAndroid.selectionStyle = HMSegmentedControlSelectionStyleFullWidthStripe;
    
    //选择条的具体位置(上部或者下部)
    _copytopAndroid.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown;
    
    _copytopAndroid.selectionIndicatorHeight =3;
    
    _copytopAndroid.selectionIndicatorColor = [UIColor redColor];
    
    //是否显示两个按钮之间的线
    _copytopAndroid.verticalDividerEnabled = YES;
    
    //设置两个按钮之间线的颜色
    _copytopAndroid.verticalDividerColor = [UIColor blackColor];
    
    //两个按钮之间线的粗细
    _copytopAndroid.verticalDividerWidth = 1.0f;
    
    //设置按钮的属性
    NSDictionary *defaults_nomal = @{
                                     NSFontAttributeName : [UIFont systemFontOfSize:15.0f],
                                     NSForegroundColorAttributeName :[UIColor grayColor],
                                     };
    NSMutableDictionary *resultingAttrs_nomal = [NSMutableDictionary dictionaryWithDictionary:defaults_nomal];
    _copytopAndroid.titleTextAttributes=resultingAttrs_nomal;
    
    //设置选中之后按钮的属性
    NSDictionary *defaults = @{
                               NSFontAttributeName : [UIFont systemFontOfSize:15.0f],
                               NSForegroundColorAttributeName :[UIColor redColor],
                               };
    NSMutableDictionary *resultingAttrs = [NSMutableDictionary dictionaryWithDictionary:defaults];
    _copytopAndroid.selectedTitleTextAttributes=resultingAttrs;
    
    //添加事件
    [_copytopAndroid addTarget:self action:@selector(selectedbtn:) forControlEvents:UIControlEventValueChanged];
    
    [self.view addSubview:_copytopAndroid];
    
    
     //仿网易侧滑界面
    
    _copyNeteaseSideslip = [[HMSegmentedControl alloc] initWithSectionTitles:@[@"今日头条",@"开心一刻",@"经济要闻",@"其他"]];
   
    _copyNeteaseSideslip.autoresizingMask = UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleWidth;
    _copyNeteaseSideslip.frame = CGRectMake(20, 64+50+100, self.view.frame.size.width-20*2, 40);
    _copyNeteaseSideslip.segmentEdgeInset = UIEdgeInsetsMake(0, 10, 0, 10);
    _copyNeteaseSideslip.selectionStyle = HMSegmentedControlSelectionStyleFullWidthStripe;
    _copyNeteaseSideslip.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown;
    _copyNeteaseSideslip.selectionIndicatorHeight =3;
    _copyNeteaseSideslip.selectionIndicatorColor = [UIColor redColor];
    _copyNeteaseSideslip.verticalDividerEnabled = NO;
    _copyNeteaseSideslip.verticalDividerColor = [UIColor blackColor];
    _copyNeteaseSideslip.verticalDividerWidth = 1.0f;
    _copyNeteaseSideslip.titleTextAttributes=resultingAttrs_nomal;
    _copyNeteaseSideslip.selectedTitleTextAttributes=resultingAttrs;
    _copyNeteaseSideslip.tag = 1;
    [_copyNeteaseSideslip addTarget:self action:@selector(selectedbtn:) forControlEvents:UIControlEventValueChanged];

    [self.view addSubview:_copyNeteaseSideslip];
    
    
    _scrollVierw = [[UIScrollView alloc] initWithFrame:CGRectMake(20, 64+50+100+40, self.view.frame.size.width-20*2, 200)];
    _scrollVierw.pagingEnabled = YES;
    _scrollVierw.scrollEnabled = YES;
    _scrollVierw.delegate =self;
    _scrollVierw.showsVerticalScrollIndicator = NO;
    _scrollVierw.contentSize = CGSizeMake((self.view.frame.size.width-20*2)*4, 200);
    [self.view addSubview:_scrollVierw];
    
    
    for (int i =0; i<4; i++) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake((self.view.frame.size.width-20*2)*i, 0, (self.view.frame.size.width-20*2), 200)];
        view.backgroundColor = [UIColor colorWithRed:(100+20*i)/255.0 green:(150+20*i)/255.0 blue:(200+20*i)/255.0 alpha:1];
        [_scrollVierw addSubview:view];
    }




          下载链接 我写的一个小demo         http://download.csdn.net/detail/zhanniuniu/9385968



1 0
原创粉丝点击