2016-11-23 31 views
3

我試圖讓這樣的工作。這是優步應用程序。用戶可以在背景視圖前輕掃另一個視圖。如何顯示UITableViewController作爲另一個ViewController前面的向上滑動手勢?

enter image description here

背景的看法是相當簡單的,它已經做了。將在頂部滑動的視圖將是一個UITableView。我希望用戶能夠在應用程序啓動時首先看到一個小頂部,然後稍微滑動一下,它應該在中間停下來,然後在完全滑動之後應該一直到頂部,取代背景視圖。

我看過的框架是適用於iOS的可拉式視圖。但它太老了,並沒有得到任何好的動畫。我也看過SWRevealViewController,但我無法弄清楚如何從下面向上滑動。

我也試過使用一個按鈕,所以當用戶點擊它時,表格視圖控制器以模態方式出現,覆蓋垂直,但這不是我想要的。它需要識別一個手勢。

任何幫助,非常感謝。謝謝。

編輯:我打算不接受我的答案,並保持它打開,如果任何人在未來有更好的方式來實現它。即使我的答案有效,它也可以改進。此外,它並不像Uber應用程序中發生的那樣氾濫,這是我的原始問題。

回答

1

您可以在自定義滾動視圖中嵌入該表格視圖,該視圖只在觸摸該表格視圖部分(覆蓋hittest)時處理觸摸,然後將其拖動(禁用tableview滾動),直到上部部分,然後禁用滾動視圖和啓用的tableview滾動再次

或者,你可以添加滑動手勢到你的tableview,並改變它的沿框架和禁用刷卡時,它與那些達到頂峯

實驗,最終你會達到你想要

效果
1

正如Tj3n指出的,你可以使用UISwipeGesture顯示UITableView。因此,使用約束(而不是框架)繼承人如何能夠做到這一點:

轉到您的故事板內您想要顯示UITableViewUIViewController。拖放UITableView並向UITableView添加前導,尾隨和高度。現在在UIViewControllerUITableView之間添加一個垂直約束,以便UITableView出現在以下UIViewController(使用此垂直值玩耍,直到您可以顯示UITableView的頂部以滿足您的需要)。爲垂直間距約束和高度約束創建出口(以防需要設置可在運行時計算出的特定高度)。在向上掃掠只是興致勃勃地設置垂直約束等於高度有點像的負值:

topSpaceToViewControllerConstraint.constant = -mainTableViewHeightConstraint.constant 

UIView.animate(withDuration: 0.3) { 

    view.layoutIfNeeded() 
}; 

或者

如果你希望能夠帶給UITableView了視在平底鍋上的金額(即。e取決於用戶在屏幕上移動了多少或者速度有多快),您應該改用UIPanGestureRecognizer,並嘗試爲UITableView設置幀而不是autoLayout(因爲我不是很喜歡重複調用view.layoutIfNeeded的粉絲。這是一個昂貴的操作,將不勝感激,如果有人會確認或糾正這一點)。

func handlePan(sender: UIPanGestureRecognizer) { 

     if sender.state == .Changed { 
       //update y origin value here based on the pan amount 
     } 
    } 

或者使用的UITableViewController

否則要執行什麼,也可以使用UITableViewController,如果你希望,但它涉及到很多僞造和努力通過創建一個自定義UINavigationControllerDelegate主要是創建一個自定義動畫將使用UIPercentDrivenInteractiveTransition根據搖攝量使用UIPanGestureRecognizer拉動新的UITableViewController。否則,您可以簡單地添加一個UISwipeGestureRecognizer來呈現UITableViewController,但您仍然必須再次創建一個自定義動畫來「僞裝」您想要的效果。

+0

謝謝,我會試試這個報告。 – Munib

1

感謝Rikh和Tj3n給我提示。我設法做了一些非常基本的事情,它沒有Uber這樣好的動畫,但它完成了工作。

使用以下代碼,您可以輕掃任何UIViewController。我在我的圖像上使用了UIPanGestureRecognizer,它始終保持在拖動視圖的頂部。基本上,您使用該圖像並識別它被拖動的位置,並根據用戶的輸入設置視圖的框架。

首先轉到故事板並添加將被拖動的UIViewController的標識符。

enter image description here

然後在MainViewController,使用下面的代碼:

class MainViewController: UIViewController { 

// This image will be dragged up or down. 
@IBOutlet var imageView: UIImageView! 

// Gesture recognizer, will be added to image below. 
var swipedOnImage = UIPanGestureRecognizer() 

// This is the view controller that will be dragged with the image. In my case it's a UITableViewController. 
var vc = UIViewController() 

override func viewDidLoad() { 
    super.viewDidLoad() 

    // I'm using a storyboard. 
    let sb = UIStoryboard(name: "Main", bundle: nil) 

    // I have identified the view inside my storyboard. 
    vc = sb.instantiateViewController(withIdentifier: "TableVC") 

    // These values can be played around with, depending on how much you want the view to show up when it starts. 
    vc.view.frame = CGRect(x: 0, y: self.view.frame.height, width: self.view.frame.width, height: -300) 


    self.addChildViewController(vc) 
    self.view.addSubview(vc.view) 
    vc.didMove(toParentViewController: self) 

    swipedOnImage = UIPanGestureRecognizer(target: self, action: #selector(self.swipedOnViewAction)) 
    imageView.addGestureRecognizer(swipedOnImage) 

    imageView.isUserInteractionEnabled = true 

} 

// This function handles resizing of the tableview. 
func swipedOnViewAction() { 

    let yLocationTouched = swipedOnImage.location(in: self.view).y 

    imageView.frame.origin.y = yLocationTouched 

    // These values can be played around with if required. 
    vc.view.frame = CGRect(x: 0, y: yLocationTouched, width: UIScreen.main.bounds.width, height: (UIScreen.main.bounds.height) - (yLocationTouched)) 
    vc.view.frame.origin.y = yLocationTouched + 50 

} 

最終產品

enter image description here

現在,這可能是我的答案可能不是最這是一種有效的方式,但我是iOS新手,所以這是目前我能想到的最好的方法。

+0

什麼是swipedDiscoveries? – Edu

+0

@Edu對不起,它應該swipedOnImage UIPanGestureRecognizer,我在我的項目中顯示發現,並忘記更改名稱。謝謝 – Munib

+0

這會解釋很多,謝謝! – Edu