麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > Swift > 正文

利用swift實(shí)現(xiàn)卡片橫向滑動(dòng)動(dòng)畫效果的方法示例

2020-03-09 17:44:49
字體:
供稿:網(wǎng)友

本文主要給大家介紹了關(guān)于利用swift/205134.html">swift實(shí)現(xiàn)卡片橫向滑動(dòng)動(dòng)畫效果的相關(guān)資料,分享出來供大家參考學(xué)習(xí),下面來一起看看詳細(xì)的介紹吧。

根據(jù)慣例,首先上效果圖:

swift,橫向滑動(dòng)卡片,橫向滾動(dòng),cell橫向滾動(dòng)圖

那天去面試,面試官突然拿出手機(jī)點(diǎn)開了一個(gè)app,自個(gè)在那點(diǎn)了一會(huì),然后問我 這個(gè)效果怎么實(shí)現(xiàn),當(dāng)時(shí)一看可以滑動(dòng),肯定用scrollView 或者 collectionView實(shí)現(xiàn),就大概的說了下。今天剛好閑下來,就敲一敲這個(gè)效果。

先來分析下這個(gè)效果:

卡片是橫向滾動(dòng),并且每個(gè)卡片的位置都是保持在屏幕中間的,而且 左右相鄰的卡片都露出來一點(diǎn)邊
collectionView 和scrollView都可以實(shí)現(xiàn),在這里,我們用collectionView實(shí)現(xiàn),但是我們平常普通用的collectionView都是正屏滑動(dòng)的!!而且是平滑,所有我們只能自定義UICollectionViewFlowLayout 流式布局,才可以達(dá)到上圖效果.

廢話不多說,直接上代碼:

創(chuàng)建collectionView布局

//創(chuàng)建collectionView布局func setepUI() {//CustomLayout是自定義的UICollectionViewFlowLayout layout = CustomLayout() layout?.itemSize = CGSize(width: SCREEN_WIDTH-80, height: SCREEN_HEIGHT-64-120) let rect = CGRect(x: 0, y: 64, width:SCREEN_WIDTH , height: SCREEN_HEIGHT-64) collectionView = UICollectionView(frame: rect, collectionViewLayout: layout!) collectionView?.delegate = self collectionView?.dataSource = self view.addSubview(collectionView!) collectionView?.register(CustomViewCell.self, forCellWithReuseIdentifier: "identifier") collectionView?.backgroundColor = UIColor.red }

實(shí)現(xiàn)代理方法:

我們在extension中實(shí)現(xiàn):

// MARK: -- delegate and datasourceextension ViewController:UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{ func numberOfSections(in collectionView: UICollectionView) -> Int { return 1 } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 10 } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {//CustomViewCell是自定義的cell let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "identifier", for: indexPath) as! CustomViewCell cell.backgroundColor = UIColor.orange cell.lable?.text = "/(indexPath.row)//(10)" return cell }}

至此,我們可以得到普通的效果,左右滑動(dòng),但中間cell不會(huì)居中,兩側(cè)cell也不會(huì)縮放,如下圖:

swift,橫向滑動(dòng)卡片,橫向滾動(dòng),cell橫向滾動(dòng)圖

這個(gè)時(shí)候就需要在自定義的流式布局 CustomLayout里做點(diǎn)什么了:

初始化方法 prepare , 初始化一些內(nèi)容:

//重寫prepare方法 //布局之前的準(zhǔn)備工作 初始化 這個(gè)方法每次layout發(fā)生改變就調(diào)用一次override func prepare() {  scrollDirection = UICollectionViewScrollDirection.horizontal   minimumLineSpacing = 20.0  sectionInset = UIEdgeInsets(top: 0, left: 40, bottom: 0, right: 40)  super.prepare() }

(該方法默認(rèn)返回false) 返回true frame發(fā)生改變就允許重新布局 內(nèi)部會(huì)重新調(diào)用prepare 和

layoutAttributesForElementsInRect override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {  return true }

