欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS 頁面滑動與標(biāo)題切換顏色漸變的聯(lián)動效果實(shí)例

 更新時間:2017年04月10日 08:31:30   作者:imsz5460  
本篇文章主要介紹了iOS 頁面滑動與標(biāo)題切換顏色漸變的聯(lián)動效果實(shí)例,具有一定的參考價值,有興趣的可以了解一下。

話不多說,直接上圖,要實(shí)現(xiàn)類似如下效果。

這個效果非常常見,這里著重講講核心代碼

封裝頂部的PageTitleView

封裝構(gòu)造函數(shù)

封裝構(gòu)造函數(shù),讓別人在創(chuàng)建對象時,就傳入其實(shí)需要顯示的內(nèi)容 frame:創(chuàng)建對象時確定了

  1. frame就可以直接設(shè)置子控件的位置和尺寸
  2. isScrollEnable:是否可以滾動。某些地方該控件是可以滾動的。
  3. titles:顯示的所有標(biāo)題
// MARK:- 構(gòu)造函數(shù)
init(frame: CGRect, isScrollEnable : Bool, titles : [String]) {
selfisScrollEnable = isScrollEnable
selftitles = titles
superinit(frame: frame)
}

設(shè)置UI界面

設(shè)置UI界面

  1.  添加UIScrollView,如果標(biāo)題過多,則可以滾動
  2. 初始化所有的Label,用于顯示標(biāo)題。并且給label添加監(jiān)聽手勢
  3. 添加頂部線和滑塊的View

實(shí)現(xiàn)相對來說比較簡單,這里代碼從略

封裝底部的PageCotentView

封裝構(gòu)造函數(shù)

封裝構(gòu)造函數(shù),讓別人在創(chuàng)建對象時,就傳入其實(shí)需要顯示的內(nèi)容

  1. 所有用于顯示在UICollectionView的Cell的所有控制器
  2. 控制器的父控制器
// MARK:- 構(gòu)造函數(shù)
init(frame: CGRect, childVcs : [UIViewController], parentViewController : UIViewController) {
selfchildVcs = childVcs
selfparentViewController = parentViewController

superinit(frame: frame)
}

設(shè)置UI界面內(nèi)容

設(shè)置UI界面

  1. 將所有的子控制器添加到父控制器中
  2. 添加UICollectionView,用于展示內(nèi)容
// MARK:- 懶加載屬性
private lazy var collectionView : UICollectionView = {
// 1.創(chuàng)建布局
let layout = UICollectionViewFlowLayout()
layout.itemSize = self.bounds.size
layout.minimumLineSpacing = 0
layout.minimumInteritemSpacing = 0
layout.scrollDirection = .Horizontal
// 2.創(chuàng)建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)
}

實(shí)現(xiàn)UICollectionView的數(shù)據(jù)源方法

  1. 在返回Cell的方法中,先將cell的contentView中的子控件都移除,防止循環(huán)引用
  2. 取出indexPath.item對應(yīng)的控制器,將控制器的View添加到Cell的contentView中
// MARK:- 遵守UICollectionView的數(shù)據(jù)源
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點(diǎn)擊改變PageContentView

通過代理將PageTitleView的事件傳遞出去

/// 定義協(xié)議
protocol PageTitleViewDelegate : class {
func pageTitleView(pageTitleView : PageTitleView, didSelectedIndex index : Int)
}
@objc private func titleLabelClick(tapGes : UITapGestureRecognizer) {
// 1.獲取點(diǎn)擊的下標(biāo)志
guard let view = tapGes.view else { return }
let index = view.tag
// 2.滾到正確的位置
scrollToIndex(index)
// 3.通知代理
delegate?.pageTitleView(self, didSelectedIndex: index)
}

內(nèi)部調(diào)整

// 內(nèi)容滾動
private func scrollToIndex(index : Int) {
// 1.獲取最新的label和之前的label
let newLabel = titleLabels[index]
let oldLabel = titleLabels[currentIndex]
// 2.設(shè)置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中設(shè)置當(dāng)前應(yīng)該滾動的位置

// MARK:- 對外暴露方法
extension PageContentView {
 func scrollToIndex(index : Int) {
let offset = CGPoint(x: CGFloat(index) * collectionViewboundswidth, y: 0)
 collectionViewsetContentOffset(offset, animated: false)
}
}

PageContentView滾動調(diào)整PageTitleView

通過觀察,我們發(fā)現(xiàn):

                1> 原來位置的Title顏色會逐漸變暗

                2> 目標(biāo)位置的Title顏色會逐漸變亮

                3> 變化程度是和滾動的多少相關(guān)

由此得出結(jié)論:

我們一共需要獲取三個值

1> 起始位置下標(biāo)值

2> 目標(biāo)位置下標(biāo)值

3> 當(dāng)前滾動的進(jìn)度

其實(shí)前2點(diǎn)可以由第3點(diǎn)計算而來,可以只需要將進(jìn)度傳遞出去。

根據(jù)進(jìn)度值處理標(biāo)題顏色漸變及滑塊邏輯

         。當(dāng)前進(jìn)度值唯一確定了標(biāo)題的狀態(tài),計算出需要發(fā)生顏色變化的兩相鄰標(biāo)題索引

         。注意:下標(biāo)值需要防止越界問題,臨界點(diǎn)的處理

實(shí)現(xiàn)代碼

extension PageContentView : UICollectionViewDelegate {

func scrollViewWillBeginDragging(scrollView: UIScrollView) {

startOffsetX = scrollView.contentOffset.x

}

func scrollViewDidScroll(scrollView: UIScrollView) {

// 0.判斷是否是點(diǎn)擊事件

 if isForbidScrollDelegate { return }

// 1.定義獲取需要的數(shù)據(jù)

 var progress : CGFloat = 0

 let currentOffsetX = scrollView.contentOffset.x

 let scrollViewW = scrollView.bounds.width

  // 1.計算progress

  progress = currentOffsetX / scrollViewW

  // 3.將progress傳遞給titleView

 delegate?.pageContentView(self, progress: progress)

 }

}

根據(jù)滾動傳入的值,調(diào)整PageTitleView

兩種顏色必須使用RGB值設(shè)置(方便通過RGB實(shí)現(xiàn)漸變效果)

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)

