2012-08-30 64 views
4

如何获取按钮单击单击或长按它点击事件?长按并单击按钮

+0

能否请您解释一下? – IronManGill

+0

你有什么试图找出它?然而...你可以从这里开始** [UILongPressGestureRecognizer类参考](http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UILongPressGestureRecognizer_Class/Reference/Reference.html)** – holex

+0

点击?你用鼠标吗? – Fogmeister

回答

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 
} 
+0

点击?什么是点击?鼠标? – Fogmeister

+0

点击被视为触摸。我已经修改为触摸。 – iPrabu

1

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

然后,您将定义“长按”的阈值,并在长时间阈值已过时处理触摸事件作为“长按”。

+0

你可以把PLZ代码放在那里没有办法“触摸里面”“触及” – Nims