iOSアプリで、左ドロワーのメニュー(TableCell)をタップすると、右ドロワーのメニューが動的に変わるという動作を実現したいと思い、
試行錯誤していったん以下のように作成して動作したものの、初心者なのでこれがベストな方法なのかいまいち自身がありません。
他のやり方があれば教えてほしいです。
手順
1.左ドロワーをタップすると、tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)内で自身をclose
2.close後、右ドロワーに遷移する
左ドロワーのViewController
swift
1class LeftMenuViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 2 3・・・略・・・ 4 // メニューがタップされた際のイベント 5 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 6 7 // 左ドロワーを閉じて(close関数は省略) 8 close() 9 10 // 右ドロワーのViewControllerに遷移 11 let storyboard: UIStoryboard = self.storyboard! 12 let rghtDrawer = storyboard.instantiateViewController(withIdentifier: “rightDrawer”) 13 self.present(rghtDrawer, animated: true, completion: nil) 14 } 15
右ドロワーのViewController
swift
1class MenuRightViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { { 2 3・・・略・・・ 4 override func viewWillAppear(_ animated: Bool) { 5 6 super.viewWillAppear(animated) 7 8 // メニューの位置を取得する 9 let menuPos = self.navigationTableR.layer.position 10 11 print(“rightview close1:(self.navigationTableR.layer.position)“) 12 13 // 初期位置を画面の外側にするため、メニューの幅の分だけplusする 14 self.navigationTableR.layer.position.x += self.navigationTableR.frame.width 15 16 // 表示時のアニメーションを作成する 17 UITableView.animate( 18 withDuration: 0.5, 19 delay: 0, 20 options: .curveLinear, 21 animations: { 22 self.navigationTableR.layer.position.x = menuPos.x 23 }, 24 completion: { 25 bool in 26 }) 27 } 28
あなたの回答
tips
プレビュー