2 bluetooth99 bluetooth99 于 2013.01.18 11:04 提问

对同一个button生成不同的事件

如何对一个button定义两次事件,比如,单击时产生一个事件,然后双击时产生另一个不同的事件?请高手指教,谢谢!

3个回答

redCoral_
redCoral_   2013.01.18 13:24
已采纳

可以用条件句 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];
g989_1314125
g989_1314125   2013.01.18 11:34

设置初始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;
    }    
}
lianlianbushell
lianlianbushell   2013.01.18 13:50

我的方法可行:

其中tapCountint变量,在 .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
}

}
Csdn user default icon
上传中...
上传图片
插入图片