在ios中识别两个Uibutton上的Longpress手势

Ran*_*jit 4 ios

我有两个UI按钮,我想在两者上实现Longpressgesture.

所以我写了下面的代码..

-(void)viewdidLoad
{
 UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(buttonLongPressed:)];
    longPress.minimumPressDuration = 0.5;
    [Button1 addGestureRecognizer:longPress];
    [Button2 addGestureRecognizer:longPress];

}

- (void)buttonLongPressed:(UILongPressGestureRecognizer *)sender
{    
    if(sender.state == UIGestureRecognizerStateBegan) 
    {        

    }   
}
Run Code Online (Sandbox Code Playgroud)

现在我的疑问是如何检查哪个按钮是长按?

谢谢Ranjit

tar*_*mes 6

首先,请注意,手势识别器应该只附加到一个视图.您应该为每个按钮创建一个新实例.

要回答您的问题,您可以为按钮添加标记值:

Button1.tag = 1000;
Button2.tag = 1001;
Run Code Online (Sandbox Code Playgroud)

然后在识别器中测试它们:

UIView *view = sender.view;
int tag = view.tag;

if (tag == 1000) {
...
}
Run Code Online (Sandbox Code Playgroud)

您可以输入任何标记值,但我经常以1000之类的高值开始,以避免与我在Interface Builder中分配的任何其他标记发生冲突.

另一种选择是为每个识别器使用不同的处理功能.