Skip to content

Instantly share code, notes, and snippets.

@MTattin
Created April 11, 2017 11:04
Show Gist options
  • Save MTattin/4f4b3fb69e671362eab8c146f77b912a to your computer and use it in GitHub Desktop.
Save MTattin/4f4b3fb69e671362eab8c146f77b912a to your computer and use it in GitHub Desktop.
タップジェスチャをコールバックで(swift3) ref: http://qiita.com/MTattin/items/36d66f1f08235c382cf3
import UIKit
///
/// ベースビュー
///
class V: UIView, UIGestureRecognizerDelegate {
// MARK: ------------------------------ UIGestureRecognizer
///
/// コールバック関数
///
private var _singleTapAction: ((_ g: UITapGestureRecognizer) -> Void)?
///
/// シングルタップ設定
///
func singleTap(_ action: ((_ g: UITapGestureRecognizer) -> Void)?) {
let singleTapGesture: UITapGestureRecognizer = UITapGestureRecognizer(
target: self,
action: #selector(V._singleTapSelector(_:))
)
singleTapGesture.delegate = self
singleTapGesture.numberOfTapsRequired = 1
singleTapGesture.numberOfTouchesRequired = 1
self.addGestureRecognizer(singleTapGesture)
self._singleTapAction = action
}
///
/// selector用
///
@objc private func _singleTapSelector(_ g: UITapGestureRecognizer) {
self._singleTapAction?(g)
}
}
let view: V = V.init()
view.singleTap { (g) in
if UIGestureRecognizerState.ended == g.state {
///
/// 何か処理
///
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment