计数按钮事件

时间:2013-01-17 14:20:32

标签: iphone xcode

如何对按钮点击事件执行两项操作。例如,单击即可执行一项操作,连续两次点击我必须执行另一项操作。

4 个答案:

答案 0 :(得分:1)

UIImageView上使用双击手势.....别忘了将UIImageView's UserInteractionEnabled设置为 TRUE

ImageView.userInteractionEnabled = YES;

答案 1 :(得分:1)

请写下面代码可能对您有所帮助:)

此处 tapCount 是{strong} .h文件中声明的int变量

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

}

这可能对您有所帮助:))

答案 2 :(得分:1)

将初始按钮标记设置为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;
    }

}

答案 3 :(得分:0)

您可以使用条件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];