1

我用代码添加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.

4

3 回答 3

3

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

-(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
        }
    }
}

另一种选择,如果每个按钮都有插座,您可以执行以下操作:

-(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
        }
    }
}

wherefirstButtonsecondButton是您的按钮属性。是的, using==适合检查手势的视图是否是按钮之一,因为您确实是要比较对象指针。

于 2013-07-08T03:38:33.660 回答
0

为什么不把手势 rec 放在 common 上superview?然后您可以使用 确定哪个UIView是长按的locationInView,然后访问视图的标记属性。

于 2013-07-08T03:18:47.883 回答
0

我使用 UIView 作为 tableview 单元格的子视图。我将 UILongGesture 应用于此。这是为我工作的代码。

func handleLongPressGesture(_ longPressGestureRecognizer: UILongPressGestureRecognizer){

            if longPressGestureRecognizer.state == UIGestureRecognizerState.began
            {
                let touchPoint = longPressGestureRecognizer.location(in: tableViewObj)

                            if let indexPath = tableViewObj.indexPathForRow(at: touchPoint)
                            {
                                print(indexPath.row)



                }
            }
}

你有索引路径。你可以做任何你需要做的事情。

于 2017-08-18T08:48:18.967 回答