我有 20 多个按钮,我想为所有人定义一个 UILongPressGestureRecognizer,这可能吗?
到目前为止,这不起作用:
let longPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(handleLongGesture))
B_BTN_1.addGestureRecognizer(longPressGestureRecognizer)
B_BTN_2.addGestureRecognizer(longPressGestureRecognizer)
出于某种原因,longPressGestureRecognizer 仅适用于“B_BTN_2”。
但是通过为每个声明一个手势识别器,它可以工作:
let longPressGestureRecognizer1 = UILongPressGestureRecognizer(target: self, action: #selector(handleLongGesture))
let longPressGestureRecognizer2 = UILongPressGestureRecognizer(target: self, action: #selector(handleLongGesture))
B_BTN_1.addGestureRecognizer(longPressGestureRecognizer1)
B_BTN_2.addGestureRecognizer(longPressGestureRecognizer2)
编辑:
如果您要使用@Andre 的解决方案,并且有很多按钮,请改用它来防止索引错误(就像永远一样):
var buttons:[UIButton] = []
buttons.append(B_BTN_1)
buttons.append(B_BTN_2)
buttons.append(B_BTN_3)
.....