4

如何让按钮单击一次或长按单击事件?

4

2 回答 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
}
于 2012-08-30T13:17:29.427 回答
1

您可以使用 NSTimer 来测量按钮上的“touch down inside”和“touch up inside”事件之间的持续时间。

然后,您将为“长按”定义一个阈值,如果已超过持续时间阈值,则将触摸事件作为“长按”处理。

于 2012-08-30T12:53:31.667 回答