swift
1import UIKit 2 3class ViewController: UIViewController { 4 5 var add = UILabel() 6 var longPressRecognizer: UILongPressGestureRecognizer! 7 //+ボタン 8 @IBAction func addlabel(_ sender: Any) { 9 10 alert() 11 //label() 12 } 13 14 //ラベルについて 15 func label(){ 16 add = UILabel(frame: CGRect(x: 130, y:250, width: 100, height:20)) 17 //ラベルの大きさ、座標指定 18 //add.text = "labelです" 19 //文字を変更 20 21 add.backgroundColor = UIColor.lightGray 22 23 add.font = UIFont.systemFont(ofSize: 30) 24 //文字の大きさ 25 26 add.textColor = UIColor.black 27 //文字カラー 28 29 add.sizeToFit() 30 //文字数にあわせてlabelの大きさを変更(サイズが文字にフィットする) 31 32 self.view.addSubview(add) 33 //実際にviewに見える形でlabelが出現する 34 } 35 36 func alert(){ 37 // テキストフィールド付きアラート表示 38 39 let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert) 40 41 // OKボタンの設定 42 let okAction = UIAlertAction(title: "OK", style: .default, handler: { 43 (action:UIAlertAction!) -> Void in 44 45 // OKを押した時入力されていたテキストを表示 46 if let textFields = alert.textFields { 47 48 // アラートに含まれるすべてのテキストフィールドを調べる 49 for textField in textFields { 50 self.label() 51 self.add.text = textField.text! 52 self.add.sizeToFit() 53 print(textField.text!) 54 self.Gesture() 55 } 56 } 57 }) 58 alert.addAction(okAction) 59 60 // キャンセルボタンの設定 61 let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) 62 alert.addAction(cancelAction) 63 64 // テキストフィールドを追加 65 alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in 66 textField.placeholder = "テキスト" 67 }) 68 69 alert.view.setNeedsLayout() // シミュレータの種類によっては、これがないと警告が発生 70 71 // アラートを画面に表示 72 self.present(alert, animated: true, completion: nil) 73 74 } 75 76 77 @objc func longpress(sender: UILongPressGestureRecognizer){ 78 79 // 長押し開始〜 80 if(sender.state == UIGestureRecognizerState.began) 81 { 82 83 } else if (sender.state == UIGestureRecognizerState.ended) 84 { 85 alert() 86 print("ロングタップされたよ。") 87 } 88 */ 89 } 90 91 func Gesture() { 92 93 // UILongPressGestureRecognizerインスタンス作成 94 let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(ViewController.longpress(sender:))) 95 // 時間(デフォルト0.5秒) 96 longPressGesture.minimumPressDuration = 0.5 97 self.add.isUserInteractionEnabled = true 98 self.add.addGestureRecognizer(longPressGesture) 99 100 101 102 } 103 104 func doubleclic(){ 105 // ダブルタップ 106 let doubeltapGesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.doubletap(sender:))) 107 doubeltapGesture.numberOfTapsRequired = 2 108 self.add.isUserInteractionEnabled = true 109 self.add.addGestureRecognizer(doubeltapGesture) 110 } 111 112 @objc func doubletap(sender: UITapGestureRecognizer){ 113 if(sender.state == UIGestureRecognizerState.began) 114 { 115 116 }else if(sender.state == UIGestureRecognizerState.ended) 117 { 118 print("double click") 119 print("tapされたよ") 120 } 121 122 } 123 124 override func viewDidLoad() { 125 super.viewDidLoad() 126 127 } 128 129 override func didReceiveMemoryWarning() { 130 super.didReceiveMemoryWarning() 131 // Dispose of any resources that can be recreated. 132 } 133 134 135} 136
困っていること
UITapGestureRecognizerについての処理が実行しても動かなくて困っています。
長押しについて質問をさせていただいた時には@objcがなくてエラーが起きていたので今回はUITapGestureRecognizerに@objcを入れてもdouble clickでできなくて困っています。
UILongPressGestureRecognizerがあるとUITapGestureRecognizerは反応しないのでしょうか?
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/03/09 04:17
退会済みユーザー
2018/03/13 01:21