[英]Why isn't my UILongPressGestureRecognizer working?
我有以下代碼,我的長按沒有按應有的方式工作。 有人能弄清楚為什么它不起作用嗎?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: "myButton:")
longPressRecognizer.minimumPressDuration = 0.5
myButton.addGestureRecognizer(longPressRecognizer)
}
@IBOutlet weak var myButton: UIButton!
@IBAction func myButton(longPress: UILongPressGestureRecognizer) {
if longPress.state != .Began {
presentAlertController()
return
}
}
當我按住按鈕時會出現此錯誤
2016-01-09 00:41:28.785 longPressTest[1870:551106] Warning: Attempt to present <UIAlertController: 0x144d6a500> on <longPressTest.ViewController: 0x144e3a450> which is already presenting <UIAlertController: 0x144e59d80>
2016-01-09 00:41:28.903 longPressTest[1870:551106] Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior (<UIAlertController: 0x144d6a500>)
2016-01-09 00:41:28.905 longPressTest[1870:551106] Warning: Attempt to present <UIAlertController: 0x144e54bb0> on <longPressTest.ViewController: 0x144e3a450> which is already presenting <UIAlertController: 0x144e59d80>
Cancel
長按手勢是連續手勢。 這意味着當識別myButton(_:)
檢測到state == .Began
的長按開始(0.5秒后)時,識別器將調用您的函數( myButton(_:)
)。開始,並在state == .Changed
的屏幕上移動觸摸時反復進行state == .Changed
,並且當手勢以state == .Ended
結束時再次state == .Ended
。 你試圖呈現在每個警報.Changed
呼叫並在.Ended
呼叫,而當你試圖呈現一個已經被提出警告你的錯誤。
如果要在0.5秒后立即顯示警報,請在狀態為.Began
時執行此警報,而不是在狀態為.Began
以外的任何狀態時執行此警報。
@IBAction func myButton(longPress: UILongPressGestureRecognizer) {
if longPress.state == .Began {
presentAlertController()
}
}
您只會收到一個帶有state .Began
呼叫,因此您不會在警報已經呈現時再次嘗試呈現。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.