Iphone 长按按钮事件

Iphone 长按按钮事件,iphone,xcode,Iphone,Xcode,可能重复: 我正在通过表格自定义单元格加载按钮。如何识别用户单击按钮还是长按按钮事件?我只是用谷歌搜索了一下,从堆栈溢出中得到了最佳答案 及活动:- - (void)longPress:(UILongPressGestureRecognizer*)gesture { if ( gesture.state == UIGestureRecognizerStateEnded ) { NSLog(@"Long Press"); } } 首先,您可以创建UILong

可能重复:


我正在通过表格自定义单元格加载按钮。如何识别用户单击按钮还是长按按钮事件?

我只是用谷歌搜索了一下,从堆栈溢出中得到了最佳答案

及活动:-

- (void)longPress:(UILongPressGestureRecognizer*)gesture {
    if ( gesture.state == UIGestureRecognizerStateEnded ) {
         NSLog(@"Long Press");
    }
}

首先,您可以创建UILongPressGestureRecognitor实例并将其附加到按钮

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
[self.button addGestureRecognizer:longPress];
[longPress release];
然后实现处理手势的方法

- (void)longPress:(UILongPressGestureRecognizer*)gesture {
    if ( gesture.state == UIGestureRecognizerStateEnded ) {
         NSLog(@"Long Press");
    }
}
现在这将是基本的方法。您还可以设置压力机的最短持续时间以及可容忍的误差大小。还要注意的是,如果您在识别手势后调用了该方法几次,因此如果您想在手势结束时执行某些操作,则必须检查其状态并进行处理


您可以使用
LongPressGestureRecognitor
…我的问题是自定义单元格中没有viewDidLoad。仅在viewDidLoad中添加此方法是不可取的。在您的自定义单元格中,您只需将上述代码添加到您的costome单元格代码中,当您添加类似CellForRowatinex等的按钮时,共享您的代码我将编辑它,我认为这是重复的问题,您应该将其标记为重复,而不是给出答案。这是stackOVerflow Bro的新用户,我还提到了此答案的链接,我只是在代码中使用了方法:)'
- (void)longPress:(UILongPressGestureRecognizer*)gesture {
    if ( gesture.state == UIGestureRecognizerStateEnded ) {
         NSLog(@"Long Press");
    }
}
- (void)setLongTouchAction:(SEL)newValue
{
    if (newValue == NULL)
    {
        [self removeGestureRecognizer:longPressGestureRecognizer];
        [longPressGestureRecognizer release];
        longPressGestureRecognizer = nil;
    }
    else
    {
        [longPressGestureRecognizer release];
        longPressGestureRecognizer = nil;

        longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:[[self allTargets] anyObject] action:newValue];
        [self addGestureRecognizer:longPressGestureRecognizer];
    }
}


[undoButton addTarget:self action:@selector(performUndo:) forControlEvents:UIControlEventTouchUpInside];
[undoButton setLongTouchAction:@selector(showUndoOptions:)];