2014-10-31 45 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

滚动干扰