如何实现iOS swift playgrounds的虚拟键盘功能。

时间:2016-12-29 03:45:46

标签: swift custom-keyboard

iOS上的Swift playground对于新程序员来说是一个很好的IDE,而apple提供了一个用于编程的虚拟键盘,我们可以滑动单个按钮来输入多个字符,包括“,; :/“...... ,我想在我的程序中实现这个功能。如何实现此功能?使用UIGestureRecognizerDelegate?pan?如何实现滑动时按钮中出现的动画?

swift playgrounds virtual keyboard

这是我的代码:

导入UIKit

class myViewController:UIViewController,UIGestureRecognizerDelegate {

@IBOutlet weak var mybutton1: UIButton!
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(buttonPressed(_:)))
    let moveRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(moved(_:)))
    let panRecognizer = UIPanGestureRecognizer(target: self, action: #selector(pan(_:)))
    let longgestureRecognizer = UILongPressGestureRecognizer(target: self, action:  #selector(longLabelPressed(_:)))
    longgestureRecognizer.minimumPressDuration = 1 //.001
    mybutton1.isUserInteractionEnabled = true;
    mybutton1.addGestureRecognizer(gestureRecognizer)
    mybutton1.addGestureRecognizer(moveRecognizer)
    mybutton1.addGestureRecognizer(panRecognizer)
    mybutton1.addGestureRecognizer(longgestureRecognizer)
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
func moved(_ recognizer:UILongPressGestureRecognizer){
     print("moved !!!!")
}
func pan(_ recognizer:UILongPressGestureRecognizer){
    print("pan !!!!")
}
func longLabelPressed(_ recognizer:UILongPressGestureRecognizer){
  //  if let label = recognizer.view as? UIButton {
        if recognizer.state == .began {
            //label.textColor = UIColor.red
             print("longlabelPressed begin !")
        }

        if recognizer.state == .ended {
            //label.textColor = UIColor.black
             print("longlabelPressed end  !")
        }
}
func buttonPressed(_ recognizer:UITapGestureRecognizer) {
   print("labelPressed  !")
}

}

// =============

0 个答案:

没有答案