2015-12-01 66 views
1

我有一個與圖像集合視圖。我想執行按鈕按下滾動。我已經搜索谷歌我發現只設置contentoffset,但我想滾動動畫(滾動應該反彈,如果內容是通過滾動區域)。我已經添加image告訴你我到底要做什麼,在那裏圖片是一個集合視圖,兩側的小箭頭都是按鈕。如何在按鈕上滾動(水平)集合視圖?

@IBAction func moveScrollLeft(sender: UIButton) { 
    UIView.animateWithDuration(0.2, delay: 0, options: UIViewAnimationOptions.CurveEaseOut, animations: { 
     self.imageCollectionView.contentOffset.x -= 50 
     }, completion: nil) 
    print(imageCollectionView.contentOffset.x) 
} 

@IBAction func moveScrollRight(sender: UIButton) { 
    UIView.animateWithDuration(0.2, delay: 0, options: UIViewAnimationOptions.CurveEaseOut, animations: { 
     self.imageCollectionView.contentOffset.x += 50 
     }, completion: nil) 
    print(imageCollectionView.contentOffset.x) 

} 
+2

UICollectionView是的UIScrollView的子類。你可以使用collectionView.setContentOffset(offset,animated:true)。關於反彈,您必須檢查contentOffset.x + bounds.width> contentSize.width併爲反彈製作動畫。 –

回答

0

請檢查該代碼,我修改代碼:

@IBAction func moveScrollLeft(sender: UIButton) { 

     self.imageCollectionView.contentOffset.x -= 50 

    UIView.animateWithDuration(0.2, delay: 0, options: UIViewAnimationOptions.CurveEaseOut, animations: { 
     }, completion: nil) 
      self.view.layoutIfNeeded() 

    print(imageCollectionView.contentOffset.x) 
} 

@IBAction func moveScrollRight(sender: UIButton) { 

     self.imageCollectionView.contentOffset.x += 50 

    UIView.animateWithDuration(0.2, delay: 0, options: UIViewAnimationOptions.CurveEaseOut, animations: { 
    self.view.layoutIfNeeded() 

     }, completion: nil) 

    print(imageCollectionView.contentOffset.x) 

}