简体   繁体   English

Swift Collection 视图水平和垂直滚动

[英]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我想让这个 Collection 视图可以水平和垂直滚动......但我想做这样的事情

https://giphy.com/gifs/lWshqs2BuvilYuE4Fl/html5 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. *编辑,我不能使用库,所以我想我需要一些真正由手动完成的东西,对于水平滚动,我需要一个动态数据,我不知道分页控制器可以使用动态数据原因,只要我知道它的静态数据.

  • take a viewcontroller拿一个视图控制器

  • add a view2 give it top constrain and height to 44添加一个 view2 给它顶部约束和高度为 44

  • add scrollview and give apropreate constrain (programaticaly or using storyborad choice is your)添加滚动视图并提供适当的约束(以编程方式或使用故事板选择是您的)

  • add 3 buttons for voucher , subscription , misson .为凭证、订阅、任务添加 3 个按钮。

  • create 3 diffrent ViewControllers for voucher, subscription, misson.为凭证、订阅、任务创建 3 个不同的 ViewController。

  • add 3 viewcontrollers in scrollview在滚动视图中添加 3 个视图控制器

// 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空间按钮功能(前 3 个按钮)点击
    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 :)我希望它对你有用:)

add collection view to every single view controller(screen), and apply Parchment framework for your required output.将集合视图添加到每个视图控制器(屏幕),并为所需的输出应用Parchment框架。

you can get more information about Parchment on :- https://github.com/rechsteiner/Parchment您可以在以下位置获取有关 Parchment 的更多信息:- https://github.com/rechsteiner/Parchment

attaching some similar frameworks (select according to your requirement)附加一些类似的框架(根据您的要求选择)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM