2017-07-07 45 views
4

的設置:連續中心UICollectionView細胞垂直和水平每當新出現

我現在有一個UICollectionView是對細胞模仿Apple關注主屏幕的UI一串圓圈。我遵循this guide。如果您沒有閱讀文章,或者出於某種原因而出現故障,那麼它基本上是一個UICollectionView,當您將它們從中心向外捲動時,外部卷標會被縮小。

問題:

我想氣泡彈出動態,使得每當用戶贏得一個獎項,該獎項「啪啪」,即在動畫,縮放從0到1。但是,我想什麼細胞可能總是居中。因此,當例如,用戶從1項跳到二,我希望在新的一個在彈出的第一個向左移動

我已經試過:

我曾嘗試實施UICollectionViewinsetForSectionAt方法,但它似乎不起作用。我懷疑這是因爲CollectionView不是FlowLayout。我試圖把它變成一個,但我最終崩潰的一切......

驗證碼:

以下是我已經實現了,以防萬一文章下降。 (濃縮爲了便於閱讀):

class CollectionViewLayout: UICollectionViewLayout { 
    //Invalidate the layout every time the bounds change so we can do the cool changes 
    override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool { 
     return true 
    } 

    override var collectionViewContentSize: CGSize { 
     return CGSize(width: self.itemSize * CGFloat(COLS), 
        height: self.itemSize * CGFloat(ROWS)) 
    } 

    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? { 
     var attributes: [UICollectionViewLayoutAttributes] = [] 
     for i in 0..<cellCount { 
      let indexPath = IndexPath(item: i, section: 0) 
      attributes.append(self.layoutAttributesForItem(at: indexPath)!) 
     } 

     return attributes 
    } 

    override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? { 
     var attributes = UICollectionViewLayoutAttributes(forCellWith: indexPath) 

     // **Code where I create "z" variable by using equation in article, where "z" is just a value [0,1] that determines cell's scale** 

     // **More code where I Calculate "x" and "y" based on indexPath like in article**  

     //Set the cell's attributes: 
     attributes.transform = CGAffineTransform(scaleX: z, y: z) 
     attributes.size = CGSize(width: self.itemSize, height: self.itemSize) 
     attributes.center = CGPoint(x: x, y: y) 

     return attributes 
    } 

} 
+0

如何將UICollectionView的屬性isPagingEnabled設置爲true? –

+0

我爲我的另一個項目做了這個。與此問題是,分頁捕捉到屏幕寬度的增量,單元格非常小,垂直以及水平捕捉 – QuantumHoneybees

+0

如果是這樣,那麼這可能有助於https://stackoverflow.com/questions/42486960/uicollectionview-水平分頁與空間之間的頁面 –

回答

2

我相信你的問題的解決方案將是滾動採集以什麼獎你想,因爲集合視圖爲中心的indexPath是一個滾動的子類視圖。

collectionView?.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true) 

其中indexPath是,你想滾動到indexPath:您可以通過滾動集合視圖的項目。這會自動爲您的滾動動畫,但如果您想自定義您可以調用UIView.animate函數