只调用一次长按按钮动作而不是快速射击

Posted

技术标签:

【中文标题】只调用一次长按按钮动作而不是快速射击【英文标题】:Calling Long Press Button action only once not rapid fire 【发布时间】:2020-01-16 03:40:53 【问题描述】:

这是我的长按代码。当我长按按钮时,它一直被调用。如何设置它只调用一次,然后仅在松开手指并再次开始长按时再次调用它?

    let tapGesture = UITapGestureRecognizer(target: self, action: #selector (tap))  
    let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(long))  
    tapGesture.numberOfTapsRequired = 1

    self.reminderButton.addGestureRecognizer(tapGesture)
    self.reminderButton.addGestureRecognizer(longGesture)

【问题讨论】:

【参考方案1】:

在您的选择器中使用此代码

@objc func gestureAction(gesture: UIGestureRecognizer) 
if let longPress = gesture as? UILongPressGestureRecognizer 
    if longPress.state == UIGestureRecognizer.State.began 

     else 

    

【讨论】:

if (gesture as? UILongPressGestureRecognizer)?.state == .began

以上是关于只调用一次长按按钮动作而不是快速射击的主要内容,如果未能解决你的问题,请参考以下文章

长按多次调用 UIButton 的动作

Android Nougat,Oreo - 如何将长按动作添加到快速切换?

【Android开发】如何 使得 android长按按钮=多次按下按钮

Android - 是不是可以禁用长按主页按钮以避免任务管理器?

iOS如何自定义支持长按操作的按钮

Android Item长按弹出删除,点击删除执行删除对应的文件