2016-11-25 14 views
1

Swift 3アプリケーションには、UITableViewを含むhomeViewControllerがあります。テーブルのセルがクリックされると、detailViewControllerと表示されます。子供のようにaddChildViewController(vc)を使って表示します。私はhomeViewControllerに戻るためにバックジェスチャーを実装したいと思います。私は、デバイスのデフォルトMailアプリケーションのバックモードと同じ結果を得たいと考えています(たとえば、受信トレイに入っていたときにメールのホームに戻るバックモード)。言い換えれば、ジェスチャーを使用すると、ユーザーはビューを左から右にドラッグしてからホームビューが表示されるまで左から右にドラッグしたり、ビューを右から左にドラッグして詳細ビューを画面全体に保持することができます。私はUIPanGestureRecognizerを使ってそれをしようとしましたが、私のビューは左右に移動し、時には右側からホームビューを表示します。要するに、私が望む結果は、メールアプリケーションのバックジェスチャー機能と同じです。スウィフト3:バックジェスチャー

これは私のコードです:

var rightSwipe: UIPanGestureRecognizer = UIPanGestureRecognizer() 
     self.rightSwipe = UIPanGestureRecognizer(target: self, action: #selector(self.handleSwipes(_:))) 
     self.view.addGestureRecognizer(rightSwipe) 
func handleSwipes(_ sender: UIPanGestureRecognizer){ 

     if self.rightSwipe.state == .began || self.rightSwipe.state == .changed { 

      let translation = rightSwipe.translation(in: self.view) 
      // note: 'view' is optional and need to be unwrapped 
      //print("translation is : \(translation)") 
      if(translation.x > 0 && translation.y == 0.0){ 
      self.rightSwipe.view!.center = CGPoint(x: self.rightSwipe.view!.center.x + translation.x, y: self.rightSwipe.view!.center.y + translation.y) 
      rightSwipe.setTranslation(CGPoint.zero, in: self.view) 
      } 

     } 
    } 
+0

この動作が必要な場合は、ナビゲーションスタックでViewControllerを押します。 –

+0

もっと説明していただけますか? –

+0

addChildではなく 'self.navigationController.pushViewController(vc、animated:true)'をテストしましたが、セルをクリックするとdetailViewControllerは表示されません。それは私のhomeViewControllerはUIViewControllerなので? –

答えて

0

ストーリーボードでNavigationControllerを使用するには、UINavigationControllerであなたのHomeViewControllerを埋め込ま。 navigationControllerを埋め込むにはHomeViewControllerを選択し、メニューでEditor > Embed In > Navigation Controllerを選択します。

didSelectRowAt indexPathにはdetailViewControllerを押してください。

self.navigationController?.pushViewController(detailVC, animated: true) 

まだジェスチャーはHomeViewControllerviewDidLoadセットで、この1を動作しない場合。

self.navigationController?.interactivePopGestureRecognizer.isEnabled = true 
+0

コメントは議論の延長ではありません。この会話は[チャットに移動]されています(http://chat.stackoverflow.com/rooms/129230/discussion-on-answer-by-nirav-d-swift-3-back-gesture)。 –