可能重复:
How to detect tap on Uitableview cell with uiview and uibutton?
UIButton Long Press Event
我正在通过表格自定义单元格加载按钮。如何确定用户单击按钮或长按事件按钮?。
答案 0 :(得分:1)
我只是google它,我从堆栈溢出This
得到了最佳答案- (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");
}
}
答案 1 :(得分: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");
}
}
现在这将是基本方法。您还可以设置印刷机的最短持续时间以及可容忍的误差范围。并且还要注意,如果你在识别出手势之后几次调用该方法,那么如果你想在它结束时做一些事情,你将必须检查它的状态并处理它。
答案 2 :(得分: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:)];