開発環境 Xcode10.2.1 / Swift5 / iOS12
フリック入力ができるボタンを作成したいと考え、こちらを参考にUISwipeGestureRecognizerを用いて、以下に示すようなコードでボタンを作成しました。
やりたいこととして、現状では左にフリックするとラベルが出るようになっているのですが、その後指を離した時にラベルを消す方法を教えていただきたいです。
iPhoneの日本語キーボードのフリック入力のような動作をイメージしています。
ご教授よろしくお願いいたします。
Swift
1import UIKit 2 3class ViewController: UIViewController { 4 5 var key = UIButton() 6 var leftLabel:UILabel? 7 8 override func viewDidLoad() { 9 super.viewDidLoad() 10 11 key = UIButton(frame: CGRect(x: 200, y: 400, width: 80 , height: 50)) 12 key.backgroundColor = UIColor.gray 13 view.addSubview(key) 14 15 let flickLeft = UISwipeGestureRecognizer() 16 flickLeft.direction = UISwipeGestureRecognizer.Direction.left 17 flickLeft.addTarget(self, action: #selector(ViewController.leftEntry)) 18 key.addGestureRecognizer(flickLeft) 19 20 } 21 22 // 左にフリックした時 23 @objc func leftEntry(sender:UISwipeGestureRecognizer){ 24 print("左") 25 leftLabel = UILabel() 26 leftLabel!.frame = CGRect(x: 0, y: 0, width: key.frame.width, height: key.frame.height) 27 leftLabel!.layer.position = CGPoint(x:key.layer.position.x - key.frame.width, y:key.layer.position.y) 28 leftLabel!.backgroundColor = UIColor(red:0.7,green:0.7,blue:0.7,alpha:1.0) 29 leftLabel!.text = "左" 30 leftLabel!.font = UIFont.systemFont(ofSize: 22) 31 leftLabel!.textAlignment = NSTextAlignment.center 32 self.view.addSubview(leftLabel!) 33 } 34 35} 36
回答2件
あなたの回答
tips
プレビュー