【问题标题】:Swift Custom keyboard - show extra letters pop-up on keyboard long press?Swift 自定义键盘 - 在键盘长按时显示额外的字母弹出?
【发布时间】:2017-10-21 21:28:48
【问题描述】:

我的应用中有一个使用 swift 开发的自定义键盘扩展。他们的键盘工作正常。我想添加在长按键盘按钮(如默认 iOS 键盘)时显示带有额外字符的弹出窗口的功能。像这样的:

我搜索了很多,但大多数都没有回答,回答的都在 Obj-C 中。我对 Obj-C 了解不多,而且对 swift 编程也很陌生。

我已经看过thisthisthis。但这些都没有多大帮助。

任何帮助将不胜感激。

【问题讨论】:

  • 您是使用自定义键盘视图还是使用默认键盘?
  • 我正在使用自定义键盘扩展
  • answer 已更新错误修复,请检查并告诉我这是否解决了您的 porob @bhakti123
  • 如果想制作和ios pop View一样的外观,我可以制作,需要私信我,我会把工程文件发给你chat.stackoverflow.com/rooms/144892/ios-dev-expert
  • 这个问题已经在下面回答了,还没有被接受,你可以接受对你有帮助的答案,接受对其他人有帮助的答案。 @bhakti123

标签: ios swift keyboard custom-keyboard uilongpressgesturerecogni


【解决方案1】:

1.在您的视图上添加按钮
(这只是为了向您展示)

let btn: UIButton=UIButton(frame: CGRect(x: 5, y: 70, width: 30, height: 30))
     btn.setTitle("A", for: .normal)
    btn.setTitleColor(UIColor.black, for: .normal);
     self.view.addSubview(btn)

2。在您的按钮上添加长按手势

     let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(longPress(sender:)))
longGesture.minimumPressDuration = 1.2
        btn.addGestureRecognizer(longGesture)

3。处理长按手势,

您可以添加 PopUpView 并在其上添加一些按钮,

⚠️ 注意:你有多个按钮,所以你必须检查 From CGPoint 在哪个按钮上被点击了

  func longPress( sender: Any) {
            
            let longPressGesture = sender as! UILongPressGestureRecognizer

//Only run this code When State Begain
if longPressGesture.state != UIGestureRecognizerState.Began {
            return
     }
// if PopUpView is Already in added than remove and than  add
 if let checkView = self.view.viewWithTag(1001) as? UIView {
         // remove popView
        popUpView .removeFromSuperview()
   }
            
            let tapLocation = longPressGesture.location(in: self.view)
            
            
            popUpView=UIView(frame: CGRect(x: tapLocation.x-10, y: tapLocation.y-65, width: 150, height: 60))
            popUpView.backgroundColor=UIColor.orange
            popUpView.layer.cornerRadius=5
            popUpView.layer.borderWidth=2
            popUpView.tag=1001
            popUpView.layer.borderColor=UIColor.black.cgColor
            
            let btn0: UIButton=UIButton(frame: CGRect(x: 5, y: 5, width: 30, height: 30))
            btn0.setTitle("A1", for: .normal)
            btn0.setTitleColor(UIColor.black, for: .normal);
            btn0.layer.borderWidth=0.5
            btn0.layer.borderColor=UIColor.lightGray.cgColor
            
            popUpView.addSubview(btn0)
            
            let btn1: UIButton=UIButton(frame: CGRect(x: 35, y: 5, width: 30, height: 30))
            btn1.setTitle("A2", for: .normal)
            btn1.setTitleColor(UIColor.black, for: .normal);
            btn1.layer.borderWidth=0.5
            btn1.layer.borderColor=UIColor.lightGray.cgColor
            
            popUpView.addSubview(btn1)
            
            let btn2: UIButton=UIButton(frame: CGRect(x: 70, y: 5, width: 30, height: 30))
            btn2.setTitle("A2", for: .normal)
            btn2.setTitleColor(UIColor.black, for: .normal);
            btn2.layer.borderWidth=0.5
            btn2.layer.borderColor=UIColor.lightGray.cgColor
            
            popUpView.addSubview(btn2)
            
            btn0.addTarget(self, action: #selector(self.buttonAction(sender:)),
                             for: UIControlEvents.touchUpInside)
            btn1.addTarget(self, action: #selector(self.buttonAction(sender:)),
                           for: UIControlEvents.touchUpInside)
            btn2.addTarget(self, action: #selector(self.buttonAction(sender:)),
                           for: UIControlEvents.touchUpInside)
     
             self.view.addSubview(popUpView)
            
           
        }

4.处理额外的按钮按下

(Do your Stuff here add remove popUpView from SuperView)

      func buttonAction( sender: Any) {
            
            // Do your Stuff Here
            
            
            //Than remove popView
            popUpView .removeFromSuperview()
        }

结果

✅ 注意:您可以使用UIBezierPath 绘制自定义形状的 PopUpView

【讨论】:

    【解决方案2】:

    您应该使用 LongPress 识别器。请检查此以获取更多详细信息。 Long press delete key of a custom keyboard in swift

    【讨论】:

    • 我不是在寻找如何使用LongPressGestureRecogniser。我正在寻找如何显示额外字符的弹出窗口。
    【解决方案3】:

    这很简单,按照这个步骤来完成这个任务

    • 打开您的主故事板
    • 选择要显示多个字母的 TextField。
    • 从屏幕右侧打开属性检查器
    • 向上滚动并在Min font size 下方查找capitalization
    • capitalization设置为单词
    • 设置所有其他默认值,主要是keyboard type 现在构建并运行它并检查字母se 等。

    【讨论】:

    • 我没有要显示多个字母的特定 TextField。正如我在问题中提到的,我正在制作自定义键盘扩展。这意味着我正在制作一个可以在任何应用程序中使用的键盘。
    • 所以你想要一个自己的键盘并想要显示多个字符,如图所示。如果是,我会尽快回复您。
    • 是的。我已经使用 chrome 键盘扩展构建了自己的键盘。现在想实现上面显示的功能
    猜你喜欢
    • 2015-06-06
    • 2019-07-15
    • 2014-11-12
    • 2016-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-20
    • 1970-01-01
    相关资源
    最近更新 更多