Methods 如何使我的按钮以编程方式响应多次触摸?

Methods 如何使我的按钮以编程方式响应多次触摸?,methods,colors,uibutton,touch,Methods,Colors,Uibutton,Touch,我已通过以下方式以编程方式创建了几个UIButton: UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(realX, realY, 64, 64); [button setTitle: str forState:UIControlStateNormal]; button.backgroundColor = greyButtonColor; 我希望按钮在颜色

我已通过以下方式以编程方式创建了几个UIButton:

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

button.frame = CGRectMake(realX, realY, 64, 64);

[button setTitle: str forState:UIControlStateNormal];

button.backgroundColor = greyButtonColor;

我希望按钮在颜色之间循环,这取决于它们被触摸的次数。例如:灰色>绿色>蓝色>返回灰色。那么,我该如何使用代码来实现这一点呢?上面的代码来自我创建的用于制作按钮的方法。教它如何根据触摸量进行操作的代码应该放在该方法中,还是放在新的方法中?

我不知道确切的代码,但我想我可以告诉你如何解决这个问题

  • 为每个按钮注册一个点击事件,并分配一个switchColor(buttonId)方法

  • 每个按钮都需要一个currentStateButtonId变量来保存按钮的状态

    • 初始值为0=灰色
    • 在你检查的方法里面
      • 如果(currentStateButtonId==0)
        ->currentStateButtonId=1
        ->按钮颜色=蓝色
      • 否则
        ->currentStateButtonId=0
        ->按钮颜色=灰色