UICollectionView在它的視圖外滾動

[英]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

我的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

請幫助,謝謝

1 个解决方案

#1


2  

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

在您的情況下,您需要告訴您的視圖Carousel觸摸在視圖內

You need to override the hitTest function and return self carousel

您需要覆蓋hitTest函數並返回自動輪播

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)

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2017/03/13/7302670d2b1001648ff864cd6a6e6ea9.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com