UIButton高亮状态仅适用于.normal-> .selected?

时间:2019-01-24 21:38:32

标签: ios swift uibutton

使用IB,我已将图像分配到按钮的.normal,.highlighted和.selected状态。在.normal->。selected过渡(someButton.isSelected = true)中按下按钮时,触摸时可以正确显示.highlighted图像,但是将someButton.isSelected = false设置为.normal时,我看到标准的快速淡入淡出过渡,而不是我的.highlighted图片。我在this SO post from 2013中看到了类似的问题,但是我想知道该解决方案是否仍然适用于Swift 4,因为我不太了解它。我的目标是在每次按钮触摸时都呈现“发光”图像。谢谢!

enter image description here

完整代码如下:

@IBAction func someButtonArray(_ sender: UIButton) {
    UIView.transition(with: sender, duration: 0.3, options: .transitionCrossDissolve, animations: {
        sender.setImage(UIImage(named: "TRANSITION_Pad-10.png"), for: UIControl.State.highlighted)
    }, completion: nil)
    if sender.isSelected == true {
        UIView.transition(with: sender, duration: 0.3, options: .transitionCrossDissolve, animations: {
            sender.setImage(UIImage(named: "INITIAL_Pad-13.png"), for: UIControl.State.normal)
        }, completion: nil)
        sender.isSelected = false
} else if sender.isSelected != true {
    sender.isSelected = true
    for buttons in 0...11 where buttons != (sender.tag) {
        someButtonArray[buttons].isSelected = false
    }
  }
    tableView.reloadData()
}

2 个答案:

答案 0 :(得分:1)

在视图中执行此操作

        yourButtonObj.adjustsImageWhenHighlighted = true

答案 1 :(得分:0)

添加如下一行:

[self.allTempoButton setBackgroundImage:[UIImage imageNamed:@"Normal"] forState:UIControlStateNormal];
[self.allTempoButton setBackgroundImage:[UIImage imageNamed:@"Selected"] forState:UIControlStateHighlighted];
[self.allTempoButton setBackgroundImage:[UIImage imageNamed:@"Selected"] forState:UIControlStateSelected];
[self.allTempoButton setBackgroundImage:[UIImage imageNamed:@"Selected"] forState:UIControlStateSelected | UIControlStateHighlighted];