【问题标题】:UICollectionView scroll outside it's viewUICollectionView 滚动到它的视图之外
【发布时间】:2017-08-03 11:20:01
【问题描述】:

我想在外部视图滚动时滚动我的收藏视图... 我找到了这个答案:How to make a collectionview respond to pan gestures outside of it's own view,但它对我不起作用。

viewController 中的代码

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    super.touchesBegan(touches, with: event)
    pager.touchesBegan(touches, with: event)
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    super.touchesMoved(touches, with: event)
    pager.touchesMoved(touches, with: event)
}

override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
    super.touchesCancelled(touches, with: event)
    pager.touchesCancelled(touches, with: event)
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    super.touchesEnded(touches, with: event)
    pager.touchesEnded(touches, with: event)
}

override func viewDidLoad() {
    super.viewDidLoad()
    pager = Carousel(withFrame: self.view.bounds, andInsets: 5)
    pager.dataSource = self
    pager.translatesAutoresizingMaskIntoConstraints = false
    pager.register(MyCollectionView.self, forCellWithReuseIdentifier: "Cell")
    self.swipeView.addSubview(pager)
    pager.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 1).isActive = true
    pager.heightAnchor.constraint(equalToConstant: 300).isActive = true
    pager.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
    pager.topAnchor.constraint(equalTo: view.topAnchor, constant: 50).isActive = true
}

请帮忙,谢谢

【问题讨论】:

  • 您的问题是什么?有错误吗?
  • 问题是:我的收藏视图没有滚动它的内容:(没有任何错误

标签: ios swift scroll uicollectionview


【解决方案1】:

您需要在您的情况下告诉您的视图Carousel 触摸在视图内

你需要重写hitTest函数并返回self carousel

class TouchCarousel: Carousel {
   override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
      return true
   }
}

然后使用您创建的类

pager = TouchCarousel(withFrame: self.view.bounds, andInsets: 5)

【讨论】:

  • @Giraffe 我刚刚更新了我的答案以启用对轮播中的单元格或项目的点击
  • 知道了)非常感谢)这对我来说非常重要:)
  • 什么是 Carousel,如何使用普通的 UICollectionview 制作它
  • @PabloMartinez 轮播是 UICollectionView :)
猜你喜欢
  • 1970-01-01
  • 2020-09-18
  • 2020-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-01
相关资源
最近更新 更多