为什么我的 UILongPressGestureRecognizer 不工作?
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
长按手势是连续手势。这意味着识别器将在检测到使用 state == .Began
开始长按(0.5 秒后)时调用您的函数 (myButton(_:)
),并随着触摸在屏幕上的移动而重复使用 [=13] =],当手势以 state == .Ended
结束时再次出现。您尝试在每个 .Changed
调用和 .Ended
调用中显示警报,但当您尝试显示已经显示的警报时出现错误。
如果您想在 0.5 秒后立即显示警报,请在状态为 .Began
时执行,而不是在状态为任何状态时执行 except .Began
.
@IBAction func myButton(longPress: UILongPressGestureRecognizer) {
if longPress.state == .Began {
presentAlertController()
}
}
您只会接到一个状态为 .Began
的电话,因此您不会在警报已经显示时再次尝试显示它。
我有以下代码,但我的长按没有按应有的方式工作。有人能弄清楚为什么它不起作用吗?
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
长按手势是连续手势。这意味着识别器将在检测到使用 state == .Began
开始长按(0.5 秒后)时调用您的函数 (myButton(_:)
),并随着触摸在屏幕上的移动而重复使用 [=13] =],当手势以 state == .Ended
结束时再次出现。您尝试在每个 .Changed
调用和 .Ended
调用中显示警报,但当您尝试显示已经显示的警报时出现错误。
如果您想在 0.5 秒后立即显示警报,请在状态为 .Began
时执行,而不是在状态为任何状态时执行 except .Began
.
@IBAction func myButton(longPress: UILongPressGestureRecognizer) {
if longPress.state == .Began {
presentAlertController()
}
}
您只会接到一个状态为 .Began
的电话,因此您不会在警报已经显示时再次尝试显示它。