2017-10-14 46 views
0

我設法以編程方式製作幻燈片菜單控制器。我正在嘗試在tableViewController中添加一個轉換,當你點擊一個標籤關閉菜單時,這個動畫會讓tableView全屏顯示0.2秒,然後關閉菜單。這會給點時間改變點擊按鈕的視圖,並且看起來不錯。在didSelectRowAtIndexPath中做了。有用!幻燈片視圖控制器

tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0) 

因爲我只打開屏幕的菜單80%我移位單元的內容查看在屏幕的80%出現。當動畫在didSelectRowAtIndexPath方法開始我轉回到0,所以我不會看到一個空間上的tableView的左側,像這樣:

tableView.contentInset = UIEdgeInsetsMake(0, menuWidthGap, 0, -menuWidthGap) 

現在,當我點擊一次按鈕仍然這樣。當我再次單擊該按鈕以返回到contentView的初始狀態時,我可以做些什麼。

這是我的tableView:

import UIKit 

class MenuLeftVC: UITableViewController { 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Initialize the window 
     let window = UIWindow.init(frame: UIScreen.main.bounds) 
     window.makeKeyAndVisible() 

     view.backgroundColor = UIColor.yellow 
     navigationController?.isNavigationBarHidden = true 

     tableView.dataSource = self 
     tableView.delegate = self 
     tableView.showsVerticalScrollIndicator = false 

     tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell") 



    } 


    override func viewDidAppear(_ animated: Bool) { 
     super.viewDidAppear(true) 
     print("did apear") 
     tableView.contentInset = UIEdgeInsetsMake(0, menuWidthGap, 0, -menuWidthGap) 
    } 

    func selectCell() { 
     print("selected cell - >") 

     NotificationCenter.default.post(name: NSNotification.Name("SelectedCell"), object: nil) 
    } 

    override func numberOfSections(in tableView: UITableView) -> Int { 
     return 1 
    } 

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return 20 
    } 

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")! 
     cell.contentView.reloadInputViews() 

     cell.isSelected = false 
     tableView.contentInset = UIEdgeInsetsMake(0, menuWidthGap, 0, -menuWidthGap) 


     cell.contentView.backgroundColor = UIColor.yellow 
     cell.textLabel?.text = "Daim Boy" 


     return cell 
    } 


    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
     let selectedCell:UITableViewCell = tableView.cellForRow(at: indexPath)! 
     selectedCell.contentView.backgroundColor = UIColor.red 

     selectedCell.isSelected = false 

     if selectedCell.isSelected == true { 
     print("selection") 
     if selectedCell.textLabel?.text == "Daim Boy" { 
      print("cell is selected") 
      UIView.animate(withDuration: 0.3, animations: { 
       tableView.contentInset = UIEdgeInsetsMake(0, menuWidthGap, 0, -menuWidthGap) 
       self.selectCell() 
       //selectedCell.isSelected = false 

      }) 

     } 

     } 


    } 






} 
+0

我不知道你怎麼implemente d幻燈片,但它聽起來像你可能想使用'UICollectionView'而不是'UITableView',因爲它帶來了免費的滑動和轉換。 – shallowThought

+0

我認爲你應該看看視圖控制器的轉換。 – dasdom

+0

無論如何感謝,但它與ViewController動畫無關。我有2個內容視圖。其中一個擁有一個菜單,另一個擁有一個視圖控制器。當我按菜單項didSelectCell觸發,我有那個動畫,將移動contentView。我將放置完整的代碼以便更好地理解。 –

回答

0

我設法解決這個問題:(它是如此簡單)我打電話viewWillAppear中,我在一開始與DispatchQue放置代碼以使佈局就像在該函數的末尾:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    let selectedCell:UITableViewCell = tableView.cellForRow(at: indexPath)! 
    selectedCell.contentView.backgroundColor = UIColor.red 

    selectedCell.isSelected = false 

    if selectedCell.isSelected == true { 
    print("selection") 
    if selectedCell.textLabel?.text == "Daim Boy" { 
     print("cell is selected") 
     UIView.animate(withDuration: 0.3, animations: { 
      tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0) 
      self.selectCell() 
      DispatchQueue.main.asyncAfter(deadline: .now() + 1.0, execute: { 
       self.viewWillAppear(true) 
      }) 

      //selectedCell.isSelected = false 

     }) 

    } 

    } 


} 

而且viewWillAppear中包含:

override func viewWillAppear(_ animated: Bool) { 
    super.viewWillAppear(true) 
    print("will appear") 
    tableView.contentInset = UIEdgeInsetsMake(0, menuWidthGap, 0, -menuWidthGap) 

}