簡體   English   中英

UILongPressGestureRecognizer訪問被點擊的按鈕

[英]UILongPressGestureRecognizer access the button being clicked

我在我的視圖中設置了一個UILongPressGestureRecognizer來處理四個不同的按鈕,如何在我的代碼中訪問哪個按鈕被點擊?

我的UILongPressGestureRecognizer看起來像這樣:

@IBAction func editText(sender: UILongPressGestureRecognizer) {
        textFieldInput.hidden = false
        iphoneSaveCharName.hidden = false
      }

我想使用長按,以便我可以編輯按鈕文本

編輯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)
            }
        }

    }
}

編輯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)
                }
        }
    }
}

回答:

這是最終的視圖控件:

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)
                }
        }
    }
}

因為每個人都幫助我,所以我最想給你一個最好的答案! 我必須為每個按鈕創建一個長按,否則代碼會混淆。

你的問題向我介紹了一個非常酷的功能,謝謝! :)

事實證明,如果你附加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

    }
}

但是,如果你想繼續保持UILongPressGestureRecognizer IBAction從分離UIButtonIBAction (即鏈接UIButton s到iphoneSaveTextInput:和你UILongPressGestureRecognizer s到editText:這應該是OK了。 只需保持你的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 {

        }
        ...

    }
}

您不希望在不同視圖中使用相同的手勢識別器:

你能將UIGestureRecognizer附加到多個視圖嗎?

手勢識別器的view屬性可能是您想要檢查的內容,但您希望每個視圖都有不同的手勢識別器。

如果你願意,它們都可以鏈接到同一個選擇器,你可以通過這種方式訪問​​傳入識別器的視圖。

制作按鈕的一般方法根據您按下它的時間而有不同的動作,看起來像這樣(計時器是屬性),

@IBAction func buttonDown(sender: UIButton) { // connected to touchDown
       timer = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: "handleTimer", userInfo: sender, repeats: false)
    }


    @IBAction func buttonUp(sender: UIButton) { // connected to touchUpInside
        if timer != nil {
            timer.invalidate()
            println("change view")
        }
    }


    func handleTimer() {
        var button = timer.userInfo as UIButton
        timer = nil
        button.setTitle("New Title", forState: .Normal)
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM