2016-04-23 17 views
0

ボタンを長押しすると、長い押しが認識されますが、「テスト」は2回呼び出されます。それをどうやって防ぐのですか?UILongPressGestureが2回呼び出される

@IBOutlet weak var button2: UIButton! 

func longPressMe(){ 
    print("test") 
} 

func longPressGes(){ 
    let longpress = UILongPressGestureRecognizer(target: self, action: "longPressMe") 
    longpress.minimumPressDuration = 1 
    button2.addGestureRecognizer(longpress) 
} 


override func viewDidLoad() { 
    super.viewDidLoad() 
    longPressGes() 
} 

答えて

-1

はここで、試してみる#selector使用する方法である:私の方法とあなたの方法ボットと

func longPressMe(recognizer: UILongPressGestureRecognizer) { 
    // do stuff here 
} 

func longPressGes(){ 
    let longpress = UILongPressGestureRecognizer(target: self, action: #selector(yourViewController.longPressMe(_:))) 
    longpress.minimumPressDuration = 1 
    button2.addGestureRecognizer(longpress) 
} 
+0

ありがとうございます。それは有り難いです。しばらくそれを得ることができませんでした。 –

+0

この回答は、質問に記載されているように問題を解決するものではありません... –

2

ジェスチャ認識機能の状態を確認する必要があります。このような何かにlongPressMe()を変更します。

func longPressMe(recognizer: UILongPressGestureRecognizer) { 
    guard recognizer.state == .Began else { return } 

    // do stuff here 
} 
+0

を私が原因キャッチされないにアプリを終了***得続けます例外 'NSInvalidArgumentException'、理由: ' - [testtest.ViewController pressAction]:インスタンスに送信された認識できないセレクタ –

+0

セレクタに文字列を使用することはできません。代わりに新しい '#selector'構文を使用してください。 –

+0

@DAMONGONZALEZセレクタを '#selector(longPressMe(_ :))'に変更するか、Xcode/Swiftの古いバージョンで 'action:"を使用している場合longPressMe: "' –

関連する問題