繁体   English   中英

长按并单击按钮

[英]Long press and single click on button

如何获得按钮单击一次或长按它点击事件?

检查此代码

//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
}

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM