-1

可能重复:
如何使用 uiview 和 uibutton 检测 Uitableview 单元格上的点击?
UIButton 长按事件

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

4

3 回答 3

1

我只是谷歌它,我从堆栈溢出中得到了最佳答案

- (void)viewDidLoad
{
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
    [self.button addGestureRecognizer:longPress];
    [longPress release];

 [super viewDidLoad];


}

和事件:-

- (void)longPress:(UILongPressGestureRecognizer*)gesture {
    if ( gesture.state == UIGestureRecognizerStateEnded ) {
         NSLog(@"Long Press");
    }
}
于 2013-01-18T06:35:15.963 回答
0

您可以从创建 UILongPressGestureRecognizer 实例并将其附加到按钮开始。

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");
    }
}

现在这将是基本方法。您还可以设置压力机的最短持续时间以及可以容忍的错误量。另请注意,如果您在识别手势后会调用该方法几次,因此如果您想在结束时执行某些操作,则必须检查其状态并进行处理。

参考

于 2013-01-18T06:36:42.810 回答
0
- (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:)];
于 2013-01-18T06:36:50.403 回答