Ios 如何点击背景图像,以便根据switch语句进行更改
因此,我希望应用程序根据我的Ios 如何点击背景图像,以便根据switch语句进行更改,ios,swift,xcode,Ios,Swift,Xcode,因此,我希望应用程序根据我的开关语句中的条件更改背景图像。现在我不知道如何点击背景图像以使其改变,例如,当案例介于801…804之间时,它不仅会返回“cloud.bolt”,还会返回我资产中的图像。代码如下: var conditionName: String { switch conditionId { case 200...232: return "cloud.bolt" case 300...321: return "cloud.dr
开关
语句中的条件更改背景图像。现在我不知道如何点击背景图像以使其改变,例如,当案例
介于801…804
之间时,它不仅会返回“cloud.bolt”
,还会返回我资产中的图像。代码如下:
var conditionName: String {
switch conditionId {
case 200...232:
return "cloud.bolt"
case 300...321:
return "cloud.drizzle"
case 500...531:
return "cloud.rain"
case 600...622:
return "cloud.snow"
case 701...781:
return "cloud.fog"
case 800:
return "sun.max"
case 801...804:
return "cloud.bolt"
default:
return "cloud"
}
}
不确定这是否正是您想要的,或者它是否可以改进(我对编程完全陌生),但接下来
override func viewDidLoad() {
super.viewDidLoad()
var conditionName = String()
var imageView: UIImageView!
imageView = UIImageView(frame: view.bounds)
imageView.contentMode = UIView.ContentMode.scaleAspectFill
imageView.clipsToBounds = true
imageView.center = view.center
self.view.sendSubviewToBack(imageView)
func assignBackground() {
view.addSubview(imageView)
}
func changeBackground(condition: Int) {
switch condition {
case 200...232:
imageView.image = UIImage(named: "img1")
conditionName = "cloud.bolt"
case 300...321:
imageView.image = UIImage(named: "img2")
conditionName = "cloud.drizzle"
case 500...531:
imageView.image = UIImage(named: "img3")
conditionName = "cloud.rain"
case 600...622:
imageView.image = UIImage(named: "img4")
conditionName = "cloud.snow"
case 701...781:
imageView.image = UIImage(named: "img5")
conditionName = "cloud.fog"
case 800:
imageView.image = UIImage(named: "img6")
conditionName = "sun.max"
case 801...804:
imageView.image = UIImage(named: "img7")
conditionName = "cloud.bolt"
default:
imageView.image = UIImage(named: "img8")
conditionName = "cloud"
}
assignBackground()
}
changeBackground(condition: 500)
}
不确定这是否正是您想要的,或者它是否可以改进(我对编程完全陌生),但接下来
override func viewDidLoad() {
super.viewDidLoad()
var conditionName = String()
var imageView: UIImageView!
imageView = UIImageView(frame: view.bounds)
imageView.contentMode = UIView.ContentMode.scaleAspectFill
imageView.clipsToBounds = true
imageView.center = view.center
self.view.sendSubviewToBack(imageView)
func assignBackground() {
view.addSubview(imageView)
}
func changeBackground(condition: Int) {
switch condition {
case 200...232:
imageView.image = UIImage(named: "img1")
conditionName = "cloud.bolt"
case 300...321:
imageView.image = UIImage(named: "img2")
conditionName = "cloud.drizzle"
case 500...531:
imageView.image = UIImage(named: "img3")
conditionName = "cloud.rain"
case 600...622:
imageView.image = UIImage(named: "img4")
conditionName = "cloud.snow"
case 701...781:
imageView.image = UIImage(named: "img5")
conditionName = "cloud.fog"
case 800:
imageView.image = UIImage(named: "img6")
conditionName = "sun.max"
case 801...804:
imageView.image = UIImage(named: "img7")
conditionName = "cloud.bolt"
default:
imageView.image = UIImage(named: "img8")
conditionName = "cloud"
}
assignBackground()
}
changeBackground(condition: 500)
}
备选案文1。如果图像资产的名称与您在
条件名称
中返回的字符串相同,则无需返回图像。您只需使用初始值设定项将其设置为backgroundImageView的图像:
backgroundImageView.image = UIImage(named: conditionName)
或
备选案文2。但是,如果需要返回字符串和图像,只需将变量的类型更改为(字符串,UIImage?
):
备选案文1。如果图像资产的名称与您在条件名称
中返回的字符串相同,则无需返回图像。您只需使用初始值设定项将其设置为backgroundImageView的图像:
backgroundImageView.image = UIImage(named: conditionName)
或
备选案文2。但是,如果需要返回字符串和图像,只需将变量的类型更改为(字符串,UIImage?
):
是否在某些UIImageView
中显示背景图像?我不这么认为。我的意思是,我只是通过主故事板添加了一些图像。目前我实际上不在家,所以我看不到我的代码,但我明天会更新并给你更好的图片。在一些UIImageView
中显示的是背景图像吗?我不这么认为。我的意思是,我只是通过主故事板添加了一些图像。目前我实际上不在家,所以我看不到我的代码,但我明天会更新,给你更好的图片。这很好,但现在我得到了:不,等等,你在做我答案中的这两件事。您需要执行第1点或第2点。如果要使用返回的字符串,则不需要元组。可能是我在回答中没有把区别讲清楚。我现在已经更新了答案,请检查。谢谢你的帮助:)这很好,但现在我得到了:不,等等,你在做我答案中的这两件事。您需要执行第1点或第2点。如果要使用返回的字符串,则不需要元组。可能是我在回答中没有把区别讲清楚。我已经更新了答案,请检查。谢谢您的帮助:)