为previewingGestureRecognizerForFailureRelationship设置委托会引发异常

时间:2017-05-06 07:31:45

标签: ios objective-c cocoa-touch uikit 3dtouch

我正在尝试使用以下代码采用UIViewController预览(Peek& Pop with 3D Touch):

id<UIViewControllerPreviewing> context = [self registerForPreviewingWithDelegate:self sourceView:self.imageView];

协议UIViewControllerPreviewingpreviewingGestureRecognizerForFailureRelationship中有一个属性,它实际上是UIGestureRecognizer的一个实例,让我很困惑。

正如doc所说:

  

通过为其实现符合UIGestureRecognizerDelegate协议的委托对象来使用此手势识别器。

但是,当我为delegate设置previewingGestureRecognizerForFailureRelationship时,

context.previewingGestureRecognizerForFailureRelationship.delegate = self;

发生错误:

  

***由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:'您无法更改UIViewControllerPreviewing失败关系手势识别器的委托'

那么如何使用这个属性?

1 个答案:

答案 0 :(得分:2)

似乎我已经迟到了几个月,但它可能会在将来帮助某人。

当您在视图控制器上调用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)