0

I need to hide my UISegmentedControl in my UICollectionView when the collection is scrolling. Below my snippet code :

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

.....
.....
if (collectionViewMovie.contentOffset.y > 100) {
        self.segmentedControl.isHidden = true
    } else if (collectionViewMovie.contentOffset.y < 100) {
        self.segmentedControl.isHidden = false
    }
}

Its work to hide when the collection is scrolling. But sometimes the segmented is not show when I scroll slowly to the top. Its not smooth too when hidden the UISegmentedControl. I read this but its on Objective C. I read this to but its on UIScrollView. Any suggest and answer will help for me. Thanks in Advance

*Edit

 func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
    if scrollView.panGestureRecognizer.translation(in: self.view).y < 0 {
        self.segmentedControl.isHidden = true
    } else {
    self.segmentedControl.isHidden = false
    }
}

Now its always show or hide when scrolling even smooth scroll, but there is stil no animation. Thanks again in advance

MrX
  • 953
  • 2
  • 16
  • 42
  • `UICollectionView` is a subclass of `UIScrollView` so you could use the `UIScrollView` property `isDragging` to set your segmented control's `isHidden` property. You could also use the `UIScrollViewDelegate` methods `scrollViewWillBeginDragging` and `scrollViewWillEndDragging` to show/hide your segemented control. – Dennis W. Jun 25 '17 at 20:30
  • @DennisW. so i must create a `UIScrollView` programaticaly at `viewDidLoad`? But i think it will be weird because the `UICollectionView` has own `Scroll` . Its make me confuse -.- Sorry im new in swift (for 4 month) – MrX Jun 27 '17 at 01:16
  • No, a `UICollectionView` is a type of `UIScrollView` ( see https://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)). You can just make your collection view conform to the `UIScrollViewDelegate` protocol to have access to the methods I mentioned. You already have access to the `isDragging ` property. – Dennis W. Jun 27 '17 at 03:30
  • @DennisW. oh i see, i will try it tonight. I will give the result ASAP. Thanks for your suggest – MrX Jun 27 '17 at 04:28
  • @DennisW. I read the site that you recomended for me. With some modificationi its working in my case now. You can accept its as answer so I can mark as accpept answer – MrX Oct 22 '17 at 07:30

0 Answers0