问题描述
- 对同一个button生成不同的事件
- 如何对一个button定义两次事件,比如,单击时产生一个事件,然后双击时产生另一个不同的事件?请高手指教,谢谢!
解决方案
可以用条件句 setAction :
单击调用selectorSingleTouch.
其他调用selectorDoubleTouch.
//没有经过编译检测,只是给你个思路
UITapGestureRecognizer *singleTap=[[UITapGestureRecognizer alloc] initWithTarget: self action:@selector(doSingleTap)];singleTap.numberOfTapsRequired = 1; [self.view addGestureRecognizer:singleTap];UITapGestureRecognizer *doubleTap=[[UITapGestureRecognizer alloc] initWithTarget: self action:@selector(doDoubleTap)];doubleTap.numberOfTapsRequired = 2; [self.view addGestureRecognizer:doubleTap];
解决方案二:
设置初始button标签为0
然后:
-(IBAction)yourBtnPress:(id)sender{ UIButton *btn = (UIButton*)sender; if (btn.tag==0) { btn.tag=1; //singlePress } else if(btn.tag==1) { //double tap //if you want other action then change tag btn.tag=2; //if you restart task then btn.tag=0; } }
解决方案三:
我的方法可行:
其中tapCount
是int
变量,在 .h中声明
- (void)viewDidLoad{ tapCount = 0; // Count tap on Slider UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(ButtonTap:)]; [button addGestureRecognizer:gr];} - (void)ButtonTap:(UIGestureRecognizer *)g {/////////////// For TapCount//////////// tapCount = tapCount + 1; NSLog(@""Tap Count -- %d""tapCount);/////////////// For TapCount////////////if (tapCount == 1){ // code for first tap}else if (tapCount == 2){ // Code for second tap}}
时间: 2024-10-29 21:36:34