LoginSignup
0
1

UITapGestureRecognizerで値・パラメータを渡す方法

Last updated at Posted at 2022-11-06

UITapGestureRecognizerでカスタムクラスを作って任意の値を渡す方法。

画像タップ時やUILabelタップ時にsenderに値を渡す際などに使用。

コード

let sampleImageView = UIImageView()
let customTapGesture = CustomTapGestureRecognizer(target: self, action: #selector(tapped(_:)))
customTapGesture.passValue = "aaa"
sampleImageView.addGestureRecognizer(customTapGesture)

//タップ時の処理
@objc func tapped(_ sender:CustomTapGestureRecognizer) {
    print("tapped")
    print(sender.passValue)
}

//カスタムクラスの作成
class CustomTapGestureRecognizer:UITapGestureRecognizer {
    var passValue: String?
}

参考

Swiftのお役立ち情報

0
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
0
1