繁体   English   中英

以编程方式选择时如何让按钮图像更改为不同的图像?

[英]How to get a button image to change to a different image when selected programmatically?

当数组中的不同项目显示在 label 中时,我有一个按钮可以更改图像。 我希望按钮图像在选择按钮时更改为不同的图像。 如何以编程方式执行此操作? 我尝试了 button1.setImage(UIImage(named: "GreenHeart"), for: .selected),但这似乎不起作用(图像只是停留在 Heart)。 出于某种原因,如果我使用 .highlighted 而不是 .selected 一切正常(我可以看到 GreenHeart 图像)。 不确定这是否有帮助,但我是编码新手,所以我想我会在这里添加它以获得更多支持。

import UIKit
class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
}

@IBOutlet weak var quotesLabel: UILabel!
var firstQuote = -1
var quotes = ["The best time to plant a tree was 20 years ago - The next best time is today - Unknown",
                            "Everytime you spend money, you're casting a vote for the type of world you want - Anna Lappe",
                            "Buy less, choose well, make it last - Vivienne Westwood",
                            "The future depends on what we do in the present - Mahatma Gandhi",
]


@IBAction func nextButton(_ sender: Any) {
   
    if firstQuote < quotes.count{
    firstQuote = (firstQuote + 1) % quotes.count
    quotesLabel.text = quotes[firstQuote]
    }
    
    let quote1 = "The best time to plant a tree was 20 years ago - The next best time is today - Unknown"
    let quote2 = "Everytime you spend money, you're casting a vote for the type of world you want - Anna Lappe"
  
    let quote = quotesLabel.text

        let view1 = UIView()
        view.addSubview(view1)
        view1.frame = CGRect(x: 100, y: 500, width: 200, height: 100)
        view1.backgroundColor = .clear
        let button1 = UIButton(type: .custom)
        button1.setImage(UIImage(named: "Heart"), for: .normal)
        button1.setImage(UIImage(named: "GreenHeart"), for: .selected)
        
        view1.addSubview(button1)
        button1.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
        button1.isHidden = true
    
        if quote == quote1{
                button1.isHidden = false
            } else {
                button1.isHidden = true
            }
    
        let button2 = UIButton(type: .custom)
        button2.setImage(UIImage(named: "GreenHeart"), for: .normal)
        view1.addSubview(button2)
        button2.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
        button2.isHidden = true

        if quote == quote2{
            button2.isHidden = false
        } else {
            button2.isHidden = true
        }
}


}

第一个问题是每次单击按钮时都会创建一个新的视图和按钮。 您只需要创建一次并执行此操作,您需要将该部分代码移动到 viewDidLoad() 中。

之后,如果您想在每次单击不同的按钮时更改按钮的图像,您可以创建一个变量:

var isSelected: 布尔 = 假

在 nextButton() 方法中,切换 boolean 值。

isSelected =.isSelected。

这意味着,如果 isSelected 为真,它将为假。 如果是假的,那就是真的。

之后,您可以检查 boolean 值是否为真,并将图像更改为 boolean 值。

button1.setImage(UIImage(name: isSelected? "Heart": "GreenHeart"), for: .normal)

如果 isSelected 为 true,则显示 Heart 图像,如果为 false,则显示 GreenHeart。

@IBOutlet weak var quotesLabel: UILabel!

private let button1 = UIButton(type: .custom)
private var isSelected: Bool = false

var firstQuote = -1
var quotes = ["The best time to plant a tree was 20 years ago - The next best time is today - Unknown",
              "Everytime you spend money, you're casting a vote for the type of world you want - Anna Lappe",
              "Buy less, choose well, make it last - Vivienne Westwood",
              "The future depends on what we do in the present - Mahatma Gandhi",
]
override func viewDidLoad() {
    super.viewDidLoad()
    setupUI()
}

private func setupUI() {
    let view1 = UIView()
    view.addSubview(view1)
    view1.frame = CGRect(x: 100, y: 500, width: 200, height: 100)
    view1.backgroundColor = .clear
    
    view1.addSubview(button1)
    button1.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
    
    // button1.isHidden = true
}

@IBAction func nextButton(_ sender: UIButton) {
    
    if firstQuote < quotes.count{
        firstQuote = (firstQuote + 1) % quotes.count
        quotesLabel.text = quotes[firstQuote]
        }
   
    isSelected = !isSelected

    let quote1 = "The best time to plant a tree was 20 years ago - The next best time is today - Unknown"
    let quote2 = "Everytime you spend money, you're casting a vote for the type of world you want - Anna Lappe"
    let quote = quotesLabel.text

    button1.setImage(UIImage(name: isSelected ? "Heart": "GreenHeart"), for: .normal)
    /*
    if quote == quote2{
        button1.isHidden = false
    } else {
        button1.isHidden = true
    } */
}

暂无
暂无

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

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