2016-09-18 25 views
2

我在我的應用程序中實現了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) 
} 

回答

0

你升級到Swift3?如果是,請檢查委託函數參數是否匹配。

此外,請記得檢查registerForPreviewing是否正確調用。

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

謝謝,但我還沒有更新Swift 3在那個項目上(而且還沒有想出這是什麼問題) – CompC

+0

啊對。如果你分享你的viewControllerForLocation委託函數,我可以用我的版本檢查它。 –

+0

我將我的代碼添加到原始帖子中。 – CompC

0

運行Swift 3 migrator。 (編輯>轉換>當前斯威夫特語法)

你似乎已經在使用雨燕3編譯器 - 如果你仍然在斯威夫特2,方法調用你說的都是工作(如registerForPreviewing(with:sourceView:)tableView.cellForRow(at:))將無法編譯,因爲這些是這些API的Swift 3語法。

如果遷移失敗,抓住它,你的委託方法雨燕3方法簽名是previewingContext(_:viewControllerForLocation:) - 注意下劃線表示有第一個參數沒有參數的標籤,所以你的聲明應該是這樣的:

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

你說得對。這是來自一箇舊版本,我試圖更新到Swift 3,但我有一些與我使用的CocoaPods有關的問題。實際構建的項目版本位於Swift 2.3中。我更新了我的原始文章,以顯示它實際上正在使用的代碼(在Swift 2.3中 - 遷移仍然會給我提供pod方面的問題) – CompC