【问题标题】:iOS Interactive Keyboard RevealiOS 交互式键盘显示
【发布时间】:2017-04-07 08:19:11
【问题描述】:

在 iOS 版 Facebook Messenger 上,它有它,所以如果键盘被隐藏,如果你向上滑动,键盘就会显示。它与交互式键盘关闭模式完全相反:当您以向上滑动的速度向上滑动时,键盘会显示出来。

是否有人对如何执行此操作有任何指示?

编辑:感谢您的回答!我主要研究是否有内置的方法来做到这一点,因为我看到它是在 Facebook Messenger 中完成的。但是,我刚刚阅读了一篇博客文章,他们说他们必须截取系统键盘才能获得效果——所以我假设没有内置的方法可以做到这一点!正如 cmets 中提到的,我使用的是自定义键盘,所以这应该会容易得多,因为我可以控制框架!

【问题讨论】:

  • 嗯,例如,您可以尝试将 UISwipeGesture 识别器设置为向上/向下滑动,并使文本字段 firstResponder 显示键盘或在滑动时从底部为您的自定义输入视图设置动画。您是否已经尝试实现它?
  • 我正在尝试为从底部出现的键盘设置动画。没有尝试实现它——我不确定如何控制键盘的位置。
  • 是自定义键盘还是iOS原生kb?
  • 它其实是一个自定义键盘,可能会有所帮助!

标签: ios keyboard


【解决方案1】:

基本上你需要 UIPanGestureRecognizer。

  1. 将 UIScreenEdgePanGestureRecognizer 设置为底部边缘,将 UIPanGestureRecognizer 设置为在 Storyboard 中隐藏键盘并将@IBAction 插座拖到代码中。

  2. 在 Storyboard 的控制器底部使用键盘设置键盘视图容器,以便用户看不到它。将@IBOutlet 拖到您的代码中,以便您可以修改它的框架。

  3. 在拖动视图移动动画时的手势动作中。

  4. 当停止拖动时,您需要检查视图的位置,如果它还没有到达目的地,则将其设置为动画。

  5. 您还需要为拖动区域添加检查,以便用户无法进一步拖动它。

这很简单,您只需要检查所有案例并正确测试即可。

这是您可以从中构建的基本设置

class ViewController: UIViewController {

    @IBOutlet weak var keyboardContainerView: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBAction func onEdgePanGestureDrag(_ sender: UIScreenEdgePanGestureRecognizer) {
        let point = sender.location(in: view)

        view.layoutIfNeeded()
        UIView.animate(withDuration: 0.33) {
            // Animate your custom keyboard view's position
            self.keyboardContainerView.frame = CGRect(x: self.keyboardContainerView.bounds.origin.x,
                                                      y: point.y,
                                                      width: self.keyboardContainerView.bounds.width,
                                                      height: self.keyboardContainerView.bounds.height)
        }
        view.layoutIfNeeded()
    }

    @IBAction func onPanGestureDrag(_ sender: UIPanGestureRecognizer) {
        let point = sender.location(in: view)

        view.layoutIfNeeded()
        UIView.animate(withDuration: 0.33) {
            // Animate your custom keyboard view's position
            self.keyboardContainerView.frame = CGRect(x: self.keyboardContainerView.bounds.origin.x,
                                                      y: point.y,
                                                      width: self.keyboardContainerView.bounds.width,
                                                      height: self.keyboardContainerView.bounds.height)
        }
        view.layoutIfNeeded()
    }
}

【讨论】:

    【解决方案2】:

    这是一个对我有用的实现。它并不完美,但运行良好且易于实现。您将需要一个集合视图或一个表格视图。

    为简单起见,我将只显示此功能所需的代码。因此请处理视图初始化所需的所有其他内容。

    class ViewController: UIViewController {
        var collectionView: UICollectionView!
        var textView: UITextView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            collectionView.panGestureRecognizer.addTarget(self, action: #selector(handlePan(_:)))
            collectionView.keyboardDismissMode = .interactive
            // Only necessary for empty collectionView
            collectionView.alwaysBounceVertical = true
        }
    
        func handlePan(_ sender: UIPanGestureRecognizer) {
            if sender.state == .changed {
                let translation = sender.translation(in: view)
    
                if translation.y < 0 && collectionView.isAtBottom && !self.textView.isFirstResponder() {
                    self.textView.becomeFirstResponder()
                }
            }
        }
    }
    
    extension UIScrollView {
    
        var isAtBottom: Bool {
            return contentOffset.y >= verticalOffsetForBottom
        }
    
        var verticalOffsetForBottom: CGFloat {
            let scrollViewHeight = bounds.height
            let scrollContentSizeHeight = contentSize.height
            let bottomInset = contentInset.bottom
            let scrollViewBottomOffset = scrollContentSizeHeight + bottomInset - scrollViewHeight
            return scrollViewBottomOffset
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2020-11-06
      • 1970-01-01
      • 2019-04-25
      • 2020-03-17
      • 1970-01-01
      • 2023-01-18
      • 2015-07-01
      • 2018-09-09
      • 1970-01-01
      相关资源
      最近更新 更多