【问题标题】:iphone - do an action while a button is pressediphone - 按下按钮时执行操作
【发布时间】:2011-05-31 06:03:29
【问题描述】:

例如,我有两个音量按钮(+ 和 -)

如何实现诸如按住 + 按钮时,它会每隔一段时间逐步提高音量? (我只对在按下按钮时每隔一段时间做一个动作感兴趣)

【问题讨论】:

    标签: iphone objective-c button


    【解决方案1】:

    您可以为此使用计时器。触摸开始时启动计时器。如果计时器到期,请增大或减小音量并重新启动计时器。当触摸结束时,取消定时器。

    【讨论】:

      【解决方案2】:

      假设您有两个按钮,一个用于 - 另一个用于 + ,

      您可以将间隔信息存储在按钮的标签字段中

      在您的按钮标签属性中安装间隔值。

      myPulseButton.tag = 10;
      myMinusButton.tag = 10;
      

      使用您的按钮添加操作。

      [myPulseButton addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside]; 
      
      [myMinusButton addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
      

      如下实现 buttonEvent 方法。

      -(void) buttonEvent:(id) sender
      {
          UIButton* myButton = (UIButton*) sender;
          if(myButton == myPulseButton)
          {
            [self increaseVolume:myPulseButton.tag];
          }
          else if(myButton == myMinusButton)
          { 
            [self decreaseVolume:myMinusButton.tag];
          }
      }
      

      【讨论】:

        【解决方案3】:

        先添加按钮...

         UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            [button addTarget:self  action:@selector(increase)forControlEvents:UIControlEventTouchDown];
            [button setTitle:@"Show View" forState:UIControlStateNormal];
            button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
            [view addSubview:button];
        

        然后编写按键的方法。

        -(void)increase
        {
        //increase the volume here
        }
        

        如果不是您想要的解决方案,请添加评论...

        【讨论】:

          猜你喜欢
          • 2011-01-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-02-27
          • 2012-11-15
          • 2021-03-09
          相关资源
          最近更新 更多