【问题标题】:Search bar when swiping down in swift快速向下滑动时的搜索栏
【发布时间】:2014-12-27 14:39:14
【问题描述】:

我正在为 iOS

开发社交媒体应用

我的ViewControllers 目前嵌入在NavigationController 中。 在我的新闻提要屏幕上,我需要在用户向下滑动时显示一个搜索栏(并在他向上滑动时将其隐藏),如果用户键入内容,搜索结果将显示在新闻提要屏幕的顶部。

我曾尝试对此进行修改,但我对 iOS 还很陌生,到目前为止还没有设法让它工作。

任何帮助都将不胜感激,并请记住,我只为 iOS 编程了几周,因此深入了解会有所帮助。 谢谢!

【问题讨论】:

    标签: swift uinavigationcontroller storyboard uisearchbar


    【解决方案1】:

    首先,您需要实现UISwipeGestureRecognizer

    viewDidAppear 中包含setup() 函数

    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
        })
    }
    

    您可以添加Bool isShowing 以避免不必要的动画。然后,实现搜索栏委托textDidChange 以在用户键入时更改搜索结果。

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

    您现在需要做的就是在UISearchController 中显示您的结果。

    注意 使用向上/向下滑动可能会干扰 UIScreachController 的滚动

    【讨论】:

      猜你喜欢
      • 2022-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-20
      • 1970-01-01
      • 2018-05-06
      相关资源
      最近更新 更多