2017-06-02 100 views
0

我有要求通過單擊「右」按鈕將UICollectionView單元滾動到數組中的下5個項目並顯示前5次點擊「左鍵」。通過單擊左側和右側按鈕動作,將UICollectionView的單元格滾動到左右方向

enter image description here

誰能幫我借給予任何建議,使其在迅速possbile。

下面是寫的代碼,以水平滾動顯示集合視圖的列表。

override func viewDidLoad() { 

     super.viewDidLoad() 

     self.arrFalconList = ["ic_falcon_1","ic_falcon_2","ic_falcon_3","ic_falcon_4","ic_falcon_5","ic_falcon_6","ic_falcon_7"] 
    } 



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

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

     let aFalconCell = collectionView.dequeueReusableCell(withReuseIdentifier: "FalconCollectionViewCell", for: indexPath) as! FalconCollectionViewCell 

     aFalconCell.imgFalcon.image = UIImage(named: self.arrFalconList[indexPath.row]) 
     aFalconCell.lblFalcon.text = self.arrFalconList[indexPath.row] 
     aFalconCell.imgFalcon.layer.cornerRadius = ((aFalconCell.imgFalcon.frame.width)/2) 
     aFalconCell.imgFalcon.clipsToBounds = true 

     if aFalconCell.isSelected { 
      aFalconCell.imgFalcon.layer.borderColor = UIColor.init(red: 241.0/255.0, green: 55.0/255.0, blue: 61.0/255.0, alpha: 1.0).cgColor 
      aFalconCell.imgFalcon.layer.borderWidth = 2 
     }else{ 
      aFalconCell.imgFalcon.layer.borderColor = UIColor.clear.cgColor 
      aFalconCell.imgFalcon.layer.borderWidth = 0 
     } 


     return aFalconCell 

    } 

請分享你的答案來處理「左」 &「右」按鈕單擊事件。

回答

6

我找到了查詢的解決方案。

下面的代碼將有助於達到要求。

@IBAction func btnLeftArrowAction(_ sender: Any) { 
     let collectionBounds = self.collectionView.bounds 
     let contentOffset = CGFloat(floor(self.collectionView.contentOffset.x - collectionBounds.size.width)) 
     self.moveCollectionToFrame(contentOffset: contentOffset) 

    } 

    @IBAction func btnRightArrowAction(_ sender: Any) { 

     let collectionBounds = self.collectionView.bounds 
     let contentOffset = CGFloat(floor(self.collectionView.contentOffset.x + collectionBounds.size.width)) 
     self.moveCollectionToFrame(contentOffset: contentOffset) 

    } 

    func moveCollectionToFrame(contentOffset : CGFloat) { 

     let frame: CGRect = CGRect(x : contentOffset ,y : self.collectionView.contentOffset.y ,width : self.collectionView.frame.width,height : self.collectionView.frame.height) 
     self.collectionView.scrollRectToVisible(frame, animated: true) 
    } 
+0

如何歸檔目標C相同的結果。 @Akash Thakkar –

0

你可以試試這個代碼

let I = 1 
    func next_btn_clicked() 
{ 
clooectionview.setContentOffset(CGPoint(x: (i) * clooectionview.frame.size.width, y: 0), animated: true) 
I = I + 1 
}