zoukankan      html  css  js  c++  java
  • IOS 类似网易新闻客户端内容滚动菜单跟随居中组件

    需求分析:

      1、类似网易新闻客户端页面滚动组件、菜单栏对应菜单项一直居中

      2、点击菜单栏可以切换到对应的page

      3、滑动页面可以自动切换相应的菜单、并且对应的菜单栏居中显示

      4、初始化时可以自定义菜单项

      5、菜单内容、页面内容自定义

    设计实现:

      1、菜单和页面内容分别设计为两个横向滚动的UITableView,将两个横向滚动的tableView 放置在一个UIView中

      2、点击菜单栏时响应该事件,并将页面切换到响应页

      3、内容页面滚动停止时,将对应菜单项滚动到中间

    实现效果:

      

    四、关键代码

      1、横向的tableview 如何实现

      

    - (UITableView *)topMenuTableView
    {
        if(nil == _topMenuTableView)
        {
            CGFloat topMenuHeight = TopTabControl_Default_TopMenuHeight;
            if([self.datasource respondsToSelector:@selector(TopTabHeight:)])
            {
                topMenuHeight = [self.datasource TopTabHeight:self];
            }
            
            //before rotate bounds = (0, 0, width, height)  , rototate -90 bounds = (0, 0, height, width)
            CGFloat x = CGRectGetWidth(self.frame)/2 - topMenuHeight/2;
            CGFloat y = -CGRectGetWidth(self.frame)/2 + topMenuHeight/2;
            CGRect topMenuRect = CGRectMake(x, y, topMenuHeight, CGRectGetWidth(self.frame));
            _topMenuTableView = [[UITableView alloc] initWithFrame:topMenuRect
                                                             style:UITableViewStylePlain];
            [self addSubview:_topMenuTableView];
            _topMenuTableView.backgroundColor = [UIColor randomColor];
            _topMenuTableView.dataSource = self;
            _topMenuTableView.delegate = self;
            _topMenuTableView.showsVerticalScrollIndicator = NO;
            _topMenuTableView.transform = CGAffineTransformMakeRotation(-M_PI / 2);
            
        }
        return _topMenuTableView;
    }
    
    
    - (UITableView *)contentTableView
    {
        if(nil == _contentTableView)
        {
            CGFloat contentHeight = CGRectGetWidth(self.frame);
            CGFloat contentWidth  = CGRectGetHeight(self.frame) - [self getMenuHeight];
            CGFloat x = CGRectGetWidth(self.frame)/2 - contentWidth/2;
            CGFloat y = (CGRectGetHeight(self.frame) - [self getMenuHeight])/2 - contentHeight/2 + ([self getMenuHeight]);
            CGRect contentRect = CGRectMake(x, y, contentWidth, contentHeight);
            _contentTableView = [[UITableView alloc] initWithFrame:contentRect
                                                             style:UITableViewStylePlain];
            [self addSubview:_contentTableView];
            
            _contentTableView.backgroundColor = [UIColor randomColor];
            _contentTableView.dataSource = self;
            _contentTableView.delegate = self;
            _contentTableView.showsVerticalScrollIndicator = NO;
            _contentTableView.pagingEnabled = YES;
            _contentTableView.transform = CGAffineTransformMakeRotation(-M_PI / 2);
        }
        
        return _contentTableView;
    }

      2、协议接口

    /** @brief TopTabControl datasource 需要支持的协议 */
    @protocol TopTabControlDataSource<NSObject>
    
    @optional
    
    /**
     *  得到顶部菜单栏的高度
     *
     *  @param tabCtrl tab控件
     *
     *  @return 高度(像素)
     */
    - (CGFloat)TopTabHeight:(TopTabControl *)tabCtrl;
    
    /**
     *  得到顶部菜单栏的宽度
     *
     *  @param tabCtrl tab控件
     *
     *  @return 高度(像素)
     */
    - (CGFloat)TopTabWidth:(TopTabControl *)tabCtrl;
    
    
    /**
     *  得到顶部菜单的个数
     *
     *  @param tabCtrl tab控件
     *
     *  @return 返回菜单的个数
     */
    - (NSInteger)TopTabMenuCount:(TopTabControl *)tabCtrl;
    
    
    
    /**
     *  得到第几个菜单的view
     *
     *  @param tabCtrl tab控件
     *  @param index   菜单的index,从0开始
     *
     *  @return 返回单个菜单项
     */
    - (TopTabMenuItem *)TopTabControl:(TopTabControl *)tabCtrl
                          itemAtIndex:(NSUInteger)index;
    
    
    /**
     *  得到第几个菜单对应的page内容
     *
     *  @param tabCtrl tab控件
     *  @param index   菜单的index,从0开始
     *
     *  @return 返回单个菜单页
     */
    - (TopTabPage *)TopTabControl:(TopTabControl *)tabCtrl
                          pageAtIndex:(NSUInteger)index;
    
    @end

    五、完整代码

     https://github.com/liqiushui/TopTabControl

  • 相关阅读:
    小tips:JS之for in、Object.keys()和Object.getOwnPropertyNames()的区别
    Promise原理讲解 && 实现一个Promise对象 (遵循Promise/A+规范)
    【笔记】原生JS实现的DOM操作
    HTML5利用canvas,把多张图合并成一张图片
    大数据之Zookeeper:zookeeper数据结构、zookeeper安装、zookeeper内部原理、分布式zookeeper部署、命令行、zookeeper的API、监听服务器动态上下线案例
    大数据Zookeeper系列之Zookeeper分布式协调服务部署
    HBase安装和基础编程
    【Hadoop入门学习系列之六】HBase基本架构、编程模型和应用案例
    【Hadoop入门学习系列之五】MapReduce 2.0编程实战
    【Hadoop入门学习系列之四】MapReduce 2.0应用场景和原理、基本架构和编程模型
  • 原文地址:https://www.cnblogs.com/doudouyoutang/p/4183415.html
Copyright © 2011-2022 走看看