Search code examples
iosswiftuisegmentedcontrolcollectionview

Swift Collection view Horizontal and vertical scroll


i want to make this Collection view that can scroll horizontal and vertical....but i wanna make something like this

https://giphy.com/gifs/lWshqs2BuvilYuE4Fl/html5

from what i see theres like Collection view and some segmented controll on top of that Collection view...... i mean my only problem is how can i scroll horizontal from anywhere on screen?

like i dont have to scroll from that top segmented control, i can also swipe it from that collection view thing

Any idea how to make this? cause i really dont know if im right or not.....

Thanks

*edit, i cant use library, so i guess i need something that really do it by manual, and for the horizontal scroll i need a dynamic data, i dunno paging controller can use a dynamic data cause as long as i know its static data.


Solution

    • take a viewcontroller

    • add a view2 give it top constrain and height to 44

    • add scrollview and give apropreate constrain (programaticaly or using storyborad choice is your)

    • add 3 buttons for voucher , subscription , misson .

    • create 3 diffrent ViewControllers for voucher, subscription, misson.

    • add 3 viewcontrollers in scrollview

    // define this variable globally
    var previousPageXOffset: CGFloat = 0.0
    var pagenumber = 0
    
    // add this in viewdidload
    let aViewController = self.storyboard!.instantiateViewController(withIdentifier: "voucher") as! voucher
    
      let bViewController = self.storyboard!.instantiateViewController(withIdentifier: "subscription") as! subscription
      
      let cViewController = self.storyboard!.instantiateViewController(withIdentifier: "misson") as! misson
      
      let bounds = UIScreen.main.bounds
      let width = bounds.size.width
      let height = CGFloat(1.0)
      
      let viewControllers = [aViewController, bViewController, cViewController]
      
      self.scrollview!.contentSize = CGSize(width: CGFloat(self.totalscreen)*width, height: height)
      
      var idx:Int = 0
      
      for viewController in viewControllers {
          self.addChild(viewController)
          let originX:CGFloat = CGFloat(idx) * width
          viewController.view.frame = CGRect(x: originX, y: 0, width: self.scrollview.frame.width, height: self.scrollview.frame.height)
          self.scrollview!.addSubview(viewController.view)
          print("change")
          viewController.didMove(toParent: self)
          idx+=1
      }
    
    // delegate for scrollview 
    
    extension yourVC: UIScrollViewDelegate{
      
      func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
          
          previousPageXOffset = scrollView.contentOffset.x
      }
      
      func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
          
          let targetOffset = targetContentOffset.pointee
          
          if targetOffset.x == previousPageXOffset {
              // page will not change
          } else if targetOffset.x < previousPageXOffset {
              print("left")
              pagenumber = pagenumber - 1
          } else if targetOffset.x > previousPageXOffset {
              print("ryt")
              pagenumber = pagenumber + 1
          }
          
          if pagenumber <= 0 {
              
              //set buttons colors or bottom line in your case if first page is selected
              
          }else if pagenumber == 1{
              //set buttons colors or bottom line in your case if second page is selected
          }else{
              //set buttons colors or bottom line in your case if third page is selected
          }
          print(pagenumber)
          previousPageXOffset = targetOffset.x
          print(previousPageXOffset)
      }
    }
    
    
    
    • function for spacific button(top 3 buttons) click
        func scrollToPage(page: Int, animated: Bool) {
            var frame: CGRect = self.scrollview.frame
            frame.origin.x = frame.size.width * CGFloat(page)
            frame.origin.y = 0
            self.scrollview.scrollRectToVisible(frame, animated: animated)
        }
    

    i hope it will work for you :)