我正在尝试使用以下代码来采用UIViewController预览(带有3D Touch的Peek&Pop):
id<UIViewControllerPreviewing> context = [self registerForPreviewingWithDelegate:self sourceView:self.imageView];
协议
UIViewControllerPreviewing
中有一个属性,previewingGestureRecognizerForFailureRelationship
实际上是UIGestureRecognizer
的一个实例,这让我很困惑。正如doc所说:
通过实现与UIGestureRecognizerDelegate协议一致的委托对象来使用此姿势识别器。
但是,当我将
delegate
设置为previewingGestureRecognizerForFailureRelationship
时,context.previewingGestureRecognizerForFailureRelationship.delegate = self;
发生错误:
***由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序,原因:“您无法更改UIViewControllerPreviewing失败关系手势识别器的委托”
那么如何使用此属性?
最佳答案
似乎我在这方面迟到了几个月,但将来仍然可能对某人有所帮助。
当在视图控制器上调用registerForPreviewing
时,您将获得一个符合UIViewControllerPreviewing
协议的上下文对象。您可以存储对该上下文的previewingGestureRecognizerForFailureRelationship
的引用,以在您的UIGestureRecognizerDelegate
方法中使用,或要求其他手势失败。
//When registering for previewing
let previewingContext = self.registerForPreviewing(with delegate: self, sourceView: self.view)
self.previewingGestureRecognizer = previewingContext. previewingGestureRecognizerForFailureRelationship
//Requiring failure for another gesture to be recognized
let longPressGesture = UILongPressGestureRecognizer(target: self.view, action: #selector(handleLongPress(_ :)))
longPressGesture.require(toFail: self.previewingGestureRecognizer)
self.view.addGestureRecognizer(longPressGesture)