美文网首页iOS-布局动画
模仿今日头条滚动界面(多种使用场景封装)

模仿今日头条滚动界面(多种使用场景封装)

作者: 摸着石头过河_崖边树 | 来源:发表于2017-05-16 16:16 被阅读1634次

    前言###

    随着swift3.0推出之后,我们广大的iOS同胞都慢慢的走向swift开发,当然小编也不例外,也在静下心来了解了解swift,但是单纯的这样看文档那也不是一个好办法,因为对于我来讲,代码才是最亲切的朋友,所以索性就是直接看代码、写代码最直接。

    代码下载地址:
    swift版本:高仿今日头条、网易新闻UI结构
    OC版本:高仿今日头条

    正文#####

    小编琢磨着,在OC中我们经常用到一些经典的UI结构(都不知道写了多少次了)都是已经封装好的,那么以后在使用swift的时候,不是也是需要这样封装好的UI结构吗?反正逻辑都是差不多的,只是写的方式改改,下面是多种使用场景的效果图


    高仿今日头条01.gif
    高仿今日头条02.gif
    高仿今日头条03.gif

    实现可以设置的功能有:
    1、可以标题栏是否可以滚动,注意标题都是滚动到中心位置

    var  pageStyleModel = LZBPageStyleModel()
    
        pageStyleModel.isScrollEnable = true
    

    调节选中的标题到中心位置

     //调整位置
    
     func adjustTargetOffset(){
        let  targetLabel = self.titleLabels[currentIndex]   //当前选中的标题的索引
        var  offsetX = targetLabel.center.x - scrollView.bounds.width * 0.5
        if offsetX < 0{
            offsetX = 0
        }
        let  maxOffsetX = self.scrollView.contentSize.width - scrollView.bounds.width
        if offsetX > maxOffsetX{
            offsetX = maxOffsetX
        }
        scrollView.setContentOffset(CGPoint(x: offsetX, y: 0), animated: true)
    }
    

    2、可以设置是否缩放选中标题

    //4.拖动contentView,改变字体大小
    
        if self.style.isNeedScale {
            let deltaScale = self.style.maxScale - 1.0
            soureLabel.transform = CGAffineTransform(scaleX: self.style.maxScale - deltaScale * progress, y: self.style.maxScale - deltaScale * progress)
            targetLabel.transform =  CGAffineTransform(scaleX: 1.0 + deltaScale * progress, y: 1.0 + deltaScale * progress)
        }
    

    3、可以设置是否下滑线长度与文字对齐

    let deltaWidth = targetLabel.frame.width - soureLabel.frame.width
    
        let deltaX = targetLabel.frame.origin.x - soureLabel.frame.origin.x
        //3.拖动contentView,下划线渐变
        if self.style.isShowBottomLine {
            bottomLine.frame.size.width = soureLabel.frame.width + deltaWidth * progress
            bottomLine.frame.origin.x = soureLabel.frame.origin.x + deltaX * progress
        }
    

    4.可以设置文字颜色渐变

    //2.颜色渐变
    
        soureLabel.textColor = UIColor(r: (self.selectColor.0 - deltaColor.0) * progress, g: (self.selectColor.1 - deltaColor.1) * progress, b: (self.selectColor.2 - deltaColor.2) * progress)
        targetLabel.textColor = UIColor(r: (self.normalColor.0 + deltaColor.0) * progress, g: (self.normalColor.1 + deltaColor.1) * progress, b: (self.normalColor.2 + deltaColor.2) * progress)
    

    5、可以设置遮罩渐变

    //4.拖动contenView,mask渐变
    
        if self.style.isNeedMask {
            self.lableMaskView.frame.size.width = self.style.isScrollEnable ? soureLabel.frame.width + 2 * self.style.maskInsetMargin + deltaWidth * progress : soureLabel.frame.width + deltaWidth * progress
            self.lableMaskView.frame.origin.x = self.style.isScrollEnable ? soureLabel.frame.origin.x -  self.style.maskInsetMargin + deltaX * progress : soureLabel.frame.origin.x + deltaX * progress
        }
    

    所有可以配置的参数都可以通过LZBPageStyleModel配置

    var  titleViewHeight : CGFloat = 44  //标题栏默认高度
    
     var  titleViewTitleNormalColor : UIColor = UIColor(r: 0, g: 0, b: 0)  //正常情况默认黑色
    var  titleViewTitleSelectColor : UIColor = UIColor(r: 255, g: 0, b: 7) //选中情况默认红色
    var  titleViewTitleFont : UIFont = UIFont.systemFont(ofSize: 14.0)  //默认字体
    var  isScrollEnable : Bool = false   //默认不可以滚动
    var  titleViewMargin :  CGFloat = 20.0  //默认间距
    var  isShowBottomLine : Bool = true      //是否显示底部线
    var  bottomLineHeight : CGFloat = 2.0   //底部线的默认高度
    var  bottomLineColor : UIColor = UIColor.red    //底部线默认颜色
    var isNeedScale : Bool = false   //是否需要放大
    var maxScale : CGFloat = 1.2   //缩放的最大值
    var isNeedMask : Bool = false   //是否需要遮罩
    var maskColor  : UIColor = UIColor(white: 0.4, alpha: 0.5)  //遮罩颜色
    var maskInsetMargin : CGFloat = 10.0  //遮罩内部间距
    var maskHeight : CGFloat = 25.0  //遮罩高度
    var maskLayerRadius : CGFloat = 12.5  //遮罩圆角
    

    封装UI使用步骤###

    1、创建标题栏标题数组,注意必须与后面控制器对应

      let titles = ["英雄联盟","火蓝","提姆队长","史前巨鳄","洛克萨斯之手","狗头","武器"]
    

    2、创建控制器数组,根据自己项目具体不同而使用不同的控制器
    > var childvcs = UIViewController
    for i in 0..<titles.count
    {
    let vc = UIViewController()
    let backgroundcolor = UIColor.getRandomColor() //获取随机颜色
    vc.view.backgroundColor = backgroundcolor
    childvcs.append(vc)
    }
    3、创建样式,根据项目自己配置

    var  pageStyleModel = LZBPageStyleModel()
    
        pageStyleModel.isScrollEnable = true  //是否可以滚动
        pageStyleModel.isNeedScale = true  //是否需要放大
        pageStyleModel.isShowBottomLine = false  /是否需要底部线
        pageStyleModel.isNeedMask = true  //是否需要遮罩
    

    4、创建pageView

    let pageView = LZBPageView(frame: pageFrame, titles: titles, pageStyle: pageStyleModel, childVcs: childvcs, parentVc: self)
    
      view.addSubview(pageView)
    

    代码结构清晰、并且有注释,集成度高、耦合性低,方便使用

    代码下载地址:
    swift版本:高仿今日头条、网易新闻UI结构
    OC版本:高仿今日头条

    最后赠言###

    如果觉得文章对您有帮助,不要忘记star哦!😝,star 是对程序猿最大的鼓励!

    相关文章

      网友评论

        本文标题:模仿今日头条滚动界面(多种使用场景封装)

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