首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Swift自定义键盘-显示额外的字母弹出在键盘上长按?

Swift自定义键盘-显示额外的字母弹出在键盘上长按?
EN

Stack Overflow用户
提问于 2017-05-22 17:20:24
回答 3查看 2.3K关注 0票数 7

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

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

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

任何帮助都将不胜感激。

EN

回答 3

Stack Overflow用户

发布于 2017-06-01 14:35:55

1.在视图上添加按钮

(这只是展示给你看)

代码语言:javascript
复制
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.在您的按钮上添加长PressGesture

代码语言:javascript
复制
     let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(longPress(sender:)))
longGesture.minimumPressDuration = 1.2
        btn.addGestureRecognizer(longGesture)

3.处理长按手势

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

⚠️注意:您有多个按钮,因此您必须从CGPoint检查它是在哪个按钮上点击的

代码语言:javascript
复制
  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.处理额外的按钮按下

(你的东西是从SuperView中添加删除popUpView的吗)

代码语言:javascript
复制
      func buttonAction( sender: Any) {
            
            // Do your Stuff Here
            
            
            //Than remove popView
            popUpView .removeFromSuperview()
        }

Result

✅注意:您可以使用UIBezierPath

绘制PopUpView的自定义形状

票数 2
EN

Stack Overflow用户

发布于 2017-05-23 13:33:15

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

票数 0
EN

Stack Overflow用户

发布于 2017-05-31 18:58:48

按照下面这一步来完成任务非常简单

  • 打开您的主故事板
  • 在您希望显示多个字母的地方选择您的TextField。
  • 从屏幕右侧打开属性检查器
  • 将其向上滚动,并在Min font size
  • Set D12的正下方查找文字H114设置所有其他默认设置,主要是D15现在构建并运行,并使用字母检查D16、D17等。H218、F219等
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44109200

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档