UILongPressGestureRecognizer访问被点击的按钮

Ska*_*kal 3 xcode storyboard uiviewcontroller ios swift

我在我的视图中设置了一个UILongPressGestureRecognizer来处理四个不同的按钮,如何在我的代码中访问哪个按钮被点击?

我的UILongPressGestureRecognizer看起来像这样:

@IBAction func editText(sender: UILongPressGestureRecognizer) {
        textFieldInput.hidden = false
        iphoneSaveCharName.hidden = false
      }
Run Code Online (Sandbox Code Playgroud)

我想使用长按,以便我可以编辑按钮文本

编辑1:

import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var iphoneTableView: UITableView!
    @IBOutlet weak var textFieldInput: UITextField!
    @IBOutlet weak var iphoneSaveCharName: UIButton!
    @IBOutlet weak var charOne: UILabel!
    @IBOutlet weak var charTwo: UILabel!
    @IBOutlet weak var charTree: UILabel!
    @IBOutlet weak var charFour: UILabel!
    @IBOutlet weak var test1: UIButton! //button that I am clicking on!



    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    //Function I made so I can save the user input
    @IBAction func iphoneSaveTextInput(sender: UIButton) {
        let textData = textFieldInput.text
        textFieldInput.hidden = true
        iphoneSaveCharName.hidden = true
        charTwo.text = textData

    }

    // This is the LongPress Action
    @IBAction func editText(sender: UILongPressGestureRecognizer) {
        textFieldInput.hidden = false
        iphoneSaveCharName.hidden = false

        func longPressMethod(gesture: UILongPressGestureRecognizer) {

            println(gesture.view)

            if gesture.view is UIButton {

                let test1 = gesture.view as UIButton
                println(test1)
            }
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

编辑2:布局

编辑3:新的ViewController

 import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var iphoneTableView: UITableView!
    @IBOutlet weak var textFieldInput: UITextField!
    @IBOutlet weak var iphoneSaveCharName: UIButton!
    @IBOutlet weak var charOne: UIButton!
    @IBOutlet weak var charTwo: UIButton!
    @IBOutlet weak var charThree: UIButton!
    @IBOutlet weak var charFour: UIButton!




    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func iphoneSaveTextInput(sender: UIButton) -> Void{

        let textData = textFieldInput.text
        textFieldInput.hidden = true
        iphoneSaveCharName.hidden = true
    }

    @IBAction func editText(sender: AnyObject) {
        if sender is UILongPressGestureRecognizer &&
            sender.state == UIGestureRecognizerState.Began {

                textFieldInput.hidden = false
                iphoneSaveCharName.hidden = false

//                func iphoneSaveTextInput(sender: UIButton){
//                    var textData = textFieldInput.text
//                    textFieldInput.hidden = true
//                    iphoneSaveCharName.hidden = true
//                
//                }

                let button = sender.view as UIButton
                println(button)

                if button.tag == 1{
                    charOne.setTitle("textData", forState: .Normal)
                } else if button.tag == 2{
                    charTwo.setTitle("textData2", forState: .Normal)
                } else if button.tag == 3{
                    charThree.setTitle("textData3", forState: .Normal)
                } else if button.tag == 4{
                    charFour.setTitle("textData4", forState: .Normal)
                }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

回答:

这是最终的视图控件:

import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var iphoneTableView: UITableView!
    @IBOutlet weak var textFieldInput: UITextField!
    @IBOutlet weak var iphoneSaveCharName: UIButton!
    @IBOutlet weak var charOne: UIButton!
    @IBOutlet weak var charTwo: UIButton!
    @IBOutlet weak var charThree: UIButton!
    @IBOutlet weak var charFour: UIButton!




    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func iphoneSaveTextInput(sender: UIButton) -> Void{

        let textData = textFieldInput.text
        textFieldInput.hidden = true
        iphoneSaveCharName.hidden = true
    }


    @IBAction func editText(sender: AnyObject) {
        if sender is UILongPressGestureRecognizer &&
            sender.state == UIGestureRecognizerState.Began {

                textFieldInput.hidden = false
                iphoneSaveCharName.hidden = false

//                func iphoneSaveTextInput(sender: UIButton){
//                    var textData = textFieldInput.text
//                    textFieldInput.hidden = true
//                    iphoneSaveCharName.hidden = true
//                
//                }

                let button = sender.view as UIButton
                println(button)

                if button.tag == 1{
                    charOne.setTitle("textData", forState: .Normal)
                } else if button.tag == 2{
                    charTwo.setTitle("textData2", forState: .Normal)
                } else if button.tag == 3{
                    charThree.setTitle("textData3", forState: .Normal)
                } else if button.tag == 4{
                    charFour.setTitle("textData4", forState: .Normal)
                }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

因为每个人都帮助我,所以我最想给你一个最好的答案!我必须为每个按钮创建一个长按,否则代码会混淆.

Lyn*_*ott 5

你的问题向我介绍了一个非常酷的功能,谢谢!:)

事实证明,如果你附加UILongPressGestureRecognizerUIButton一个故事板,并附上按钮手势的IBAction你迅速类中,该IBAction方法可以识别触摸是否是轻敲或长按!很酷,对吧?

首先,确保每个UIButton都有自己独特的UILongPressGestureRecognizer; 那么你可以像这样编辑你的代码,这样它就可以识别出按下了哪个按钮,以及该按下是简单的点击还是UILongPressGestureRecognizer:

// Connect both your button *and* its gestures to the
// `IBAction` method so that the function will be called
// no matter what kind of gesture it recognizes -- tap, 
// long press, or otherwise.
@IBAction func buttonSelected(sender: AnyObject) {

    // But to see if the gesture is a long press, you can
    // simply check the sender's class and execute the code
    // when the gesture begins.
    if sender is UILongPressGestureRecognizer &&
        sender.state == UIGestureRecognizerState.Began {

        // These two lines are originally from your
        // editText method
        textFieldInput.hidden = false
        iphoneSaveCharName.hidden = false

        // Then to identify which button was long pressed you
        // can check the sender's view, to see which button IBOutlet
        // that gesture's view belongs to.

        // Method #1:

        let button = sender.view as UIButton

        if button == thisButton {

        } else if button == thatButton {

        }
        ...

        // Or you can check the gesture view's tag to see which
        // button it belongs to (i.e. whichever button has a matching
        // tag).

        // Method #2:

        let button = sender.view as UIButton

        if button.tag == 1 {

        } else if button.tag == 2 {

        }
        ...
    }

    // Else if it's not a long press gesture, perform
    // whatever action you'd like to accomplish during a
    // normal button tap
    else if !(sender is UILongPressGestureRecognizer) {

        // These lines are originally from your
        // iphoneSaveTextInput
        let textData = textFieldInput.text
        textFieldInput.hidden = true
        iphoneSaveCharName.hidden = true

    }
}
Run Code Online (Sandbox Code Playgroud)

但是如果你想继续保持与s 的UILongPressGestureRecognizer IBAction分离(即链接s 和你的s ),那也应该没问题.保持您的方法不变,并像这样更新您的方法:UIButtonIBActionUIButtoniphoneSaveTextInput:UILongPressGestureRecognizereditText:iphoneSaveTextInput:editText:

// This is the LongPress Action
@IBAction func editText(sender: UILongPressGestureRecognizer) {

    if sender.state == UIGestureRecognizerState.Began {

        textFieldInput.hidden = false
        iphoneSaveCharName.hidden = false

        // Then to identify which button was long pressed you
        // can check the sender's view, to see which button IBOutlet
        // that gesture's view belongs to.

        // Method #1:

        let button = sender.view as UIButton

        if button == thisButton {

        } else if button == thatButton {

        }
        ...

        // Or you can check the gesture view's tag to see which
        // button it belongs to (i.e. whichever button has a matching
        // tag).

        // Method #2:

        let button = sender.view as UIButton

        if button.tag == 1 {

        } else if button.tag == 2 {

        }
        ...

    }
}
Run Code Online (Sandbox Code Playgroud)