具有单按和长按事件的 UIButton 快速

UIButton with single press and long press events swift(具有单按和长按事件的 UIButton 快速)
本文介绍了具有单按和长按事件的 UIButton 快速的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 button clickbutton long click 上触发两个动作.我在界面生成器中添加了一个 UIbutton.我如何使用 IBAction 触发两个动作有人可以告诉我如何存档吗?

I want to trigger two action on button click and button long click. I have add a UIbutton in my interface builder. How can i trigger two action using IBAction can somebody tell me how to archive this ?

这是我用来点击按钮的代码

this is the code i have used for a button click

@IBAction func buttonPressed (sender: UIButton) {……}

我可以使用这种方法还是必须使用其他方法进行长按?

can i use this method or do i have to use another method for long click ?

推荐答案

如果您想通过单击执行任何操作并长按您可以通过这种方式将手势添加到按钮中:

If you want to perform any action with single tap you and long press the you can add gestures into button this way:

@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 ..

This way you can add multiple method for single button and you just need Outlet for that button for that..

这篇关于具有单按和长按事件的 UIButton 快速的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
Dropbox Files.download does not start when number of files in folder is gt; 1000(当文件夹中的文件数为1000时,Dropbox Files.Download不会启动)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)