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

最佳答案

检查此代码

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

关于iphone - 长按并单击按钮,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12197212/

10-13 04:03