MARK:---用來計(jì)算出rect這個(gè)范圍內(nèi)所有cell的UICollectionViewLayoutAttributes 對象,循環(huán)遍歷每個(gè)attribute對象,修改frame,再將這數(shù)組返回給系統(tǒng)

 override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {  //根據(jù)當(dāng)前滾動(dòng)進(jìn)行對每個(gè)cell進(jìn)行縮放  //首先獲取 當(dāng)前rect范圍內(nèi)的 attributes對象  let array = super.layoutAttributesForElements(in: rect)  private let ScaleFactor:CGFloat = 0.001//縮放因子  //計(jì)算縮放比 首先計(jì)算出整體中心點(diǎn)的X值 和每個(gè)cell的中心點(diǎn)X的值  //用著兩個(gè)x值的差值 ,計(jì)算出絕對值  //colleciotnView中心點(diǎn)的值  let centerX = (collectionView?.contentOffset.x)! + (collectionView?.bounds.size.width)!/2  //循環(huán)遍歷每個(gè)attributes對象 對每個(gè)對象進(jìn)行縮放  for attr in array! {   //計(jì)算每個(gè)對象cell中心點(diǎn)的X值   let cell_centerX = attr.center.x   //計(jì)算兩個(gè)中心點(diǎn)的便宜(距離)   //距離越大縮放比越小,距離小 縮放比越大,縮放比最大為1,即重合   let distance = abs(cell_centerX-centerX)   let scale:CGFloat = 1/(1+distance*ScaleFactor)   attr.transform3D = CATransform3DMakeScale(1.0, scale, 1.0)  }  return array }

到目前為止,我們可以得到一個(gè)縮放的效果,但是仍然沒有達(dá)到我們要的效果,可視區(qū)域的cell并沒有居中顯示,而是滑到哪里就到哪里:

如下圖:

swift,橫向滑動(dòng)卡片,橫向滾動(dòng),cell橫向滾動(dòng)圖

所以我們還得重寫一個(gè)方法:

func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint

需要注意的兩個(gè)參數(shù):

  • proposedContentOffset :手指滑動(dòng)視圖最終停止的便宜量,并不是手指離開時(shí)的偏移量(congtentOffset)
  • velocity:手指滑動(dòng)的速率

實(shí)現(xiàn)該方法:

/// <#Description#> /// /// - Parameter proposedContentOffset: 當(dāng)手指滑動(dòng)的時(shí)候 最終的停止的偏移量 /// - Returns: 返回最后停止后的點(diǎn) override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint {  let visibleX = proposedContentOffset.x  let visibleY = proposedContentOffset.y  let visibleW = collectionView?.bounds.size.width  let visibleH = collectionView?.bounds.size.height  //獲取可視區(qū)域  let targetRect = CGRect(x: visibleX, y: visibleY, width: visibleW!, height: visibleH!)  //中心點(diǎn)的值  let centerX = proposedContentOffset.x + (collectionView?.bounds.size.width)!/2  //獲取可視區(qū)域內(nèi)的attributes對象  let attrArr = super.layoutAttributesForElements(in: targetRect)!  //如果第0個(gè)屬性距離最小  var min_attr = attrArr[0]  for attributes in attrArr {   if (abs(attributes.center.x-centerX) < abs(min_attr.center.x-centerX)) {    min_attr = attributes   }  }  //計(jì)算出距離中心點(diǎn) 最小的那個(gè)cell 和整體中心點(diǎn)的偏移  let ofsetX = min_attr.center.x - centerX  return CGPoint(x: proposedContentOffset.x+ofsetX, y: proposedContentOffset.y) }

至此,整個(gè)過程結(jié)束,其實(shí)很簡單,主要是對這幾個(gè)方法理解!

最后代碼下載:

github下載地址:點(diǎn)這里

 

總結(jié)

以上就是這篇文章的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對VEVB武林網(wǎng)的支持。

 

注:相關(guān)教程知識(shí)閱讀請移步到swift教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 视频一区 日韩 | 久久艳片 | 欧美区在线 | 曰韩精品 | 精品一区二区久久久久久久网精 | 欧美特一级 | 一本色道久久综合亚洲精品小说 | 欧美一级网 | 黄色av片三级三级三级免费看 | 久久亚洲国产精品 | 欧美中文字幕一区二区三区亚洲 | 91一区二区在线观看 | 成人免费毛片一 | 日本不卡一区二区在线观看 | 日本成人一二三区 | 日日摸夜夜添夜夜添牛牛 | 一级做a爱片性色毛片 | 操操操日日日干干干 | 美女亚洲综合 | 亚洲国产精品久久久久久久久久 | 亚洲成人自拍电影 | xxxx欧美视频 | 亚洲第一成人在线观看 | 成年毛片 | av电影网站在线观看 | 国产精品刺激对白麻豆99 | 一区二区三区播放 | 污视频在线免费 | 91麻豆精品国产91久久久无需广告 | 久久精品亚洲一区 | 国产精品久久久久久久午夜片 | 毛片一区二区三区四区 | 内地av在线 | www.91sese| 国产正在播放 | 欧美日韩中文字幕在线 | 亚洲精品 欧美 | 爽爽视频免费看 | 免费视频a | www.9191.com| 高清国产福利 |