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

2023-07-08移动开发问题
35

本文介绍了具有单按和长按事件的 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 快速的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

硬件音量按钮更改应用程序音量
Hardware Volume buttons change in app volume(硬件音量按钮更改应用程序音量)...
2024-08-12 移动开发问题
10

Cocos2d - 如何检查不同层中对象之间的交集
Cocos2d - How to check for Intersection between objects in different layers(Cocos2d - 如何检查不同层中对象之间的交集)...
2024-08-12 移动开发问题
8

恢复游戏 cocos2d
Resume game cocos2d(恢复游戏 cocos2d)...
2024-08-12 移动开发问题
6

突出显示朗读文本(在 iPhone 的故事书类型应用程序中)
Highlight Read-Along Text (in a storybook type app for iPhone)(突出显示朗读文本(在 iPhone 的故事书类型应用程序中))...
2024-08-12 移动开发问题
9

Cocos2D + 仅禁用 Retina iPad 图形
Cocos2D + Disabling only Retina iPad Graphics(Cocos2D + 仅禁用 Retina iPad 图形)...
2024-08-12 移动开发问题
10

正确的 cocos2d 场景重启?
Proper cocos2d scene restart?(正确的 cocos2d 场景重启?)...
2024-08-12 移动开发问题
7