在 UICollectionView 上放置一个按钮并保持滚动
Place a button over UICollectionView and maintain the scroll
我有一个 UICollectionView 和一个 UIButton(在右边)。但是在按钮的框架中,collectionView 失去了滚动 属性。
我想知道是否有任何方法可以同时保持这两种交互,Button 在内部触摸和 CollectionView 滚动。
使用带有轻击手势识别器的视图代替按钮,并让该轻击手势识别器与滚动视图的平移手势同时工作。
好吧,我想出了如何做到这一点,但我使用了 UITapGestureRecognizer 而不是按钮,因为 rounak 建议:
let tapGesture = UITapGestureRecognizer(target: self, action: "collectionViewDidReceiveTap:")
tapGesture.delegate = self
self.collectionView.addGestureRecognizer(tapGesture)
因为我想要一个特定的矩形 "tapable" 我已经实现了 UIGestureRecognizerDelegate 协议,这段代码是这样做的:
public func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {
let collectionViewCurrentCell = self.collectionView.layoutAttributesForItemAtIndexPath(NSIndexPath(forItem: Int(self.currentPage), inSection: 0))
let cellFrameInSuperview = collectionView.convertRect(collectionViewCurrentCell!.frame, toView: self)
if gestureRecognizer.isKindOfClass(UITapGestureRecognizer) && CGRectContainsPoint(cellFrameInSuperview, touch.locationInView(self)) {
return false
}
return true
}
cellFrameInSuperview 应该是 TapGesture 不应该做任何事情的区域。但与 UIButton 不同的是,滚动在所有 Collection 范围内继续工作。
如果您对它的工作原理或原因有任何疑问,请发表评论。
我有一个 UICollectionView 和一个 UIButton(在右边)。但是在按钮的框架中,collectionView 失去了滚动 属性。
我想知道是否有任何方法可以同时保持这两种交互,Button 在内部触摸和 CollectionView 滚动。
使用带有轻击手势识别器的视图代替按钮,并让该轻击手势识别器与滚动视图的平移手势同时工作。
好吧,我想出了如何做到这一点,但我使用了 UITapGestureRecognizer 而不是按钮,因为 rounak 建议:
let tapGesture = UITapGestureRecognizer(target: self, action: "collectionViewDidReceiveTap:")
tapGesture.delegate = self
self.collectionView.addGestureRecognizer(tapGesture)
因为我想要一个特定的矩形 "tapable" 我已经实现了 UIGestureRecognizerDelegate 协议,这段代码是这样做的:
public func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {
let collectionViewCurrentCell = self.collectionView.layoutAttributesForItemAtIndexPath(NSIndexPath(forItem: Int(self.currentPage), inSection: 0))
let cellFrameInSuperview = collectionView.convertRect(collectionViewCurrentCell!.frame, toView: self)
if gestureRecognizer.isKindOfClass(UITapGestureRecognizer) && CGRectContainsPoint(cellFrameInSuperview, touch.locationInView(self)) {
return false
}
return true
}
cellFrameInSuperview 应该是 TapGesture 不应该做任何事情的区域。但与 UIButton 不同的是,滚动在所有 Collection 范围内继续工作。
如果您对它的工作原理或原因有任何疑问,请发表评论。