2016-11-22 50 views
1

我有一個水平collectionView,我想管理滾動時,中心單元格變大。所以,每一個細胞是中心細胞,它將比其他細胞更大。檢查下面的圖像。 enter image description hereSwift如何獲取collectionView中的中心單元格並在滾動時調整其大小?

任何人都可以幫助我以最好的方式做到這一點?提前致謝!

我迄今爲止代碼:

import UIKit 

class ViewController: UIViewController, UICollectionViewDataSource,  UICollectionViewDelegate { 
@IBOutlet var horizontalCollectionView: UICollectionView! 
@IBOutlet var verticalCollectionView: UICollectionView! 

var horizontal = ["1","2","3","4","5","6","7","8","9","10","1","2","3","4","5","6","7","8","9","10"] 
var horizontalCellSize = CGSize(width: 50, height: 50) 
var horizontalLargeCellSize = CGSize(width: 80, height: 80) 


var centerCell = UICollectionViewCell() 
var cellIsInCenter = false 
var myIndexPath = IndexPath() 

override func viewDidLoad() { 
    super.viewDidLoad() 

} 

// When the main view's dimensions change this will re-layout the collection view 
override func viewWillLayoutSubviews() { 
    verticalCollectionView.collectionViewLayout.invalidateLayout() 
} 

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return horizontal.count 
} 

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

    if collectionView == horizontalCollectionView { 
     let horizontalCell = collectionView.dequeueReusableCell(withReuseIdentifier: "HorizontalCell", for: indexPath) as! HorizontalCell 
     horizontalCell.backgroundColor = UIColor.blue 
     horizontalCell.textLabel.text = horizontal[indexPath.row] 
     return horizontalCell 
    } 

    else { 

     let verticalCell = collectionView.dequeueReusableCell(withReuseIdentifier: "VerticalCell", for: indexPath) as! VerticalCell 
     verticalCell.backgroundColor = UIColor.red 
     verticalCell.textLabel.text = horizontal[indexPath.row] 
     return verticalCell 
    } 
} 

// MARK: UIScrollViewDelegate 

func scrollViewDidScroll(_ scrollView: UIScrollView) { 
    if scrollView == self.horizontalCollectionView { 
     let horizontalScrollPosition = scrollView.contentOffset.x * 35 
     self.verticalCollectionView.delegate = nil 
     self.verticalCollectionView.contentOffset = CGPoint(x: 0, y: horizontalScrollPosition) 
     self.verticalCollectionView.delegate = self 
    } 

    if scrollView == self.verticalCollectionView { 
     let verticalScrollPosition = scrollView.contentOffset.y 
     self.horizontalCollectionView.delegate = nil 
     self.horizontalCollectionView.contentOffset = CGPoint(x: verticalScrollPosition, y: 0) 
     self.horizontalCollectionView.delegate = self 
    } 
} 


// MARK: CollectionViewLayout 

func collectionView(_ collectionView: UICollectionView, 
        layout collectionViewLayout: UICollectionViewLayout, 
        sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 
    if collectionView == verticalCollectionView { 
     let size = CGSize(width: collectionView.bounds.width, height: collectionView.bounds.height) 

     return size 
    } 

    if indexPath.item == 3 { 
     return CGSize(width: 70, height: 70) 
    } 
    return CGSize(width: 50, height: 50) 

} 

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
    self.verticalCollectionView.scrollToItem(at: indexPath, at: .centeredVertically, animated: true) 
    print("\(indexPath)") 
} 

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) { 
    var closestCell: UICollectionViewCell = self.horizontalCollectionView.visibleCells[0] 
    for cell in self.horizontalCollectionView.visibleCells as [UICollectionViewCell] { 
     let closestCellDelta = abs(closestCell.center.x - self.horizontalCollectionView.bounds.size.width/2.0 - self.horizontalCollectionView.contentOffset.x) 
     let cellDelta = abs(cell.center.x - self.horizontalCollectionView.bounds.size.width/2.0 - self.horizontalCollectionView.contentOffset.x) 
     if (cellDelta < closestCellDelta){ 
      closestCell = cell 
      self.centerCell = cell 
      self.cellIsInCenter = true 
      horizontalCellSize = CGSize(width: 80, height: 50) 
     } 
    } 
    let indexPath = self.horizontalCollectionView.indexPath(for: closestCell) 
    self.horizontalCollectionView.scrollToItem(at: indexPath!, at: .centeredHorizontally , animated: true) 
    if closestCell.backgroundColor == UIColor.black { 
     closestCell.backgroundColor = UIColor.blue 
    } 
    horizontalCollectionView.reloadItems(at: [self.horizontalCollectionView.indexPath(for: closestCell)!]) 
    closestCell.backgroundColor = UIColor.black 
} 
} 
+0

嘗試使用icarousel圖書館https://github.com/nicklockwood/iCarousel –

+2

謝謝易卜拉欣,我寧願解決它沒有圖書館。因爲我還在學習,所以希望這會有助於我的進步。 – FredFlinstone

回答

2

嘗試了這一點:

func collectionView(_ collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout, 
sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 
if collectionView == verticalCollectionView { 
let size = CGSize(width: collectionView.bounds.width, height: collectionView.bounds.height) 
     return size 
    } 
let point = CGPoint(x: self.view.frame.size.width/2.0, y:35) 
//or you can try this code also 
//let point = CGPoint(x: UIScreen.main.bounds.width/2, y:35) 
    print(point) 
let centerIndex = self.collectionView.indexPathForItemAtPoint(point) 
    if indexPath.item == centerIndex { 
     return CGSize(width: 70, height: 70) 
    } 
    return CGSize(width: 50, height: 50) 
} 

此代碼給你的項目在集查看中心的指標。 希望這可以幫助

+0

謝謝你的幫助。不幸的是它不工作。它只收回第一個項目。如果我打印centerIndex我得到0,0。我還想到,我們需要獲取可見單元中的中心項目索引? – FredFlinstone

相關問題