UICollectionView滚动到它的视图之外

时间:2017-03-13 12:29:46

标签: ios swift scroll uicollectionview

我想在外部视图滚动上滚动我的集合视图... 我找到了这个答案: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
}

请帮助,谢谢

1 个答案:

答案 0 :(得分:2)

在您的案例中,您需要告诉您的观点Carousel触摸在视图中

您需要覆盖hitTest功能并返回自己的轮播

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

然后使用您创建的类<​​/ p>

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