Created
April 11, 2017 11:04
-
-
Save MTattin/4f4b3fb69e671362eab8c146f77b912a to your computer and use it in GitHub Desktop.
タップジェスチャをコールバックで(swift3) ref: http://qiita.com/MTattin/items/36d66f1f08235c382cf3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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