长按并单击按钮

时间:2012-08-30 12:47:37

标签: iphone objective-c ios uibutton

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

2 个答案:

答案 0 :(得分: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
}

答案 1 :(得分:1)

您可以使用NSTimer来衡量按钮上“触及内部”和“内部触摸”事件之间的持续时间。

然后,您可以为“长按”定义阈值,并在持续时间阈值已通过时将触摸事件处理为“长按”。