gpt4 book ai didi

ios - 为 previewingGestureRecognizerForFailureRelationship 设置委托(delegate)会引发异常

转载 作者:行者123 更新时间:2023-11-28 21:10:13 25 4
gpt4 key购买 nike

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

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

UIViewControllerPreviewing协议(protocol)中有一个属性previewingGestureRecognizerForFailureRelationship,它实际上是UIGestureRecognizer的一个实例,让我很困惑。

作为doc说:

Use this gesture recognizer by implementing a delegate object for it that conforms to the UIGestureRecognizerDelegate protocol.

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

context.previewingGestureRecognizerForFailureRelationship.delegate = self;

发生错误:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'You cannot change the delegate of the UIViewControllerPreviewing failure relationship gesture recognizer'

那么如何使用这个属性呢?

最佳答案

似乎我在这方面晚了几个月,但它仍然可以帮助将来的人。

当您调用 registerForPreviewing 时在你的 View Controller 上,你得到一个符合 UIViewControllerPreviewing 的上下文对象。协议(protocol)。您可以存储对该上下文的 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)

关于ios - 为 previewingGestureRecognizerForFailureRelationship 设置委托(delegate)会引发异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43817940/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com