2014-10-31 41 views
6

向下滑動,當我開發一個社交媒體應用程序的iOS版搜索欄在迅速

ViewControllers目前嵌入NavigationController。 在我的新聞提要屏幕上,當用戶向下滑動(並在向上滑動時隱藏它)時,我需要顯示一個搜索欄,如果用戶輸入內容,搜索結果將顯示在新聞提要屏幕的頂部。

我曾嘗試過修改這個,但我對iOS很新,至今還沒有設法讓它工作。

任何幫助將不勝感激,並記住,我只是編程iOS幾個星期,所以一些深入會有所幫助。 謝謝!

回答

3

首先,你需要實現UISwipeGestureRecognizer

包括viewDidAppear

func setup() { 
    let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(down)) 
    swipeDown.direction = .down 
    let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(up)) 
    swipeUp.direction = .up 

    self.view.addGestureRecognizer(swipeDown) 
    self.view.addGestureRecognizer(swipeUp) 

    searchBar = UISearchBar(frame: CGRect(x: 0.0, y: 0.0, width: self.view.frame.size.width, height: 40.0)) 
    if let searchBar = searchBar 
    { 
     searchBar.backgroundColor = UIColor.red 
     self.view.addSubview(searchBar) 
    } 
} 

那麼你的兩個功能上下

func down(sender: UIGestureRecognizer) { 
    print("down") 
    //show bar 
    UIView.animate(withDuration: 1.0, animations: {() -> Void in 
     self.searchBar!.frame = CGRect(x: 0.0, y: 64.0, width: self.view.frame.width, height: 40.0) 
    }, completion: { (Bool) -> Void in 
    }) 
} 

func up(sender: UIGestureRecognizer) { 
    print("up") 
    UIView.animate(withDuration: 1.0, animations: {() -> Void in 
     self.searchBar!.frame = CGRect(x: 0.0, y: 0.0, width: self.view.frame.width, height: 40.0) 
    }, completion: { (Bool) -> Void in 
    }) 
} 

setup()功能您可以添加Bool isShowing避免不必要的動畫。然後,執行搜索欄代理textDidChange以按用戶鍵入的方式更改搜索結果。

func searchBar(_ searchBar: UISearchBar,textDidChange searchText: String)` 

現在您只需將結果顯示在UISearchController中。

注意 使用刷卡向上/向下運動可能與的UIScreachController

滾動干擾