Giraffe
Giraffe

Reputation: 15

UICollectionView scroll outside it's view

I want to scroll my collection view on scroll of outside view... I found this answer: How to make a collectionview respond to pan gestures outside of it's own view but it doesn't works on me.

Code in my 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
}

Help please, thanks

Upvotes: 1

Views: 833

Answers (1)

zombie
zombie

Reputation: 5259

You need to tell your view Carousel in your case that the touch is inside the view

You need to override the hitTest function and return self carousel

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

then use the class that you created

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

Upvotes: 2

Related Questions