LoginSignup
1
1

More than 3 years have passed since last update.

UISwipeGestureRecognizerを使ったときにはまった(unrecognized selector sent to instance)

Last updated at Posted at 2019-11-30

状況

UISwipeGestureRecognizerを使ったとき

unrecognized selector sent to instance

がで続けてハマったのでメモ。ちなみにハマったときに書いてたコードが以下。(一部簡略化してます)

private func setSwipe(girlsView: GirlsView) {
        let rightSwipe = UISwipeGestureRecognizer(target: self, action: "didSwipe:"))
        rightSwipe.direction = .right
        girlsView.addGestureRecognizer(rightSwipe)
}

private func didSwipe(sender: UISwipeGestureRecognizer) {
        if sender.direction.contains(.right) {
            print("Right!")
            getFrontView().removeFromSuperview()
            girlsViewArray.removeLast()
        }
}

対策

調べるとactionの関数名書くところに:をつけなきゃダメとかあったのですがうまくいかず
@objcにして#selectorで囲ったらうまく行きました(addTargetと同じやり方ですね)

private func setSwipe(girlsView: GirlsView) {
        let rightSwipe = UISwipeGestureRecognizer(target: self, action: #selector(didSwipe(sender:)))
        rightSwipe.direction = .right
        girlsView.addGestureRecognizer(rightSwipe)
    }

@objc func didSwipe(sender: UISwipeGestureRecognizer) {
        if sender.direction.contains(.right) {
            print("Right!")
            getFrontView().removeFromSuperview()
            girlsViewArray.removeLast()
        }
}

参考

ios – SpriteKitとUISwipeGestureRecognizer - コードログ
ios - UITapGestureRecognizer unrecognized selector sent to instance - Stack Overflow
UISwipeGestureRecognizerの罠と左右スワイプの実装 - Qiita

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1