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

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

我正在尝试使用以下代码采用 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/

相关文章:

ios - Xcode5:无法注册“/System/Library/CoreServices/CoreTypes.bundle/Contents/Library/MobileDevices.bundle

iphone - Objective C - 在主线程上调用类方法?

ios - 使用 CoreGraphics 而不是 SpriteKit 的原因?

ios - 将 CAShapeLayer 角上的破折号与虚线边框对齐

iphone - 更改相机胶卷取消按钮的语言

ios - 在 cocos2d 中禁用图层触摸

objective-c - 将 NSStrings 的 NSArray 与另一个 NSStrings 的 NSArray 进行比较

ios - 使用 SKPhysics body 检测碰撞

iphone - 重新排序单元格后重新加载自定义 UITableViewCell

ios - 获取文档目录中特定文件夹的内容