Swift自定义键盘-如何在长按键盘时显示额外的字母弹出框?

8
我在我的应用程序中开发了一个使用swift编写的自定义键盘扩展。这个键盘可以正常工作。我想添加一个功能,在按住键盘按钮时显示带有额外字符的弹出窗口,就像默认iOS键盘一样。类似于这样:enter image description here。我搜索了很多,但大多数都没有答案,而且回答的问题是Obj-C代码。我不太了解Obj-C,对swift编程也比较新手。我已经查看了这个这个这个。但这些并没有什么帮助。非常感谢任何帮助。

我正在使用自定义的键盘扩展。 - bhakti123
回答已更新,修复了错误,请检查并告诉我这是否解决了你的问题 @bhakti123 - Dhiru
这个问题已经在下面得到了回答,但还未被接受。您可以接受帮助过您的答案,接受答案有助于其他人。 - Dhiru
我知道,但是没有一个答案能够帮助到我。 - bhakti123
你解决了吗?如果是的话,能否在这里分享一下解决方案? - crost
显示剩余2条评论
3个回答

2

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并在其上添加一些按钮。

⚠️ 注意:您有多个按钮,因此必须检查从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. 处理额外的按钮按下事件

(在这里完成您的操作,添加或移除弹出视图)

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

结果

在此输入图片描述

✅ 注意:您可以使用 UIBezierPath 绘制自定义的弹出视图形状。


0

我不是在寻找如何使用LongPressGestureRecogniser,而是在寻找如何显示额外字符的弹出窗口。 - bhakti123

-1

这很简单,按照以下步骤完成该任务:

  • 打开你的主故事板
  • 选择你想要显示多个字母的文本框。
  • 从屏幕右侧打开属性检查器
  • 向上滚动并查找capitalization,它位于Min font size下面
  • capitalization设置为Words
  • 设置所有其他默认值,特别是keyboard type。现在构建和运行它,并使用字母se等进行检查。

我没有特定的TextField需要显示多个字母。正如我在问题中提到的,我正在制作自定义键盘扩展程序。这意味着我正在制作一个可以在任何应用程序中使用的键盘。 - bhakti123
所以你想要自己的键盘,并且想要像图中显示的那样显示多个字符。如果是的话,我会尽快回复你的答案。 - Pushpendra
是的。我已经使用Chrome键盘扩展程序构建了自己的键盘。现在想要实现上面显示的功能。 - bhakti123

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接