繁体   English   中英

很多按钮添加相同的addTarget(_:action:for :)方法

[英]a lot of button add same addTarget(_:action:for:) method

我正在开发一个计算应用程序。 并有很多按钮,我将它们存储在一个大的stackView中(在SB中创建,没有插座)。 每个按钮都会投射一些阴影(也在SB属性中设置)。 我想摆脱按下按钮时的阴影。 tapGestureRecognizer或目标动作只能影响一个UIButton。 任何方便的方法

PS我的意思是当按钮.touchupinside或tapGestureRecognizer .end .start。当手指移动按钮仍应投射阴影时

帮助赞赏

UIButton会在单击时突出显示,因此请选中按钮设置将突出显示状态配置中的标题颜色更改为默认状态,或者您可以设置:

[button setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];

如果要控制“代码突出显示”,则可以禁用子类Button突出显示的普通文本,并在touchesBegin中禁用:

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    if (self.state == UIControlStateHighlighted) {
        [self setHighlighted:NO];
    }
}

您可以将相同的IBAction分配给多个按钮,以便为所有按钮调用相同的方法

func buttonDidTouch(sender:UIButton) {}

现在,如果您想确定正在调用的确切按钮,则可以使用UIButton.tag属性进行标识。 可以在SB中为每个按钮设置标签。

有很多方法可以实现目标,但我将在下面进行介绍。

请给出我的逻辑。 取一个UIButton临时可选变量

防爆。

var myTemButton: UIButton()?

在按钮动作方式

@IBAction func myButtonActionFunc(_ sender: UIButton) {
  if myTemButton == nil {
    myTemButton = sender 
  }
  else {
    // Use "myTemButton" and write here code for remove shadow Or other stuff that you want.
  }

  // use "sender" and write code here for apply shadow of button or other stuff that you want.
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM