簡體   English   中英

如何添加UILongPressGestureRecognizer發布功能

[英]How to add a UILongPressGestureRecognizer release function

我正在使用長按手勢識別器,因為沒有它,如果我在按鈕上快速單擊並釋放,代碼將無法正常執行。 但是使用長按手勢識別器,我的buttonUp功能不會執行。 如何使用長按手勢識別器檢查手指是否在屏幕外?

如果您想在按鈕中執行釋放操作並按住操作,則可以參考此操作!

要么

你可以在這里查看長按的手勢狀態!

要么

處理來自Apple Developer Documentation的長按手勢

希望能幫助到你。 干杯。

如果您想通過單擊並長按來執行任何操作,您可以通過以下方式將按鈕添加到按鈕:

 @IBOutlet weak var btn: UIButton!

override func viewDidLoad() {

    let tapGesture = UITapGestureRecognizer(target: self, #selector (tap))  //Tap function will call when user tap on button
    let longGesture = UILongPressGestureRecognizer(target: self, #selector(long))  //Long function will call when user long press on button.
    tapGesture.numberOfTapsRequired = 1
    btn.addGestureRecognizer(tapGesture)
    btn.addGestureRecognizer(longGesture)
}

@objc func tap() {

    print("Tap happend")
}

@objc func long() {

    print("Long press")
}

這樣,您可以為單個按鈕添加多個方法,您只需為該按鈕選擇Outlet。

暫無
暫無

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

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