ios - 要知道哪个 UILongPressGestureRecognizer 被触发

标签 ios objective-c uigesturerecognizer

我加 UILongPressGestureRecognizer给几个 UIButton使用代码:

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(btnLong:)];
[btnOne addGestureRecognizer:longPress]; //there are btnTwo, btnThree for example

当我长按一个按钮时,该方法被调用:
-(void)btnLong:(UILongPressGestureRecognizer *)gestureRecognizer{

    if ([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
    }
}  

我的问题是,我怎么知道是哪个 UILongPressGestureRecognizer被触发,因为 UILongPressGestureRecognizer 没有标签属性.

最佳答案

给每个按钮一个唯一的标签号。然后在您的操作方法中,您可以执行以下操作:

-(void)btnLong:(UILongPressGestureRecognizer *)gestureRecognizer{
    if ([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
        UIView *view = gestureRecognizer.view;
        if (view.tag == 1) { // first button's tag
            // process 1st button
        } else if (view.tag == 2) { // second button's tag
            // process 2nd button
        }
    }
}

另一种选择,如果每个按钮都有 socket ,您可以执行以下操作:
-(void)btnLong:(UILongPressGestureRecognizer *)gestureRecognizer{
    if ([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
        UIView *view = gestureRecognizer.view;
        if (view == self.firstButton) {
            // process 1st button
        } else if (view == self.secondButton) {
            // process 2nd button
        }
    }
}

在哪里 firstButtonsecondButton是您的按钮属性。是的,使用 ==适合检查手势的 View 是否是按钮之一,因为您确实是要比较对象指针。

关于ios - 要知道哪个 UILongPressGestureRecognizer 被触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17518590/

相关文章:

ios - 按下按钮连续增加标签值

iOS 7.1 默认色调颜色不会为 UISwitch 更改

iOS 7 uitabbar 显示,在 ios8 上不可见

iphone - 如何从UIAlertView更改键盘颜色

iOS:如何在父类(super class)中正确实现自定义手势识别器?

ios - UISearchBar 提示出现在栏上,而不是在栏的顶部

ios - 随机 float 的数学方程式?

ios - 外设管理器如何理解哪个是 CBCentral(设备)试图访问服务/特征

ios - 如何让 View 跟随我的手指移动?方向是从左到右

objective-c - UITapGestureRecognizer 仅适用于初始化阶段可见的项目