調(diào)整scrollLine及兩個Label顏色漸變

// MARK:- 對外暴露方法

extension PageTitleView

 func changeLabel(progress: CGFloat) {

// 開啟彈簧效果時的過濾處理
 var progress = progress > 0 ? progress : 0

  progress = progress <= CGFloat(titleLabels.count - 1) ? progress : CGFloat(titleLabels.count - 1)

 var leftLabelIndex = Int(floor(progress))

 let ratio = progress - CGFloat(leftLabelIndex)

 //獲取leftLabel和rightLabel

 let leftLabel = titleLabels[leftLabelIndex]

 if leftLabelIndex >= 3{

  leftLabelIndex = 3

 }

 print("leftLabelIndex = \(leftLabelIndex)")

 var rightIndex = leftLabelIndex + 1

 if rightIndex >= 3{

  rightIndex = 3

 }

 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.0 - kNormalColor.0, kSelectedColor.1 - kNormalColor.1, kSelectedColor.2 - kNormalColor.2)

 if leftLabelIndex != rightIndex {

 // 3.2.變化leftLabel

 leftLabel.textColor = UIColor(r: kSelectedColor.0 - colorDelta.0 * ratio, g: kSelectedColor.1 - colorDelta.1 * ratio, b: kSelectedColor.2 - colorDelta.2 * ratio)

 // 3.2.變化rightLabel

 rightLabel.textColor = UIColor(r: kNormalColor.0 + colorDelta.0 * ratio, g: kNormalColor.1 + colorDelta.1 * ratio, b: kNormalColor.2 + colorDelta.2 * ratio)

 }

 // 4.記錄最新的index

 currentIndex = leftLabelIndex
 }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • iOS通過UISwitch控制搖一搖

    iOS通過UISwitch控制搖一搖

    這篇文章主要為大家詳細(xì)介紹了iOS通過UISwitch控制搖一搖,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-06-06
  • iOS13即將到來,iOS推送DeviceToken適配方案詳解

    iOS13即將到來,iOS推送DeviceToken適配方案詳解

    這篇文章主要介紹了iOS13即將到來,iOS推送DeviceToken適配方案詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • iOS中id類型的理解及底層原理詳解

    iOS中id類型的理解及底層原理詳解

    這篇文章主要給大家介紹了關(guān)于iOS中id類型的理解以及底層原理的相關(guān)資料,文中介紹的非常詳細(xì),對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。
    2017-07-07
  • 詳細(xì)談?wù)刬OS字符串翻轉(zhuǎn)

    詳細(xì)談?wù)刬OS字符串翻轉(zhuǎn)

    字符串翻轉(zhuǎn)是我們在學(xué)習(xí)算法的時候經(jīng)常會遇到的一個基礎(chǔ)算題,下面這篇文章主要給大家詳解介紹了關(guān)于iOS字符串翻轉(zhuǎn)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧
    2018-07-07
  • 講解iOS開發(fā)中UITableView列表設(shè)計的基本要點(diǎn)

    講解iOS開發(fā)中UITableView列表設(shè)計的基本要點(diǎn)

    這篇文章主要介紹了講解iOS開發(fā)中UITableView列表設(shè)計的基本要點(diǎn),其中對列表行操作的常用操作舉例是iOS開發(fā)中經(jīng)常用到的基礎(chǔ),需要的朋友可以參考下
    2016-01-01
  • iOS將地址解析成經(jīng)緯度的方法

    iOS將地址解析成經(jīng)緯度的方法

    這篇文章主要為大家詳細(xì)介紹了iOS將地址解析成經(jīng)緯度的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • iOS開發(fā)中仿Tumblr點(diǎn)贊心破碎動畫效果

    iOS開發(fā)中仿Tumblr點(diǎn)贊心破碎動畫效果

    這篇文章主要介紹了iOS開發(fā)中仿Tumblr點(diǎn)贊心破碎動畫效果,本文圖文并茂給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2017-04-04
  • IOS展開三級列表效果示例

    IOS展開三級列表效果示例

    今天介紹的是一個很不錯的三級列表展開效果的例子,文章運(yùn)用實(shí)例代碼介紹的很詳細(xì),提供給學(xué)習(xí)IOS的小伙伴們使用。
    2016-08-08
  • iOS實(shí)現(xiàn)文字轉(zhuǎn)化成彩色文字圖片

    iOS實(shí)現(xiàn)文字轉(zhuǎn)化成彩色文字圖片

    這篇文章主要為大家詳細(xì)介紹了iOS文字轉(zhuǎn)化成彩色文字圖片的實(shí)現(xiàn)方法,可以實(shí)現(xiàn)不同字體,漸變的效果,感興趣的小伙伴們可以參考一下
    2016-03-03
  • iOS制作framework靜態(tài)庫圖文教程

    iOS制作framework靜態(tài)庫圖文教程

    這篇文章主要為大家詳細(xì)介紹了iOS制作framework靜態(tài)庫圖文教程,感興趣的小伙伴們可以參考一下
    2016-08-08

最新評論