话不多说,直接上图,要实现类似如下效果。

  • 这个效果非常常见,这里着重讲讲核心代码

封装顶部的PageTitleView

封装构造函数

  • 封装构造函数,让别人在创建对象时,就传入其实需要显示的内容

    • frame:创建对象时确定了frame就可以直接设置子控件的位置和尺寸
    • isScrollEnable:是否可以滚动。某些地方该控件是可以滚动的。
    • titles:显示的所有标题
  1. // MARK:- 构造函数
  2. init(frame: CGRect, isScrollEnable : Bool, titles : [String]) {
  3. self.isScrollEnable = isScrollEnable
  4. self.titles = titles
  5. super.init(frame: frame)
  6. }

设置UI界面

  • 设置UI界面

    • 添加UIScrollView,如果标题过多,则可以滚动
    • 初始化所有的Label,用于显示标题。并且给label添加监听手势
    • 添加顶部线和滑块的View

实现相对来说比较简单,这里代码从略

封装底部的PageCotentView

封装构造函数

  • 封装构造函数,让别人在创建对象时,就传入其实需要显示的内容

    • 所有用于显示在UICollectionView的Cell的所有控制器
    • 控制器的父控制器
  1. // MARK:- 构造函数
  2. init(frame: CGRect, childVcs : [UIViewController], parentViewController : UIViewController) {
  3. self.childVcs = childVcs
  4. self.parentViewController = parentViewController
  5. super.init(frame: frame)
  6. }

设置UI界面内容

  • 设置UI界面

    • 将所有的子控制器添加到父控制器中
    • 添加UICollectionView,用于展示内容
  1. // MARK:- 懒加载属性
    private lazy var collectionView : UICollectionView = {
    // 1.创建布局
    let layout = UICollectionViewFlowLayout()
    layout.itemSize = self.bounds.size
    layout.minimumLineSpacing =
    layout.minimumInteritemSpacing =
    layout.scrollDirection = .Horizontal
    // 2.创建collectionView
    let collectionView = UICollectionView(frame: self.bounds, collectionViewLayout: layout)
    collectionView.showsHorizontalScrollIndicator = false
    collectionView.pagingEnabled = true
    collectionView.bounces = false
    collectionView.scrollsToTop = false
    collectionView.dataSource = self
    collectionView.delegate = self
    collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: kContentCellID)
    return collectionView
    }()
    private func setupUI() {
    // 1.添加所有的控制器
    for childVc in childVcs {
    parentViewController?.addChildViewController(childVc)
    }
    // 2.添加collectionView
    addSubview(collectionView)
    }

实现UICollectionView的数据源方法

  • 在返回Cell的方法中,先将cell的contentView中的子控件都移除,防止循环引用
  • 取出indexPath.item对应的控制器,将控制器的View添加到Cell的contentView中
    // MARK:- 遵守UICollectionView的数据源
    extension PageContentView : UICollectionViewDataSource {
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return childVcs.count
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(kContentCellID, forIndexPath: indexPath)
    // 移除之前的
    for subview in cell.contentView.subviews {
    subview.removeFromSuperview()
    }
    // 取出控制器
    let childVc = childVcs[indexPath.item]
    childVc.view.frame = cell.contentView.bounds
    cell.contentView.addSubview(childVc.view)
    return cell
    }
    }

    PageTitleView点击改变PageContentView

  • 通过代理将PageTitleView的事件传递出去

    /// 定义协议
    protocol PageTitleViewDelegate : class {
    func pageTitleView(pageTitleView : PageTitleView, didSelectedIndex index : Int)
    }
    @objc private func titleLabelClick(tapGes : UITapGestureRecognizer) {
    // 1.获取点击的下标志
    guard let view = tapGes.view else { return }
    let index = view.tag
    // 2.滚到正确的位置
    scrollToIndex(index)
    // 3.通知代理
    delegate?.pageTitleView(self, didSelectedIndex: index)
    }

    内部调整

    // 内容滚动
    private func scrollToIndex(index : Int) {
    // 1.获取最新的label和之前的label
    let newLabel = titleLabels[index]
    let oldLabel = titleLabels[currentIndex]
    // 2.设置label的颜色
    newLabel.textColor = kSelectTitleColor
    oldLabel.textColor = kNormalTitleColor
    // 3.scrollLine滚到正确的位置
    let scrollLineEndX = scrollLine.frame.width * CGFloat(index)
    UIView.animateWithDuration(0.15) {
    self.scrollLine.frame.origin.x = scrollLineEndX
    }
    // 4.记录index
    currentIndex = index
    }
  • 在PageContentView中设置当前应该滚动的位置
  1. // MARK:- 对外暴露方法
  2. extension PageContentView {
  3. func scrollToIndex(index : Int) {
  4. let offset = CGPoint(x: CGFloat(index) * collectionView.bounds.width, y: 0)
  5. collectionView.setContentOffset(offset, animated: false)
  6. }
  7. }

