我有一个UICollectionView,它通常包含100-200个单元格,可以水平和垂直滚动。当用户执行LongPress手势以允许他们轻松导航到UICollectionView中的几个特定单元格时,我试图提供一个带有3个图标的简单UIView弹出窗口。
我希望LongPress启动弹出窗口,然后用户将手指拖动到三个图标中的一个并释放以选择该图标。
我遇到的问题是我无法弄清楚如何获得包含3个图标的视图或图标本身以响应LongPress手势的结束。
这是UIViewController的简化:
class MyViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
@IBOutlet weak var myCollectionView: UICollectionView!
@IBAction func longTouch(_ sender: UILongPressGestureRecognizer) {
if sender.state == .began {
self.menu!.frame = CGRect(x: sender.location(in: self.view).x - 20,
y: sender.location(in: self.view).y - 75,
width: 100, height: 100)
self.menu!.isHidden = false
}
else if sender.state == .ended {
self.menu!.isHidden = true
}
}
// Popup for long touch
var menu: UIView?
// MARK: UIViewController
override func viewDidLoad() {
super.viewDidLoad()
self.initMenu()
}
// MARK:- UICollectionViewDelegate
...
// MARK:- UICollectionViewDataSource
...
// MARK:- UIScrollViewDelegate
...
private func initMenu() -> Void {
self.menu = UIView()
self.menu!.isUserInteractionEnabled = true
self.menu!.frame = CGRect(x: 0,
y: 0,
width: 100,
height: 50)
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(menuLongPressHandler))
self.menu!.addGestureRecognizer(longPress)
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap(sender:)))
self.menu!.addGestureRecognizer(tapGesture)
// There would be ImageView icons here as well, which I've left out for simplicity
self.menu!.isHidden = true
}
func menuLongPressHandler() {
print("Long Press")
}
func handleTap(sender: UITapGestureRecognizer) {
print("Tap")
}
}
当LongPress在视图上方结束时,不会打印“Tap”或“Long Press”。有关如何获取视图以捕获LongPress手势结束的任何建议?