gpt4 book ai didi

ios - 每次单击按钮时可以更改背景图像吗?

转载 作者:行者123 更新时间:2023-11-30 12:52:31 26 4
gpt4 key购买 nike

我已检查文档并已成功添加图像作为背景,但是,既然已设置,我无法更改它。

我想要做的是每次单击按钮时更改背景图像。在网上查找时,我找到了有关如何更改按钮背景的信息,但没有找到实际背景。

import UIKit

class ViewController: UIViewController {

var arrays = ["A", "b", "C", "D", "E", "F"]
var currentIndex = 0
var image = UIImage()
var image3 = UIImage()

@IBOutlet weak var ChangeText: UILabel!

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
ChangeText.text = arrays[currentIndex]

self.view.backgroundColor = UIColor(patternImage: UIImage(named: "LaunchImage")!)
self.view.backgroundColor = UIColor(patternImage: UIImage(named: "Image2")!)
}

@IBAction func Button() {
currentIndex += 1

if currentIndex == arrays.count {
currentIndex = 0
}
ChangeText.text = arrays[currentIndex]

if image == UIImage(named: "Image2") {
print("Image")
}
self.view.backgroundColor = UIColor(patternImage: UIImage(named: "Image2")!)
}
}

强制展开 UIImage 是否可以?我知道强制展开是不受欢迎的,但我确信我会有一个背景图像。

我也尝试过这个,但我遇到了同样的问题,它不会多次更改背景图像。

```

    @IBAction func Button() {
currentIndex += 1

if currentIndex == arrays.count {
currentIndex = 0
}
ChangeText.text = arrays[currentIndex]

if image == UIImage(named: "Image2") {
print("Image")
}
self.view.backgroundColor = UIColor(patternImage: UIImage(named: "Image2")!)

if image3 == UIImage(named: "Image3") {
print("Image")
}
self.view.backgroundColor = UIColor(patternImage: UIImage(named: "Image3")!)
}
}

```

最佳答案

我不确定问题出在哪里,因为您没有向我们展示您已经尝试过的内容。

用于设置背景 View 的相同代码可以在任何地方使用,并且可以随意使用

    if <some condition>
{
self.view.backgroundColor = UIColor(patternImage: UIImage(named: "one")!)
}
if <another condition>
{
self.view.backgroundColor = UIColor(patternImage: UIImage(named: "two")!)
}

关于ios - 每次单击按钮时可以更改背景图像吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40792732/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com