我想根据信标的接近程度更改应用程序的背景图像。如果我在iPhone上运行该应用程序(请参见下面的代码),则它会根据接近程度正确显示背景图片-但是,如果我移动,它不会更改/刷新背景图片。 我尝试使用背景色进行相同操作(请参见下面的代码),并且可以正常工作:该应用程序根据信标的接近程度更改其背景色,并且如果我正确移动它,则可以更改/刷新背景色。只是背景图片无法正常工作。
如何强制背景图像正确刷新?
func updateDistance(_ distance: CLProximity) {
UIView.animate(withDuration: 0.8) {
switch distance {
case .unknown:
let backgroundImage = UIImageView(frame: UIScreen.main.bounds)
backgroundImage.image = UIImage(named: "1.jpg")
//backgroundImage.reloadInputViews()
backgroundImage.contentMode = UIView.ContentMode.scaleAspectFill
self.view.insertSubview(backgroundImage, at: 0)
//print("nothing here..")
case .far:
let backgroundImage = UIImageView(frame: UIScreen.main.bounds)
//backgroundImage.image = UIImage(named: "2.png")
backgroundImage.image = UIImage(named: "3.png")
//backgroundImage.reloadInputViews()
backgroundImage.contentMode = UIView.ContentMode.scaleAspectFill
self.view.insertSubview(backgroundImage, at: 0)
case .near:
let backgroundImage = UIImageView(frame: UIScreen.main.bounds)
backgroundImage.image = UIImage(named: "4.png")
//backgroundImage.reloadInputViews()
backgroundImage.contentMode = UIView.ContentMode.scaleAspectFill
self.view.insertSubview(backgroundImage, at: 0)
case .immediate:
let backgroundImage = UIImageView(frame: UIScreen.main.bounds)
backgroundImage.image = UIImage(named: "5.jpg")
//backgroundImage.reloadInputViews()
backgroundImage.contentMode = UIView.ContentMode.scaleAspectFill
self.view.insertSubview(backgroundImage, at: 0)
@unknown default:
// fatalError()
let backgroundImage = UIImageView(frame: UIScreen.main.bounds)
backgroundImage.image = UIImage(named: "6.jpg")
//backgroundImage.reloadInputViews()
backgroundImage.contentMode = UIView.ContentMode.scaleAspectFill
self.view.insertSubview(backgroundImage, at: 0)
}
}
func updateDistance(_ distance: CLProximity) {
UIView.animate(withDuration: 0.8) {
switch distance {
case .unknown:
self.view.backgroundColor = UIColor.gray
print("nothing here..")
case .far:
self.view.backgroundColor = UIColor.blue
case .near:
self.view.backgroundColor = UIColor.orange
case .immediate:
self.view.backgroundColor = UIColor.red
// self.sendUserNotification()
@unknown default:
<#fatalError()#>
}
}
}
答案 0 :(得分:0)