-1

i am scrolling at left and right side in collectionView by click button horizontally, it works well, but i do not know how to hide button when is no more item in a side of collectionView. the previewButton will hide when no more item in left side and the next button will hide when no more item in right side. here is code of left and right button

let arrow_leftBtn: UIButton = {
    let btn = UIButton()
    btn.setImage(UIImage(named: "arrow_left"), for: .normal)

    btn.addTarget(self, action: #selector(arrowLeftBtnClick), for: .touchUpInside)
    return btn

}()

func arrowLeftBtnClick() {
    let collectionBounds = self.collectionView?.bounds
    let contentOffset = CGFloat(floor((self.collectionView?.contentOffset.x)! - (collectionBounds?.size.width)!))
    self.moveToFrame(contentOffset: contentOffset)
}

let arrow_rightBtn: UIButton = {
    let btn = UIButton()
    btn.setImage(UIImage(named: "arrow_right"), for: .normal)


    btn.addTarget(self, action: #selector(arrowRightBtnClick), for: .touchUpInside)
    return btn


}()


func arrowRightBtnClick() {

    let collectionBounds = self.collectionView?.bounds
    let contentOffset = CGFloat(floor((self.collectionView?.contentOffset.x)! + (collectionBounds?.size.width)!))
    self.moveToFrame(contentOffset: contentOffset)
}

func moveToFrame(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)


}


func setupLeftAndRightArrow() {



    view.addSubview(arrow_leftBtn)
    view.addSubview(arrow_rightBtn)

    view.addConstraintsWithFormat("H:|-20-[v0(20)]", views: arrow_leftBtn)
    view.addConstraintsWithFormat("V:|-180-[v0(20)]|", views: arrow_leftBtn)

    view.addConstraintsWithFormat("H:[v0(20)]-30-|", views: arrow_rightBtn)
    view.addConstraintsWithFormat("V:|-180-[v0(20)]|", views: arrow_rightBtn)
}

enter image description here

Quiet Islet
  • 536
  • 1
  • 8
  • 28
  • You want to hide next and previous button which used to scroll `UICollectionView`? – Mathi Arasan Aug 23 '17 at 11:17
  • see this https://stackoverflow.com/questions/29362915/uiscrollview-with-left-and-right-arrows-in-ios – Anbu.Karthik Aug 23 '17 at 11:19
  • the previewButton will hide when no more item in left side and the next button will hide when no more item in right side – Quiet Islet Aug 23 '17 at 11:19
  • @user3589771 i edit my question please check it again – Quiet Islet Aug 23 '17 at 11:25
  • @user3589771 i added image of project please check it – Quiet Islet Aug 23 '17 at 11:32
  • If `UICollectionViewItem` has full-width use collection view item count `(array.count-1)*screen_width==currentcontentOffset` for rightbutton and `0==contentOffset` for left side button. [Check this answer for swipe](https://stackoverflow.com/a/31857825/3589771) – Mathi Arasan Aug 23 '17 at 11:32
  • @user3589771 please look at image which i added newly. it is scrolling by button click but i do not know how to hide the button when scrolling is finish – Quiet Islet Aug 23 '17 at 11:36
  • We have only option to find scroll end or not, Use `scrollViewDidEndDecelerating` instead of `scrollViewDidScroll` method. Still, it's not what you want kindly update your question. You didn't mention any thing related to scroll to hide. – Mathi Arasan Aug 23 '17 at 11:46

2 Answers2

3

Implement UICollectionViewDelegate method:

func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath)
{
    arrow_rightBtn.isHidden = (indexPath.row == collectionView.numberOfItems(inSection: 0) - 1)
    arrow_leftBtn.isHidden = (indexPath.row == 0)
}

This will hide the arrow_rightBtn when last cell is visible and hide arrow_leftBtn when first cell is visible.

PGDev
  • 23,751
  • 6
  • 34
  • 88
  • come to here https://stackoverflow.com/questions/48970198/how-to-detect-one-button-in-tableview-cell/48971060#48971060 – Quiet Islet Feb 25 '18 at 07:50
  • @QuietIslet Can you please explain the link you provided? I don't understand. – PGDev Feb 25 '18 at 07:53
  • How to detect just one button in tableview cell, here detect multiple button, https://stackoverflow.com/questions/48970198/how-to-detect-one-button-in-tableview-cell?noredirect=1#comment84948065_48970198 – Quiet Islet Feb 26 '18 at 03:17
  • @QuietIslet I've answered your question in the provided link. Have a look and let me know if you still face any issues regarding this. – PGDev Feb 26 '18 at 06:52
1

You can use collectionView delegate method to detect first last cells

override func collectionView(collectionView: UICollectionView, willDisplayCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
    if indexPath.row == dataSource.count - 1 {
        // Last cell is visible
        leftBtn.isHidden = false
        rightBtn.isHidden =true
    } else if indexPath.row == 0 {
        // First cell is visible
        leftBtn.isHidden = true
        rightBtn.isHidden = false
    } else {
        //for others cells
        leftBtn.isHidden = false
        rightBtn.isHidden =false

    }
}
Baig
  • 4,737
  • 1
  • 32
  • 47