未调用位置的 3D 触摸视图控制器

Posted

技术标签:

【中文标题】未调用位置的 3D 触摸视图控制器【英文标题】:3D Touch View Controller For Location not being called 【发布时间】:2016-09-18 05:16:56 【问题描述】:

我在我的应用程序中实现了 3D 触摸,通过将我的视图控制器与我的表格视图注册为源矩形。自从我实施它以来,过去几周它一直运行良好。

但我只是尝试了一下,发现它似乎不再起作用了。我没有接触任何代码,所以我不知道问题是什么。

视图控制器肯定是为UIViewControllerPreviewing 注册的,但previewingContext(previewingContext: viewControllerForLocation:) 甚至没有被调用!

我不知道除了注册预览之外还有什么要做的设置,而且我所做的任何事情似乎都不会触发该方法。我有一个单独的视图控制器,其中 3D 触摸似乎工作正常,但我没有做任何不同的事情。

有没有人知道为什么该方法甚至没有被调用?这真的让我很沮丧,因为它似乎已经停止工作,因为它之前工作正常。谢谢。

这是我的previewingContext(previewingContext: viewControllerForLocation:) 方法的代码:

func previewingContext(previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? 

    let identifier = "ClipPlayerViewController"

    guard let indexPath = tableView.indexPathForRowAtPoint(location), let destination = storyboard?.instantiateViewControllerWithIdentifier(identifier) as? ClipPlayerViewController else  return nil 

    let effect = filteredEffectsSortedBySection[indexPath.section].effects[indexPath.row]

    switch effect.mediaType 
    case .Video:
        destination.effect = effect

        destination.showsPlaybackControls = false

        if let thumbnail = thumbnailsForEffects[effect], let unwrappedThumbnail = thumbnail 
            destination.preferredContentSize = CGSizeMake(unwrappedThumbnail.size.width - 2, unwrappedThumbnail.size.height - 2)
         else if let effectSize = destination.player?.currentItem?.asset.tracksWithMediaType(AVMediaTypeVideo).first?.naturalSize 
            destination.preferredContentSize = effectSize
        

        previewingContext.sourceRect = tableView.cellForRowAtIndexPath(indexPath)!.frame

        return destination

    case .Texture:
        return nytPhotosViewControllerForEffectAtIndexPath(indexPath)
    

这是我注册预览的代码,在viewDidLoad()中调用:

if  traitCollection.forceTouchCapability == .Available         
    registerForPreviewingWithDelegate(self, sourceView: tableView)

【问题讨论】:

【参考方案1】:

你升级到 Swift3 了吗?如果是,请检查委托函数参数是否匹配。

另外,记得检查 registerForPreviewing 是否被正确调用。

   registerForPreviewing(with: self as! UIViewControllerPreviewingDelegate, sourceView: view)

【讨论】:

谢谢,但我还没有在那个项目上更新到 Swift 3(而且还没有弄清楚这是什么问题) 啊对。如果您共享您的 viewControllerForLocation 委托函数,我可以使用我的有效版本进行检查。 酷! registerForPreviewing(with: self, sourceView: view) 确定触发了吗?而且,如果你放一个断点,viewControllerForLocation 的第一行会被命中吗? 是的,正在调用 registerForPreviewing(with: self, sourceView: tableView)。 viewControllerForLocation 不是。【参考方案2】:

运行 Swift 3 迁移器。 (编辑 > 转换 > 到当前的 Swift 语法)

您似乎已经在使用 Swift 3 编译器——如果您仍在使用 Swift 2,那么您说正在运行的方法调用(例如 registerForPreviewing(with:sourceView:)tableView.cellForRow(at:))将无法编译,因为这些是 Swift 3 语法对于那些 API。

如果迁移器未能捕获它,则委托方法的 Swift 3 方法签名是 previewingContext(_:viewControllerForLocation:) — 请注意下划线表示第一个参数上没有参数标签,因此您的声明应如下所示:

func previewingContext(_ previewingContext: UIViewControllerPreviewing, 
    viewControllerForLocation location: CGPoint) -> UIViewController?

【讨论】:

你是对的。那是我尝试更新到 Swift 3 的旧版本,但我在使用的一些 CocoaPods 中遇到了一些问题。实际构建的项目版本是 Swift 2.3。我更新了我的原始帖子以显示它当前实际使用的代码(在 Swift 2.3 中——迁移仍然给我带来 pod 问题)

以上是关于未调用位置的 3D 触摸视图控制器的主要内容,如果未能解决你的问题,请参考以下文章