[英]Can you change the background image each time you click the button?
我已经检查了文档并成功添加了图像作为背景,但是,既然已经设置,就无法更改。
我要做的是每次单击按钮时更改背景图像。 在网上查找时,我发现了有关如何更改按钮背景的信息,但没有更改实际背景的信息。
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")!)
}
}
```
我不确定问题出在哪里,因为您没有向我们展示您已经尝试过的内容。
您用于设置背景视图的相同代码可以在任何地方使用,并且可以随意使用
if <some condition>
{
self.view.backgroundColor = UIColor(patternImage: UIImage(named: "one")!)
}
if <another condition>
{
self.view.backgroundColor = UIColor(patternImage: UIImage(named: "two")!)
}
我认为您的问题与按钮无关,与条件等一般概念无关。 正如罗素所说,您只需要条件。 您已经证明了图像加载是可行的,所以现在您只需要“重新”使用另一张图像即可。
我有几点提示。 首先,不要使用名词来命名函数,例如ButtonTapped或类似名称。 快捷键3中的Button将是UIButton,因此它将无法正常工作。
我知道您没有更改Button的背景,但我认为您已经在更改。 如果是的话,按钮的行为就不会像图像和视图那样。 他们有“国家背景”和“国家标题”。 哎呀,您甚至不能使用Core Animation缩放它们,您必须缩放它们的超级视图才能看到它。 虽然我现在暂不介绍Core Animation。
您想要做的就是罗素所说的,但是尝试一些简单的事情。 喜欢:
var switch:bool = true;
func buttonTapped() {
if (switch == true) {
self.view.backgroundColor = UIColor(patternImage: UIImage(named: "one");
} else {
self.view.backgroundColor = UIColor(patternImage: UIImage(named: "two");
}
switch = !switch;
}
我首先要做一些简单的事情,以证明它可以正常工作。view.backgroundColor = UIColor.blackColor(); 和橙色/白色的东西。
无论您是否应该强制拆开包装,获得良好的命名习惯,理解结构等,我都建议学习C ++。 一个简单的C ++教程将比任何有关swift或iPhone编程的新手书涵盖更多的结构和模块化。 另外,所有其他语言只是其功能的一小部分,因此您将很快选择其他所有内容。
用以下代码替换代码:
@IBAction func Button() {
currentIndex += 1
if currentIndex == arrays.count {
currentIndex = 0
}
ChangeText.text = arrays[currentIndex]
if image == UIImage(named: "Image2")
{
self.view.backgroundColor = UIColor(patternImage: UIImage(named: "Image3")!)
return;
}
else if image3 == UIImage(named: "Image3")
{
self.view.backgroundColor = UIColor(patternImage: UIImage(named: "Image4")!)
return;
}
else if image == UIImage(named: "Image")
{
self.view.backgroundColor = UIColor(patternImage: UIImage(named: "Image2")!)
return;
}
else
{
self.view.backgroundColor = UIColor(patternImage: UIImage(named: "Image")!)
return;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.