ios - 立即触发 UIButton

标签 ios uibutton

现在我有一个像这样的方形按钮

关闭

enter image description here

enter image description here

开启 我希望按钮在我第一次按下时立即改变(不要等待手指抬起),所以我需要它立即触发并且只触发一次,因为我还将包含一个长按手势来执行不同的操作。我将如何着手让按钮表现得像这样?现在它只适用于我松开手指的时候。 (所有配置已在界面构建器中完成)。

编辑:

正如 liushaikobe 在下面解释的那样,我现在已将事件设置为“Touch Down”。按钮现在可以正确触发,但在我松开手指之前图像不会改变:

- (IBAction)pressButton:(id)sender {

    NSLog(@"button pressed");
    button.selected = YES;
}

应该立即选择该按钮,我在界面构建器中将关闭图像设置为默认设置,将打开图像设置为选中。知道我在这里做错了什么吗?

最佳答案

为状态 UIControlEventTouchDown 上的按钮添加一个 Action 。

这是“按下”按钮而不是手指离开屏幕时的状态(称为 UIControlEventTouchUpInside)。

enter image description here

关于ios - 立即触发 UIButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33275130/

相关文章:

ios - uicollectionviewcell swift ios 中的自定义复选框

ios - 导航 Collection View 时导航栏损坏

ios - pch 文件中的枚举?

ios - 我正在尝试将值从 UICollectionView Item 传递到 Swift 4 中的 UIButton

ios - 在 iOS7 中自定义 UIButton 类的行为

iphone - 带有圆角和边框的 UIButton

ios - 以编程方式添加UIButton时不可单击

ios - 通过导航 Controller 通过 segue 传递数据 - Objective C

ios - 在应用程序购买中收到零产品

ios - 如何在 Xcode 中隐藏 Outlet 并使其不再出现?