gpt4 book ai didi

swift - 如何根据信标的接近程度刷新背景图像?

转载 作者:行者123 更新时间:2023-11-30 10:44:07 25 4
gpt4 key购买 nike

我想根据信标的接近程度更改应用程序的背景图像。如果我在 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()#>
}
}
}

最佳答案

  • 您将在零索引处插入所有图像,首先删除前一个图像,就好像它包含任何图像一样,在邻近度之间切换会插入许多图像,所有图像都为零,最后一个将出现。或者只需将其添加为 subview 并将其置于前面(如果需要),而不是在零处插入。
    • 颜色有效,因为您的 View 将只有一种颜色。图片正在一一附加。为了更好地理解,请尝试查看调试。

关于swift - 如何根据信标的接近程度刷新背景图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56165678/

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