2

我希望当有人在按钮内滑动时执行一次操作。

我目前的代码如下:

    let recogniser = UISwipeGestureRecognizer(target: self, action: "didTapButton2:")
    recogniser.direction = .Up
    button.addGestureRecognizer(recogniser)

    func didTapButton2(sender: UIGestureRecognizer!) {

    //Here I want to be able to recognise which button this was sent from (they are all tagged)
    let button = sender. as UIButton //Gives an error

我需要使用手势识别器而不是 UIControlEvents,因为我需要事件只触发一次。使用它会使事件触发多次 - 只需要它执行一次:

    button.addTarget(self, action: "didTapButton2:", forControlEvents: .TouchDragInside)

有没有人有办法解决吗?谢谢

4

1 回答 1

6

AUIGestureRecognizer有一个名为的属性view,它是它所附加的视图。View 被声明为一个可选的,所以你需要解开它:

if let button = sender.view as? UIButton {
    // use button
    if button.tag == 10 {
        // handle button tagged with 10
    }
}
于 2014-10-07T22:41:56.783 回答