我的应用程序中有一个使用 swift 开发的自定义键盘扩展。他们的键盘工作正常。我想添加在长按键盘按钮(如默认 iOS 键盘)时显示带有额外字符的弹出窗口的功能。像这样的东西:
我搜索了很多,但大多数都没有回答,回答的都在 Obj-C 中。我对 Obj-C 了解不多,而且对 swift 编程也很陌生。
任何帮助将非常感激。
我的应用程序中有一个使用 swift 开发的自定义键盘扩展。他们的键盘工作正常。我想添加在长按键盘按钮(如默认 iOS 键盘)时显示带有额外字符的弹出窗口的功能。像这样的东西:
我搜索了很多,但大多数都没有回答,回答的都在 Obj-C 中。我对 Obj-C 了解不多,而且对 swift 编程也很陌生。
任何帮助将非常感激。
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.处理额外的按钮按下
(你的东西在这里添加从 SuperView 中删除 popUpView)
func buttonAction( sender: Any) {
// Do your Stuff Here
//Than remove popView
popUpView .removeFromSuperview()
}
结果✅</p>
✅ 注意:您可以使用 PopUpView 绘制自定义形状
UIBezierPath
您应该使用 LongPress 识别器。请检查此以获取更多详细信息。快速长按自定义键盘的删除键
这很简单,请按照此步骤完成该任务
capitalization
在下面查找Min font size
capitalization
为单词keyboard type
现在构建并运行它并使用 letters
等进行检查e
。