长按和单击按钮

4

如何使按钮只能单击一次或长按后才触发点击事件?


你尝试过什么来找到它?不过...你可以从这里开始:UILongPressGestureRecognizer类参考 - holex
点击?你用鼠标吗? - Fogmeister
2个回答

3

请检查这段代码

//Add Long Press Gesture Reconizer
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] 
                                      initWithTarget:self action:@selector(handleLongPress:)];
longPress.minimumPressDuration = 3; //seconds
longPress.delegate = self;
[yourButton addGestureRecognizer:longPress];

//Add button touch
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDetected:)];
tapGesture.numberOfTapsRequired = 1;
tapGesture.numberOfTouchesRequired = 1;
[yourButton addGestureRecognizer:tapGesture];
//For touch you can also set selector for button event with Controlevent touchupinside


-(void) handleLongPress : (id)sender
{
   //Long Press done by the user
}

-(void) tapDetected : (id) sender
{
   //Button Tapped by user
}

点击?什么是点击?鼠标? - Fogmeister
点击被视为触摸。我已经修改为触摸。 - ipraba

1
你可以使用NSTimer来测量按钮的“touch down inside”和“touch up inside”事件之间的持续时间。
然后,您将定义一个“长按”阈值,并在持续时间阈值已过时处理触摸事件作为“长按”。

能否请您将代码放在那里,没有“touch down inside”这个方法,而是“touch down”。 - Nims

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接