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

How to refresh a background image depending on the proximity of a beacon?

我想根据信标的接近程度更改我的应用程序的背景图片。如果我 运行 我 iPhone 上的应用程序(参见下面的代码),它会根据距离正确显示背景图像 - 但如果我移动,它不会 change/refresh 背景图像. 我尝试了同样的背景颜色(见下面的代码)并且它起作用了:应用程序根据信标的接近程度改变它的背景颜色,如果我正确移动它 changes/refreshes 背景颜色。只是背景图片不能正常显示。

如何强制背景图片正确刷新?

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()#>
        }
    }
}
  • 您将在零索引处插入所有图像,首先删除上一张图像,就好像它包含任何图像一样,在接近度之间切换将插入许多图像,所有图像都在零索引处,最后一个将出现。或者只需将其添加为子视图并置于前面(如果需要)而不是在零处插入。
    • 颜色有效,因为您的视图只有一种颜色。图片正在一张一张添加。为了更好地理解尝试查看调试。