如何为所有按钮定义单个 UILongPressGestureRecognizer?在 swift 3
How to define a single UILongPressGestureRecognizer for all buttons? In swift 3
我有 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)
.....
引自 Apple 文档:
Every gesture recognizer is associated with one view. By contrast, a view can have multiple gesture recognizers, because a single view might respond to many different gestures. For a gesture recognizer to recognize touches that occur in a particular view, you must attach the gesture recognizer to that view. When a user touches that view, the gesture recognizer receives a message that a touch occurred before the view object does. As a result, the gesture recognizer can respond to touches on behalf of the view.
因此,您需要为每个视图初始化手势识别器,就像您所做的那样:
But by declaring a gesture recognizer for each, it works:
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)
因为@Andriy Savran 已经说过,手势识别器只能有 一个 视图附加到。您可以使用循环来简化设置过程...
override func viewDidLoad() {
super.viewDidLoad()
let button1 = UIButton(type: .system)
let button2 = UIButton(type: .system)
let button3 = UIButton(type: .system)
// ...
let buttons = [button1, button2, button3]
for button in buttons {
let longPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress(sender:)))
button.addGestureRecognizer(longPressGestureRecognizer)
}
}
func handleLongPress(sender: UILongPressGestureRecognizer) {
guard let button = sender.view else {
fatalError("could not get the button attached to the gesturerecognizer")
}
// do something
}
我有 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)
.....
引自 Apple 文档:
Every gesture recognizer is associated with one view. By contrast, a view can have multiple gesture recognizers, because a single view might respond to many different gestures. For a gesture recognizer to recognize touches that occur in a particular view, you must attach the gesture recognizer to that view. When a user touches that view, the gesture recognizer receives a message that a touch occurred before the view object does. As a result, the gesture recognizer can respond to touches on behalf of the view.
因此,您需要为每个视图初始化手势识别器,就像您所做的那样:
But by declaring a gesture recognizer for each, it works:
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)
因为@Andriy Savran 已经说过,手势识别器只能有 一个 视图附加到。您可以使用循环来简化设置过程...
override func viewDidLoad() {
super.viewDidLoad()
let button1 = UIButton(type: .system)
let button2 = UIButton(type: .system)
let button3 = UIButton(type: .system)
// ...
let buttons = [button1, button2, button3]
for button in buttons {
let longPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress(sender:)))
button.addGestureRecognizer(longPressGestureRecognizer)
}
}
func handleLongPress(sender: UILongPressGestureRecognizer) {
guard let button = sender.view else {
fatalError("could not get the button attached to the gesturerecognizer")
}
// do something
}