2017-08-15 17 views
0

コレクションビューまたはテーブルビューにない単一の写真に3Dタッチを追加できますか?私が見ているすべてのものがコレクションビューを使用しています。シングル写真の3Dタッチ。 iOS、Swift

私は情報があるビューに1枚の画像を表示していますが、画像は小さな正方形です。写真をタップでき、ビューコントローラーで開くことができますが、

答えて

1

はい、すべてあなたが行う必要があるのはUIViewControllerPreviewingDelegateです。深刻なプレスが登録されたときに

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

というメソッドが呼び出されます。非nilビューコントローラを返すと、ピークとポップのやりとりが開始されます。

たとえば、ここではViewControllerを小さな中央の画像ビューとして示します。ビューが3Dに触れると、その位置がイメージビュー内にあるかどうかを確認することができます。もしそうなら、私たちは次のビューコントローラをプレビューすることができます:OtherViewControllerは、ビュー全体を占める画像ビューとビューコントローラである

class ViewController: UIViewController { 

    @IBOutlet weak var imageView: UIImageView! 

    override func viewDidLoad() 
    { 
     super.viewDidLoad() 
     if traitCollection.forceTouchCapability == .available 
     { 
      registerForPreviewing(with: self, sourceView: view) 
     } 
     else 
     { 
      print("3D Touch Not Available") 
     } 
    } 

} 

extension ViewController: UIViewControllerPreviewingDelegate 
{ 
    func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? 
    { 
     let convertedLocation = view.convert(location, to: imagePhoto) 
     if imagePhoto.bounds.contains(convertedLocation) 
     { 
      // Init the peek view controller, set relevant properties, and return it 
      let otherViewController = self.storyboard?.instantiateViewController(withIdentifier: "\(OtherViewController.self)") as? OtherViewController 
      otherViewController?.image = imageView.image 
      previewingContext.sourceRect = imageView.frame 
      return otherViewController 
     } 
     else 
     { 
      return nil 
     } 
    } 

    func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) 
    { 
     // Present or push the view controller 
     present(viewControllerToCommit, animated: true) 
    } 
} 

。その結果得られ

enter image description here

+0

はありがとう、あなたがこの@beyowulfに返信し、時間あちこちに..アイブ氏はこれを実装しようとしましたが、私の魔術師がimagePhotoと呼ばれていた場合はどのようにこれが見えるでしょう。 –

+0

'imagePhoto'はあなたの' UIImageView'の名前ですか? 'imagePhoto'が' view'のサブビューであれば、if if imagePhoto.frame.contains(location){//次のView Controllerを作成してイメージを渡す} else {return nil; } ' – beyowulf

+0

私はクリックしている画像の写真を持つビュー内のビュー。私はすでにジェスチャーを設定していますので、クリックすると新しいコントローラーに移動してその画像をすべて表示できます。 –

関連する問題