UPScroll in UICollectionView
Sometimes we need to provide the Upscroll in UICollectionView, when we scroll down to UICollectionView approx seeing 100 or 200 items than again to come up we need to scroll upside at same time so it’s better to provide the icon where we go down at some count we can show the icon ,or on up we can hide the icon.
For this we need to do like that:
1: First we need to set icon in mainView:
1 2 3 4 5 6 7 |
let upArrow = UIImageView(frame: CGRect(x: SCREEN_WIDTH - 70, y:SCREEN_HEIGHT - 100, width: 40, height: 40)) upArrow.image = UIImage(named: "ic_ScrollUp") self.view.addSubview(upArrow) upArrow.isUserInteractionEnabled = true; let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.scorllUp)) upArrow.addGestureRecognizer(tapGesture) upArrow.isHidden = true; |
1: we need to observe on each scroll like that :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
func scrollViewDidScroll(_ scrollView: UIScrollView) { let currentCellCount = self.productCollectionView.numberOfItems(inSection: 0) for cell: UICollectionViewCell in self.productCollectionView.visibleCells { indexPathValue = self.productCollectionView.indexPath(for: cell)! if indexPathValue.row == self.productCollectionView.numberOfItems(inSection: 0) - 1 { if (totalCount > currentCellCount) && loadPageRequestFlag{ loadPageRequestFlag = false pageNumber += 1; callingHttppApi() // its call api for next data } } if cell.frame.origin.y > SCREEN_HEIGHT - 50{ upArrow.isHidden = false }else{ upArrow.isHidden = true } } } |
3: So now we have to define the touch event of icon:
1 2 3 4 5 |
func scorllUp(_sender : UITapGestureRecognizer){ let indexPath = IndexPath(row: 0, section: 0) productCollectionView.scrollToItem(at: indexPath, at: .top, animated: true) self.navigationController?.setNavigationBarHidden(false, animated: true) } |
4: On click, we directly come to the first cell.