我正在尝试使用以下代码采用UIViewController预览(Peek& Pop with 3D Touch):
id<UIViewControllerPreviewing> context = [self registerForPreviewingWithDelegate:self sourceView:self.imageView];
协议UIViewControllerPreviewing
,previewingGestureRecognizerForFailureRelationship
中有一个属性,它实际上是UIGestureRecognizer
的一个实例,让我很困惑。
正如doc所说:
通过为其实现符合UIGestureRecognizerDelegate协议的委托对象来使用此手势识别器。
但是,当我为delegate
设置previewingGestureRecognizerForFailureRelationship
时,
context.previewingGestureRecognizerForFailureRelationship.delegate = self;
发生错误:
***由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:'您无法更改UIViewControllerPreviewing失败关系手势识别器的委托'
那么如何使用这个属性?
答案 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)