PageContentView滚动调整PageTitleView

通过观察,我们发现:

1> 原来位置的Title颜色会逐渐变暗

2> 目标位置的Title颜色会逐渐变亮

3> 变化程度是和滚动的多少相关

  • 由此得出结论:
  • 我们一共需要获取三个值

    • 1> 起始位置下标值
    • 2> 目标位置下标值
    • 3> 当前滚动的进度

其实前2点可以由第3点计算而来,可以只需要将进度传递出去。

  • 根据进度值处理标题颜色渐变及滑块逻辑

。当前进度值唯一确定了标题的状态,计算出需要发生颜色变化的两相邻标题索引

。注意:下标值需要防止越界问题,临界点的处理

实现代码

extension PageContentView : UICollectionViewDelegate {

func scrollViewWillBeginDragging(scrollView: UIScrollView) {

startOffsetX = scrollView.contentOffset.x

}

func scrollViewDidScroll(scrollView: UIScrollView) {

// 0.判断是否是点击事件

       if isForbidScrollDelegate { return }

// 1.定义获取需要的数据

        var progress : CGFloat = 

        let currentOffsetX = scrollView.contentOffset.x

        let scrollViewW = scrollView.bounds.width

             // 1.计算progress

            progress = currentOffsetX / scrollViewW

             // 3.将progress传递给titleView

        delegate?.pageContentView(self, progress: progress)

   }

}

根据滚动传入的值,调整PageTitleView

两种颜色必须使用RGB值设置(方便通过RGB实现渐变效果)

private let kNormalRGB : (CGFloat, CGFloat, CGFloat) = (85, 85, 85)

private let kSelectRGB : (CGFloat, CGFloat, CGFloat) = (255, 128, 0)

private let kDeltaRGB = (kSelectRGB.0 - kNormalRGB.0, kSelectRGB.1 - kNormalRGB.1, kSelectRGB.2 - kNormalRGB.2)

private let kNormalTitleColor = UIColor(red: 85/255.0, green: 85/255.0, blue: 85/255.0, alpha: 1.0)

private let kSelectTitleColor = UIColor(red: 255.0/255.0, green: 128/255.0, blue: 0/255.0, alpha: 1.0)

调整scrollLine及两个Label颜色渐变

// MARK:- 对外暴露方法

extension PageTitleView

    func changeLabel(progress: CGFloat) {

//        开启弹簧效果时的过滤处理
var progress = progress > ? progress : progress = progress <= CGFloat(titleLabels.count - ) ? progress : CGFloat(titleLabels.count - ) var leftLabelIndex = Int(floor(progress)) let ratio = progress - CGFloat(leftLabelIndex) //获取leftLabel和rightLabel let leftLabel = titleLabels[leftLabelIndex] if leftLabelIndex >= { leftLabelIndex = } print("leftLabelIndex = \(leftLabelIndex)") var rightIndex = leftLabelIndex + if rightIndex >= { rightIndex = } print("rightIndex = \(rightIndex)") let rightLabel = titleLabels[rightIndex] //滑块的逻辑 let moveTotalX = leftLabel.frame.width let moveX = moveTotalX * ratio scrollLine.frame.origin.x = leftLabel.frame.origin.x + moveX //3.Label颜色的渐变 // 3.1.取出变化的范围 let colorDelta = (kSelectedColor. - kNormalColor., kSelectedColor. - kNormalColor., kSelectedColor. - kNormalColor.) if leftLabelIndex != rightIndex { // 3.2.变化leftLabel leftLabel.textColor = UIColor(r: kSelectedColor. - colorDelta. * ratio, g: kSelectedColor. - colorDelta. * ratio, b: kSelectedColor. - colorDelta. * ratio) // 3.2.变化rightLabel rightLabel.textColor = UIColor(r: kNormalColor. + colorDelta. * ratio, g: kNormalColor. + colorDelta. * ratio, b: kNormalColor. + colorDelta. * ratio) } // 4.记录最新的index currentIndex = leftLabelIndex
}
}

