2017-08-16 10 views
0

私はUILongPressGestureRecognizerを持っていて、しばらくしてから反応しなくなったようです。それは時間が経過すると相関しているようで、おそらくアクティブ状態を失ってバックグラウンドに入るアプリと相関しているようです。時間の経過後に反応しないジェスチャー認識器

初めてアプリを開いて長時間押すか、スワイプしたり、どちらの作業をしても問題が発生することがよくあります。別のView Controllerに行ってから戻ると、そのリロードによってジェスチャ認識機能が再び動作するようになります。

let longpress = UILongPressGestureRecognizer(target: self, action: #selector(ViewController.longPressGestureRecognized(_:))) 
    mainView.addGestureRecognizer(longpress) 

このように応答しなくなってきてジェスチャー認識装置を扱う周りの任意のアイデア?

+1

viewWillDisappearまたはviewDidDisappearでuserInteractionを無効にする、またはジェスチャーを削除するなどの操作を行いましたか? –

+0

どのメソッドで 'mainView.addGestureRecognizer(longpress)'を実行しますか? – Hooda

+0

コードを追加することはできますか?少なくとも、設定する方法は可能です。 –

答えて

0

次の方法を試してください。

  1. それは
  2. viewWillDisappear

にジェスチャーや通知を削除UIApplicationWillEnterForeground

  • ためviewWillAppearに通知を追加存在しない場合longpressグローバル変数
  • viewWillAppearでジェスチャーを追加してくださいこのようなもの

    var longpress: UILongPressGestureRecognizer! 
    
    override viewDidLoad() { 
        super.viewDidLoad 
        longpress = UILongPressGestureRecognizer(target: self, action: #selector(ViewController.longPressGestureRecognized(_:))) 
    } 
    
    override viewWillAppear(_ animated: Bool) { 
        super.viewWillAppear(animated) 
        checkGestureAvailability() 
        NotificationCenter.default.addObserver(self, selector: #selector(checkGestureAvailability), name: Notification.Name.UIApplicationWillEnterForeground, object: nil) 
    } 
    
    override viewWillDisappear(_ animated: Bool) { 
        if mainView.gestureRecognizers.contains(longpress) { 
         mainView.removeGestureRecognizer(longpress) 
        } 
        NotificationCenter.default.removeObserver(self, name: Notification.Name.UIApplicationWillEnterForeground, object: nil) 
        super.viewWillDisappear(animated) 
    } 
    
    func checkGestureAvailability() { 
        if !mainView.gestureRecognizers.contains(longpress) { 
         mainView.addGestureRecognizer(longpress) 
        } 
    } 
    
  • 関連する問題