我的应用程序中有一个自定义键盘扩展程序,它是使用swift开发的。他们的键盘工作正常。我想在长按键盘按钮(如默认的iOS键盘)时添加显示带有额外字符的弹出窗口的功能。像这样:
我搜索了很多,但大多数都没有回答,答案是在Obj-C。我对Obj-C了解不多,对快速编程也是一个新手。
我已查看过this,this和this。但这些都没有多大帮助。
任何帮助都会非常感激。
答案 0 :(得分: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。在按钮上添加Long PressGesture
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。处理额外按钮按
(你的东西在这里添加从SuperView中删除popUpView)
func buttonAction( sender: Any) {
// Do your Stuff Here
//Than remove popView
popUpView .removeFromSuperview()
}
结果✅
✅注意:您可以使用
绘制PopUpView的自定义形状UIBezierPath
我希望这会对你有所帮助。
答案 1 :(得分:0)
您应该使用LongPress识别器。请查看此更多详细信息。 Long press delete key of a custom keyboard in swift
答案 2 :(得分:0)
按照此步骤实现该任务非常简单
capitalization
Min font size
capitalization
设为字词keyboard type
现在构建并运行它并检查字母s
,e
等。这将帮助你