【问题标题】:Count button event计数按钮事件
【发布时间】:2013-01-17 14:20:31
【问题描述】:

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

【问题讨论】:

标签: iphone xcode


【解决方案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;
    }

}

【讨论】:

    【解决方案2】:

    UIImageView 上使用双击手势.....。不要忘记将UIImageView's UserInteractionEnabled 设置为 TRUE

    ImageView.userInteractionEnabled = YES;
    

    【讨论】:

      【解决方案3】:

      这里 tapCount 是在 .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
      }
          
      }
      

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-04-18
          • 1970-01-01
          • 2012-01-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多