iOS 滑动页面标题切换颜色渐变效果的更多相关文章

  1. Selenium WebDriver-通过页面标题切换窗口

    selenium webdriver可以通过获取页面标题,再跟据标题去切换浏览器窗口,代码如下: #encoding=utf-8 import unittest import time import ...

  2. 解决IOS滑动页面fixed浮动问题

    <div style="position: fixed"></div> <div style="height: 100%; overflow ...

  3. 在uwp仿IOS的页面切换效果

    有时候我们需要编写一些迎合IOS用户使用习惯的uwp应用,我在这里整理一下仿IOS页面切换效果的代码. 先分析IOS的页面切换.用户使用左右滑动方式进行前进和后退,播放类似于FlipView的切换动画 ...

  4. Android之怎样实现滑动页面切换【Fragment】

    Fragment 页面切换不能滑动 所以对于listview 能够加入的左右滑动事件 .不会有冲突比如(QQ的好友列表的删除)  Fragment 和viewpager 的差别  Viewpager ...

  5. bootsrtap h5 移动版页面 在苹果手机ios滑动上下拉动滚动卡顿问题解决方法

    bootsrtap h5 移动版页面 在苹果手机ios滑动上下拉动滚动卡顿问题解决方法 bootsrtap框架做的h5页面,在android手机下没有卡顿问题,在苹果手机就一直存在这问题,开始毫无头绪 ...

  6. ViewPager+PagerTabStrip实现页面的切换

    页面切换效果图 首先创建布局: 代码: <?xml version="1.0" encoding="utf-8"?><LinearLayout ...

  7. iOS开发系列--视图切换

    概述 在iOS开发中视图的切换是很频繁的,独立的视图应用在实际开发过程中并不常见,除非你的应用足够简单.在iOS开发中常用的视图切换有三种,今天我们将一一介绍: UITabBarController ...

  8. Visual Studio跨平台开发实战(3) - Xamarin iOS多页面应用程式开发

    原文 Visual Studio跨平台开发实战(3) - Xamarin iOS多页面应用程式开发 前言 在前一篇教学中, 我们学会如何使用Visual Studio 搭配Xcode 进行iOS基本控 ...

  9. ViewPager和View组合 实现页面的切换

    //--------------主页面------------------------------- package com.bw.test; import java.util.ArrayList;i ...

随机推荐

  1. DP 动态规划

    p1269 马棚 题目: 每天,小明和他的马外出,然后他们一边跑一边玩耍.当他们结束的时候,必须带所有的马返回马棚,小明有K个马棚.他把他的马排成一排然后跟随它走向马棚,因为他们非常疲劳,小明不想让他 ...

  2. USB重定向

    第一期中,我们一起简要的看了下传统PC和桌面云下USB重定向和USB设备重定向方式的差异,了解了桌面云下外设兼容性问题来源的根源-USB设备本身驱动不规范/不支持, 或者虚拟机驱动实现上与USB设备对 ...

  3. spring mvc 结合 Hessian 配置

    spring mvc 结合 Hessian 配置 1.先在web.xml中配置 <!-- Hessian配置 --> <servlet> <servlet-name> ...

  4. JavaScript基础——兼容性、错误处理

    JavaScript基础-错误处理Throw.Try.Catch try语句执行可能出错的代码 catch语句处理捕捉到的错误 throw语句创建自定义错误语句 发生的常见的错误类型 可能是语法错误, ...

  5. Sublime Text 3 (Build 3126) 最新注册码

    Sublime Text 作为程序员开发神器,听说最新版更新了 并且增加了不少新特性.马上到官网下载了最新版 Sublime Text 3 3126 使用了下,反应速度比以前的确更快了.随手找了几个S ...

  6. 分享学习——ERP项目管理经验

    为什么在实施过程中有的项目就能做的非常好,有的项目应用效果就非常差?原因在哪里?下面本人就从下面几个方面进行分析: 1.什么是项目? 2.在ERP软件行业项目应该怎么做? 3.为什么有一些项目会失败, ...

  7. hadoop使用笔记

    一:hadoop程序添加三方包: 使用hadoop jar 运行时 抛出 java.lang.NoClassDefFoundError 原因:找不到三方包 解决方案: 1.可以将需要使用的包添加进 $ ...

  8. 编程那些事儿:如何快速地"借用"CSS

    做前端开发有时候会碰到任务紧急,需要马上写好静态页的问题.比如,设计师给你扔了一个设计稿,要你在下班之前搞定.这时候你如热锅上的蚂蚁,如果自己写css的话,时间紧张,于是上网找了一下相关模板页面,找到 ...

  9. 选择 GCD 还是 NSTimer

    我们常常会延迟某件任务的执行,或者让某件任务周期性的执行.然后也会在某些时候需要取消掉之前延迟执行的任务. 延迟操作的方案一般有三种: 1.NSObject的方法: gcdTimer 2.使用NSTi ...

  10. tp框架之对列表的一系列操作及跳转页面(详细步骤)

    依旧是在Main控制器里面写类方法,如果想看tp全部的话,可以从前几篇开始看,都是一整个步骤下来的 在控制器中重新写个类 然后再做个shouye.html页面 nation表的数据,将会在shou.h ...