ios - UIButton 的两种方法?

标签 ios objective-c uibutton

我们需要用两种方法设置一个UIButton,第一种是当你触摸它(向下和向上)时你得到一个 Action ,但是当你长按它时,你得到另一个。

例如,当您长按时获取有关它的数据,而当常规点击时,则另当别论。

如何使用 UIButton 实现此目的?

            UIButton *CAT = [UIButton buttonWithType:UIButtonTypeCustom];
            CAT.contentHorizontalAlignment=UIControlContentHorizontalAlignmentCenter;
            CAT.backgroundColor=[UIColor clearColor];
            [CAT addTarget:self action:@selector(newcat:)forControlEvents:UIControlEventTouchUpInside];

我已经开始用这个给它添加一个手势

  UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
        [CAT addGestureRecognizer:longPress];

但这只有在您松开手指时才会触发。 我希望在 1-2 秒后我的手指还在那里时触发它。 我可以这样做吗?我可以调整它触发所需的时间吗?

最佳答案

您可以使用事件UIControlEventTouchDown,为长按使用延迟时间并且您必须处理UIControlEventTouchUpInsideUIControlEventTouchUpOutside。祝你好运!

关于ios - UIButton 的两种方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26753743/

相关文章:

ios - 当按下 UIButton 并调用 IBAction 时,显示带有事件指示器的 UIAlertView 直到完成

ios - 如何在 Swift 中以编程方式更改 UIButton 状态

ios - 使用 iCloud 文档存储的最佳方式

ios - 如何在iOS中获取谷歌地图的中心位置经纬度?

ios - 获取 iOS 开发人员证书

ios - Xcode 8 预处理器宏?

ios - 在 uibutton SWIFT 周围包装文本

ios - 以编程方式更改 UIButton 的标题在 iOS Xcode-Beta 3 中不起作用

ios - iOS App因审核小组误认为其用途而被拒绝。我要重新提交吗?

ios - 使用字符替换制作有效的 jSON 字符串