【发布时间】:2018-02-27 14:35:38
【问题描述】:
我有一个启用了 3D Touch Peek and Pop 的 UITableView。我希望能够检测到用户何时偷看视图以及何时用户停止偷看视图而不弹出。我知道previewingGestureRecognizerForFailureRelationship 存在。我试过这样使用它:
func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
// Here's where I add the gesture
let gesture = previewingContext.previewingGestureRecognizerForFailureRelationship
gesture.addObserver(self, forKeyPath: "state", options: .new, context: nil)
let sb = UIStoryboard(name: "Main", bundle: nil)
let detailVC = sb.instantiateViewController(withIdentifier: "SongPreviewViewController") as? SongPreviewViewController
return detailVC
}
然后我添加
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
// can recognize peeking and canceling commit
if let object = object {
if keyPath == "state" {
let newValue = (change![NSKeyValueChangeKey.newKey]! as AnyObject).integerValue
let state = UIGestureRecognizerState(rawValue: newValue!)!
switch state {
case .possible:
print("switch - possible")
case .began:
print("switch - began")
case .changed:
print("switch - changed")
case .ended:
print("switch - ended")
case .cancelled:
print("switch - cancelled")
case .failed:
print("switch - failed")
}
}
}
}
但每次查看视图开始时,都会调用.ended。有谁知道如何正确地做到这一点? Here's the reference I used for this code.感谢您的帮助!
【问题讨论】: