Similar to Android set view click, long press listener
use
let label = UILabel()
label.text = "Test"
self.view.backgroundColor = UIColor.green
self.view.addSubview(label)
label.snp.makeConstraints { (make) in
make.top.equalToSuperview().offset(0)
make.width.equalTo(200)
make.height.equalTo(80)
}
label.backgroundColor = UIColor.red
label.addOnClick { (view) in
ToastUtil.show("click")
}
label.addOnLongPress { (view) in
ToastUtil.show("long press")}Copy the code
Realize the principle of
Use swift extensions, associated properties, closures
import UIKit
import Foundation
extension UIView {
// Click events
typealias OnClickListener = (UIView) - >Void
// Long press event
typealias OnLongPressListener = (UIView) - >Void
private struct AssociatedKeys {
static var clickKey = "UIView.click"
static var longPressKey = "UIView.onpress"
}
var clickListener: OnClickListener? {
get {
return objc_getAssociatedObject(self.&AssociatedKeys.clickKey) as? OnClickListener
}
set (listener) {
objc_setAssociatedObject(self.&AssociatedKeys.clickKey, listener, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)}}var longPressListener: OnLongPressListener? {
get {
return objc_getAssociatedObject(self.&AssociatedKeys.longPressKey) as? OnLongPressListener
}
set (listener) {
objc_setAssociatedObject(self.&AssociatedKeys.longPressKey, listener, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)}}func addOnClick(listener: OnClickListener?). -> Void {
let tapGes = UITapGestureRecognizer(target: self, action: #selector(onClick))
tapGes.numberOfTapsRequired = 1
self.addGestureRecognizer(tapGes)
self.isUserInteractionEnabled = true
clickListener = listener
}
func addOnLongPress(listener: OnLongPressListener?). -> Void {
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(onLongPress))
longPress.minimumPressDuration = 1
self.addGestureRecognizer(longPress)
self.isUserInteractionEnabled = true
longPressListener = listener
}
@objc func onClick(sender: UITapGestureRecognizer) -> Void {
if let listener = clickListener {
listener(self)}}@objc func onLongPress(sender: UILongPressGestureRecognizer) -> Void {
print(sender.state);
if (sender.state = = UIGestureRecognizer.State.began) {
if let listener = longPressListener {
listener(self)}}}}Copy the code